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

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

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..,

  • 写回答

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条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器