简体   繁体   中英

How to insert form data into MySQL database table

So I have this registration script:

The 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>

The 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, the username and password gets inserted into the "registration_info" table just fine. But the Gender input from the select drop down menu doesn't. Can some one tell me how to fix this, thanks.

检查数据库中的数据类型,可能存在不匹配

Try this one if you have taken gender field as integer 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 selected value is inserted into the database

您没有给任何选项赋值,因此只插入一个空字符串。

You need to set the value attribute in list items in the HTML.

eg

<option value="Male">Male</option>
  1. You have no password input here.
  2. Your gender select box 's options have no values.
  3. Use mysql_escape_string at least

    $username = mysql_escape_string($_POST['username']);

or parse to int if you except int value.

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

<?php echo var_dump($_POST['gender']); ?>

Does that tell you anything? Maybe your gender datatype (in your table) is not compatible with what you put into it? Perhaps it's an ENUM('M','F') for example.

Create a variable say, $gend in the php file.

Then,

$gend=$_POST['gender'];

Then, proceed with:

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

Try this.

There are two option 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>

Second option is same as @sneha suggestion.

It may help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM