将数据插入数据库
我想做的是:当我从下拉列表中选择颜色时,它将被提交到数据库并进入car表。我已经设置了数据库。
这是 HTML 代码的一部分。
<form action="rinnad.php" method="post">
Car color:
<select name="Rinnad">
<option>red</option>
<option>blue</option>
<option>green</option>
</select>
<input type="submit" value="lisa" />
</form>
这是我用来将汽车颜色输入数据库的 PHP 代码:
<?php
$connect = @mysql_connect ("localhost", "root", "") or die("Fail!!!! :D:D:D");
mysql_select_db("tibid") or die("there is no such db");
if($connect){
echo "connected to db";
}else{
echo "didnt connect to db";
}
$car = "red"
$query = mysql_query("insert into test values('','$car')");
?>
我需要做什么? 谢谢。
What I am trying to do is this: When I select color from dropdown list, it will be submited to database and into car table. I have already setup the database.
This is part of the HTML code.
<form action="rinnad.php" method="post">
Car color:
<select name="Rinnad">
<option>red</option>
<option>blue</option>
<option>green</option>
</select>
<input type="submit" value="lisa" />
</form>
Here is the PHP code that I am using to enter car color into database:
<?php
$connect = @mysql_connect ("localhost", "root", "") or die("Fail!!!! :D:D:D");
mysql_select_db("tibid") or die("there is no such db");
if($connect){
echo "connected to db";
}else{
echo "didnt connect to db";
}
$car = "red"
$query = mysql_query("insert into test values('','$car')");
?>
What do I need to do?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,您需要为选项赋值:
http://www.w3schools.com/TAGS/tag_option .asp
之后,您需要将表单中的值分配给您的 $car 变量:
或者,更安全地,对 SQL 注入进行一些保护:
First you need to assign values to the options:
http://www.w3schools.com/TAGS/tag_option.asp
After that your need to assign the value from the form to your $car variable:
or, more securely, with some protection against SQL-injections:
放一个 ;在此行之后
Put a ; after this line
您必须获得发布的值:
You would have to get the posted value:
你遇到了一个错误,
Here you did not指定 $car 指定的字段名。我想你给了汽车 ID 的 auto_increment..??
thn 您的代码将
不需要指定自动增量字段。默认可以添加它。
You got an error with
Here you did'nt specify the fieldname to which $car specifies.i thnk u gave the auto_increment for car id..??
thn Your code will be
you neednot specify the auto increment field.default it can be added.