如何使用php中的下拉列表将值插入数据库

时间:2014-01-20 20:28:17

标签: php sql

我正在尝试使用此代码向我的数据库添加值,但它不起作用。

的index.html

    <html>
<head>
<body>
<select style="width:200px;" name="eventList" tabindex="5">
//Three option values in a group

<form action="reg.php" method=POST>

<input type="submit" value="register">
</form>
</body>
</head>
</html>

php文件

<?php
$conn=mysql_connect("localhost","root","");
mysql_select_db('reg');
$list = isset($_POST['eventList']) ? $_POST['eventList'] : '';
$query = "INSERT INTO 'reg1' ('choice') VALUES ('$list')";
$result = mysql_query($query, $conn);

    if ($result) {
    // Success!
        echo " Success ";                           
        } else {
                    // Display error message.
        echo "<p>Updation Failed</p>";
                   echo "<p>" . mysql_error() . "</p>";                         
              }                         

?>

当我点击提交按钮时,出现错误:

Updation Failed

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 ''reg1' ('choice') VALUES ('Vogue')' at line 1

如何解决此问题?

3 个答案:

答案 0 :(得分:1)

你忘了;

mysql_select_db(reg);

并使用单引号:

$query = "INSERT INTO `reg1` (`choice`) VALUES ('$list')";

答案 1 :(得分:1)

$query = "INSERT INTO `reg1` (`choice`) VALUES (`.$list`)";

应该是:

$query = "INSERT INTO `reg1` (`choice`) VALUES ('$list')";

答案 2 :(得分:0)

<?php
$conn=mysql_connect("localhost","root","");
mysql_select_db('reg');
    $list = isset($_POST['eventList']) ? $_POST['eventList'] : '';
    $query = "INSERT INTO `reg1` (`choice`) VALUES ('$list')";
    $result = mysql_query($query, $conn);
         if ($result) {
            // Success!
            echo " Success ";                           
          } else {
            // Display error message.
            echo "<p>Updation Failed</p>";
            echo "<p>" . mysql_error() . "</p>";                            
                            }                           

?>