How to insert form data into a MySQL database table
So, I have this registration script:
HTML:
<form action="register.php" method="POST">
<label>Username:</label> <input type="text" name="username" /><br />
<label>Password:</label> <input type="text" name="password" /><br />
<label>Gender:</label>
<select name="gender">
<optgroup label="genderset">
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Hermaphrodite">Hermaphrodite</option>
<option value="Not Sure!!!">Not Sure!!!</option>
</optgroup>
</select><br />
<input type="submit" value="Register" />
</form>
PHP / SQL:
<?php
$username = $_POST['username'];
$password = $_POST['password'];
$gender = $_POST['gender'];
mysql_query("INSERT INTO registration_info (username, password, gender) VALUES ('$username', '$password', '$gender')
")
?>
The problem is that the username and password are inserted into the table "registration_info" in order. But gender input from the drop down menu does not work. Can someone tell me how to fix this, thanks.
Try this if you accepted the gender field as a whole in the database
<select name="gender">
<optgroup label="genderset">
<option value="1">Male</option>
<option value="2">Female</option>
<option value="3">Hermaphrodite</option>
<option value="4">Not Sure!!!</option>
</optgroup>
</select>
Then the selected value is inserted into the database
There are two options for your question: -
1) If you want to store the gender value in your database then change the datatype of gender field to varchar and then try again
2) If your gender field is integer in database then you have to make changes in select option value like following
<select name="gender">
<optgroup label="genderset">
<option value="1">Male</option>
<option value="2">Female</option>
<option value="3">Hermaphrodite</option>
<option value="4">Not Sure!!!</option>
</optgroup>
</select>
The second option is similar to @sneha's suggestion.
It can help you.