【问题标题】:MYSQL syntax error submitting with PHP [duplicate]用PHP提交MYSQL语法错误[重复]
【发布时间】:2013-10-04 00:51:26
【问题描述】:

我有点卡住了。我不断收到语法错误。任何人都可以看看,让我知道出了什么问题?

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“return (Badge, Dept, Asset) VALUES ('61584', 'IT', 12345)' 附近使用正确的语法

<?php
$dept = $_GET['dept'];
$badge = $_GET['badge'];
if (empty($badge)) {
             echo '<center> You must insert a value into the Badge field </center>';
    }
    else {
foreach($_GET['asset'] as $key => $value) {
            $db=mysql_connect ("localhost",  "USER", "PASSWORD") or die ('I cannot connect  to  the database because: ' . mysql_error());
            $mydb=mysql_select_db("radios") or die ('I cannot select that table' . mysql_error());
#                mysql_query("INSERT INTO return  (Badge, Dept, Asset)  VALUES (test, test, test)") or die ('Can not write to DB because: ' . mysql_error());
            mysql_query("INSERT INTO return (Badge, Dept, Asset)  VALUES ('$badge', '$dept', $value)") or die ('<body style="background-color:red">' . mysql_error());
echo $value;
echo '<br>';
            }
echo '<body style="background-color:green">';

}
?>
<html>
    <head>
            <title>Radio return</title>



    </head>
    <body>
            <form action="index.php" method="get" id="badgeform">
            <center> Badge: <input type="text" name="badge" id="badgefield"/>
            <script>document.getElementById('badgefield').focus()</script>
            <center> Dept: <input type="text" name="dept" id="deptfield"/>
            <center> Asset: <input type="text" name="asset[1]" id="assetfield"/>
            <center> Asset: <input type="text" name="asset[2]" id="assetfield"/>
            <center> Asset: <input type="text" name="asset[3]" id="assetfield"/>
            <center> Asset: <input type="text" name="asset[4]" id="assetfield"/>
            <center> Asset: <input type="text" name="asset[5]" id="assetfield"/>
            <center> Asset: <input type="text" name="asset[6]" id="assetfield"/>
            <center> Asset: <input type="text" name="asset[7]" id="assetfield"/>
            <center> Asset: <input type="text" name="asset[8]" id="assetfield"/>
            <center> Asset: <input type="text" name="asset[9]" id="assetfield"/>
            <center> Asset: <input type="text" name="asset[10]" id="assetfield"/>
            <center> Asset: <input type="text" name="asset[11]" id="assetfield"/>
            <center> Asset: <input type="text" name="asset[12]" id="assetfield"/>
            <center> Asset: <input type="text" name="asset[13]" id="assetfield"/>
            <center> Asset: <input type="text" name="asset[14]" id="assetfield"/>
            <center> Asset: <input type="text" name="asset[15]" id="assetfield"/>
            <center> Asset: <input type="text" name="asset[16]" id="assetfield"/>
            <center> Asset: <input type="text" name="asset[17]" id="assetfield"/>
            <center> Asset: <input type="text" name="asset[18]" id="assetfield"/>
            <center> Asset: <input type="text" name="asset[19]" id="assetfield"/>
            <center> Asset: <input type="text" name="asset[20]" id="assetfield"/>
            <input type="submit"> </center>
            </form>


    </body>


</html>

【问题讨论】:

  • 警告: 自 PHP 5.5.0 起,mysql 扩展为 deprecated,未来将被删除。相反,应该使用MySQLiPDO_MySQL 扩展名。请不要使用mysql_* 开发新代码。
  • return 是保留字。 `return` 应该可以工作。
  • RETURN 是 MySQL 中的保留字;你可以用反引号引用它,或者将它重命名为不是保留字的东西
  • 你的代码容易被sql注入。你应该更好地使用准备好的语句。至少逃脱字符串。还将您与数据库的连接移至foreach 循环之外

标签: php mysql sql syntax


【解决方案1】:

RETURNreserved keyword

使用 INSERT INTO `return` (B...

【讨论】:

  • 哇,我觉得自己很愚蠢,哈哈。谢谢!
【解决方案2】:

您的表被命名为 MySQL 保留字。这意味着你应该尝试这样的事情。

INSERT INTO `return` ....

Read More

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    • 2016-08-14
    • 1970-01-01
    相关资源
    最近更新 更多