dongqiuxu2270
2014-06-04 04:46
浏览 54
已采纳

将值从下拉列表插入数据库表

I am new to PHP. When i am trying to insert the value from drop down list to database table showing some errors

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

Thank you..,

图片转代码服务由CSDN问答提供 功能建议

我是PHP的新手。 当我试图将值从下拉列表插入到显示一些错误的数据库表

代码

 &lt; html&gt; \  n&lt; head&gt; 
&lt; title&gt; OPTION&lt; / title&gt; 
&lt; / head&gt; 
&lt; body&gt; 
&lt; form action =“option.php”method =“get”&gt; 
Name:&lt; select name  =“name”&gt; 
&lt; option value =“name1”&gt; name1&lt; / option&gt; 
&lt; option value =“name2”&gt; name2&lt; / option&gt; 
&lt; option value =“name3”&gt; name3&lt;  ; / option&gt; 
&lt; option value =“name4”&gt; name4&lt; / option&gt; 
&lt; / select&gt;&lt; br&gt; 
&lt; input type =“submit”name =“submit”value =“Insert”&gt  ; 
&lt; / form&gt; 
&lt; / body&gt; 
&lt; / html&gt; 
&lt;?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”&lt; br&gt;“。$ name。”inserted“; 
} 
 else 
 {
 echo mysql_error(  ); 
} 
} 
?&gt; 
    
 
 

当我尝试显示此错误时

您的SQL语法中有错误; 检查与MySQL服务器版本对应的手册,以便在第1行的'选项(名称)值('name3')'附近使用正确的语法

谢谢..,

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

  • dousi2013 2014-06-04 04:50
    最佳回答

    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.

    Change the table name to option_test or something and make appropriate changes in your php code too. Then it will work.

    Also start using mysqli_ or PDO since mysql_ has been deprecated from PHP5 onwards.

    评论
    解决 无用
    打赏 举报
查看更多回答(3条)

相关推荐 更多相似问题