php 如何将表单数据插入到 MySQL 数据库表中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2169736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert form data into MySQL database table
提问by Richard
So I have this registration script:
所以我有这个注册脚本:
The HTML:
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/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.
问题是,用户名和密码被插入到“registration_info”表中就好了。但是选择下拉菜单中的性别输入没有。谁能告诉我如何解决这个问题,谢谢。
回答by mouthpiec
check the datatype in the database, there could be a mismatch
检查数据库中的数据类型,可能存在不匹配
回答by sneha
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
然后将选定的值插入到数据库中
回答by Ignacio Vazquez-Abrams
You didn't give any of the options a value, so only an empty string gets inserted.
你没有给任何选项一个值,所以只插入一个空字符串。
回答by Paolo
You need to set the value attribute in list items in the HTML.
您需要在 HTML 的列表项中设置 value 属性。
e.g.
例如
<option value="Male">Male</option>
回答by hsz
You have nopasswordinput here.Yourgenderselect box's options have no values.Use
mysql_escape_stringat least$username = mysql_escape_string($_POST['username']);
你password在这里没有输入。您genderselect box的选项没有值。$username = mysql_escape_string($_POST['username']);
or parse to intif you except intvalue.
或者解析为int如果你除了int值。
$id = (int) $_POST['id'];
回答by Ron Korving
<?php echo var_dump($_POST['gender']); ?>
<?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.
这能告诉你什么吗?也许您的性别数据类型(在您的表格中)与您输入的内容不兼容?例如,它可能是一个 ENUM('M','F')。
回答by Harsh Sanghani
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.
第二个选项与@sneha 建议相同。
It may help you.
它可能会帮助你。
回答by Indu Bhusan Nath
Create a variable say, $gend in the php file.
在 php 文件中创建一个变量,比如 $gend。
Then,
然后,
$gend=$_POST['gender'];
$gend=$_POST['性别'];
Then, proceed with:
然后,继续:
mysql_query("INSERT INTO registration_info (username, password, gender) VALUES ('$username', '$password', '$gend') ") ;
mysql_query("插入注册信息(用户名,密码,性别) VALUES('$用户名','$密码','$gend')");
Try this.
尝试这个。

