将数据插入数据库

发布于 2024-11-04 03:24:50 字数 773 浏览 0 评论 0原文

我想做的是:当我从下拉列表中选择颜色时,它将被提交到数据库并进入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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如此安好 2024-11-11 03:24:50

首先,您需要为选项赋值:

<option value="red">red</option>

http://www.w3schools.com/TAGS/tag_option .asp

之后,您需要将表单中的值分配给您的 $car 变量:

$car = $_POST['Rinnad'];

或者,更安全地,对 SQL 注入进行一些保护:

$car = mysql_real_escape_string($_POST['Rinnad']);

First you need to assign values to the options:

<option value="red">red</option>

http://www.w3schools.com/TAGS/tag_option.asp

After that your need to assign the value from the form to your $car variable:

$car = $_POST['Rinnad'];

or, more securely, with some protection against SQL-injections:

$car = mysql_real_escape_string($_POST['Rinnad']);
蓝梦月影 2024-11-11 03:24:50

放一个 ;在此行之后

$car = "red";

Put a ; after this line

$car = "red";
我是有多爱你 2024-11-11 03:24:50

您必须获得发布的值:

$car = mysql_real_escape_string($_POST['Rinnad']);

You would have to get the posted value:

$car = mysql_real_escape_string($_POST['Rinnad']);
永言不败 2024-11-11 03:24:50

你遇到了一个错误,

$car = "red";
$query = mysql_query("insert into test values('','$car')");

Here you did not指定 $car 指定的字段名。我想你给了汽车 ID 的 auto_increment..??
thn 您的代码将

$query = mysql_query("insert into test (color)values('$car')");

不需要指定自动增量字段。默认可以添加它。

You got an error with

$car = "red";
$query = mysql_query("insert into test values('','$car')");

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

$query = mysql_query("insert into test (color)values('$car')");

you neednot specify the auto increment field.default it can be added.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文