<...">

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.

+5
source share
8 answers

check the type of data in the database, there may be a mismatch

+1
source

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

+1
source

, .

0

value HTML.

.

<option value="Male">Male</option>
0
  • password.
  • gender select box .
  • mysql_escape_string

    $username = mysql_escape_string ($ _ POST ['username']);

int, int .

$id = (int) $_POST['id'];
0

<? PHP echo var_dump ($ _ POST ['gender']); ? >

- ? , ( ) , ? , ENUM ( "M", "F" ), .

0

say $gend php.

$Gend = $_ POST [ ''];

:

mysql_query ("INSERT INTO registration_info (username, password, gender) VALUES ('$ username', '$ password', '$ gend')");

Try it.

-1
source

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.

-1
source

All Articles