Reputation: 938
I made a form where a user can select different options from a dropdown field. The selected option should be saved inside the database in another table. So here is what I did:
Get data from database for dropdown list:
include 'inc/database.php';
// Get all Schulen from Database
$get_schulname3 = "SELECT schulname FROM schule";
$result_get_schulname3 = mysqli_query($connect, $get_schulname3);
Display dropdown field on website:
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Schule:</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<?php while ($get_schulname3 = mysqli_fetch_array($result_get_schulname3, MYSQLI_BOTH)) {
echo "<select name='schule-lehrer' class='form-control'>Dropdown</option>";
while($r3 = mysqli_fetch_array($result_get_schulname3, MYSQLI_BOTH)){
echo "<option value=" . $r3['schulname'] . ">" .$r3['schulname'] . "</option>";
}
echo "</select>";
} ?><br /><br />
</div>
</div>
With this code everything is displayed and everything is working as it should. Now if a user selects something from the dropdown list, not all data are saved inside the database.
Here is how is save the data inside my database:
$schule_lehrer = mysqli_real_escape_string ($connect, $_POST['schule-lehrer']);
$query = "INSERT INTO teacher (schule) VALUES ('schule_lehrer')";
For example I have the following entries inside my dropdown field:
SRG Traumschule, AHS Bergschule, AHS Testschule,
If a user selects "SRG Traumschule" for example and submits the form, inside my database for the user I only see "SRG" instead of "SRG Traumschule" or if a user selects "AHS Testschule" I only see inside the database "Test". See here: http://awesomescreenshot.com/0c8572u7c8
Can someone help me and tell me why this issue appears? What am I doing wrong?
Upvotes: 1
Views: 52
Reputation: 21437
Its because you were not enclosing your option
elements value
attribute
echo "<option value=" . $r3['schulname'] . ">" .$r3['schulname'] . "</option>";
^^ ^^
needs to be
echo "<option value='" . $r3['schulname'] . "'>" .$r3['schulname'] . "</option>";
^^ ^^
Your code will work unless until you don't have space
within your values
Upvotes: 1