php 将下拉列表中的值插入到数据库表中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24029212/
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
insert values from a drop down list to database table
提问by najmal
I am new to PHP. When i am trying to insert the value from drop down list to database table showing some errors
我是 PHP 新手。当我尝试将下拉列表中的值插入到显示一些错误的数据库表时
the code
编码
<html>
<head>
<title>OPTION</title>
</head>
<body>
<form action="option.php" method="get">
Name :<select name="name">
<option value="name1">name1</option>
<option value="name2">name2</option>
<option value="name3">name3</option>
<option value="name4">name4</option>
</select><br>
<input type="submit" name="submit" value="Insert">
</form>
</body>
</html>
<?php
if(isset($_GET['submit']))
{
$name=$_GET['name'];
$c=mysql_connect("localhost","root","");
mysql_select_db("test");
$ins=mysql_query("insert into option (name) values ('$name')",$c);
if($ins)
{
echo "<br>".$name."inserted";
}
else
{
echo mysql_error();
}
}
?>
when i am trying out put showing this error
当我尝试显示此错误时
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'option (name) values ('name3')' at line 1
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在第 1 行的“选项(名称)值(“名称3”)附近使用的正确语法
Thank you..,
谢谢..,
回答by AeJey
you have to change the name of your table (option
). Option
is a mysql keyword and so it will cause syntax error while executing queries with php
.
您必须更改表的名称 ( option
)。Option
是一个 mysql 关键字,因此在使用php
.
Change the table name to option_test
or something and make appropriate changes in your php code too. Then it will work.
将表名更改为option_test
或其他内容,并在您的 php 代码中进行适当的更改。然后它会起作用。
Also start using mysqli_
or PDO
since mysql_
has been deprecated from PHP5 onwards.
也开始使用mysqli_
或PDO
因为mysql_
从 PHP5 开始已被弃用。
回答by najmal
Try this code, I changed the name
of select
to names
and
试试这个代码,我改变了name
的select
要names
和
use backticks around table name
在表名周围使用反引号
<form action="option.php" method="GET">
Name :<select name="names">
<option value="name1">name1</option>
<option value="name2">name2</option>
<option value="name3">name3</option>
<option value="name4">name4</option>
</select><br>
<input type="submit" name="submit" value="Insert">
</form>
<?php
if(isset($_GET['names']))
{
$name=$_GET['names'];
$c=mysql_connect("localhost","root","");
mysql_select_db("test");
$ins=mysql_query("INSERT INTO `option`
(name)
VALUES ('$name')",$c) or die(mysql_error());
if($ins)
{
echo "<br>".$name."inserted";
}
}
?>
回答by Rahul Goel
Html Code Is:
HTML代码是:
<select name="a">
<option value="val1">value1<option>
<option value="val2">value2<option>
<option value="val3">value3<option>
</select>
PHP Code Is:
PHP代码是:
<?php
$a=$_POST[a];
?>
The above php code will help you to fetch data from the selected option into php variable $a. From there on ou can insert your data into the query simply as you inserted before.
上面的 php 代码将帮助您从所选选项中获取数据到 php 变量 $a 中。从那时起,您可以像之前插入的那样将数据插入到查询中。
回答by Nils
in your code mysql_query("insert into option (name) values ('$name')",$c);
here is no need to add connection variable to that function.
you can just write mysql_query("insert into option (name) values ('$name')")or die(mysql_error());
reason for calling function mysql_error()is that you came to know what type of error is coming..
and another thing is you should change the name of your table from option to option1 or anything else...then it will work fine..no worry...:)
在您的代码中mysql_query("insert into option (name) values ('$name')",$c);
,无需向该函数添加连接变量。
你可以只写mysql_query("insert into option (name) values ('$name')")or die(mysql_error());
调用函数mysql_error() 的原因是你开始知道什么类型的错误即将发生..
另一件事是你应该将表的名称从 option 更改为 option1 或其他任何内容......然后它会工作正常..不用担心...:)