【问题标题】:Getting "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version"收到“您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册”
【发布时间】:2014-01-20 11:00:04
【问题描述】:

有人可以看看我的代码,找出我得到这个的原因吗:

错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以便在第 1 行的 '' 附近使用

我知道这会很简单,但我看不到。

<body>
<?php
//connect to database//
$dbc = mysql_connect("localhost", "root", "***");
if (!$dbc)
die ('Could not connect: ' . mysql_error());

//select database//
$db_selected = mysql_select_db("tafe", $dbc );
if (!$db_selected)
die ('Could not connect: ' . mysql_error());

// initialise variables to store form control values
$Name = "";
$Address = "";
$Phone = "";
$Mobile = "";
$Email = "";


if($_SERVER['REQUEST_METHOD'] == "POST") // if form has been posted
{  
// initialise variables to store posted values
$ContactID = $_POST["ContactID"];
$Name = $_POST["Name"];
$Address = $_POST["Address"];
$Phone = $_POST["Phone"];
$Mobile = $_POST["Mobile"];
$Email = $_POST["Email"];

//build sql insert statement
$qry = "UPDATE contacts SET Name = '" . $Name . "', Address = '" . $Address . "', Phone = '" . $Phone . "', Mobile = '" . $Mobile . "', Email = '" . $Email . "' WHERE ContactID =" . $ContactID;

// run insert statement against database
$rst = mysql_query($qry, $dbc);

if ($rst)
{
    echo "<b><font color='green'>The contact has been updated.</font></b>";
    echo "</br></br>";
    echo "<a href=list-contacts.php>Continue</a>"; 
}

else 

{
    echo "<b><font color='red'>Error: ". mysql_error($dbc) . "</font></b>"; //alert if contact could not be added//
}

}


else // if form has not been posted
{
// build sql statement
$qry = "SELECT * FROM contacts WHERE ContactID = " . $_GET["ContactID"];


// run select statement
$rst = mysql_query($qry, $dbc);

if ($rst)
{
    $row = mysql_fetch_assoc($rst); // fetch row and place column values into respective place holder variable 

    $Name = $row["Name"];
    $Address = $row["Address"];
    $Phone = $row["Phone"];
    $Mobile = $row["Mobile"];
    $Email = $row["Email"];
}

else // in case of an error
{
    echo "<b><font color='red'>Error: ". mysql_error($dbc) . "</font></b>"; 
} // end of nested else statement ?>

<form name="editcontact" method="post" action="edit-contact.php"> 
<table border="1" cellpadding="2">
<caption> Caption 5</caption>

<!--Name Input-->
<tr>
<td><label for="Name">Name</label></td>
<td><input type="text" name="Name" value="<?php echo $Name ?>" size="30" maxlength="50" tabindex="1"/>
</td>
</tr>

<!-- Address Input-->
<tr>
<td><label for="Address">Address</label></td>
<td><textarea name="Address" cols="45" rows="5" tabindex="2"><?php echo $Address?></textarea></td>
</tr>

<!--Phone Input-->
<tr>
<td><label for="Phone">Phone</label></td>
<td><input type="text" name="Phone" value="<?php echo $Phone ?>" size="20" maxlength="20" tabindex="3" /> </td>
</tr>

<!--Mobile Input-->
<tr>
<td><label for="Mobile">Mobile</label></td>
<td><input type="text" name="Mobile" value="<?php echo $Mobile ?>" size="20" maxlength="20" tabindex="4" /> </td>
</tr>

<!--Email Input-->
<tr>
<td><label for="Email">Email</label></td>
<td><input type="text" name="Email" value="<?php echo $Email ?>" size="30" maxlength="50" tabindex="5" /></td>
</tr>

<!--Submit Button-->
<tr>
<td colspan="2" align="center"><input type="submit" name="Submit" value="Submit" tabindex="6"/>      
</td>
</tr>

</table>
</form>



<?php
} // end of main else statement

mysql_free_result($rst); //free memory//

?>

</body>
</html>`

【问题讨论】:

  • 您确定名称和地址不包含 ' 吗?你确定 $ContactID 是一个整数吗?
  • var_dump($qry); 显示什么?
  • 我完全复制并粘贴了我的编码,因此不确定您的意思是 $Name 和 $Address 包含 '. ContactID 在我的 sql 数据库中绝对是一个整数
  • var_dump($qry);返回NULL,这是什么意思?
  • 我的意思是:想想如果地址是:例如 John's Street 54 会发生什么。

标签: php mysql sql


【解决方案1】:

$_POST["ContactID"] 返回 null,这就是您收到该错误的原因。
将 ContactID 发送到服务器:

<input type="hidden" name="ContactID" value="<?php echo $_GET["ContactID"]; ?>" />

您的代码有七个问题:

  1. 不要使用mysql_* 函数。它们已经过时了。使用mysqli_*PDO
  2. 始终检查用户发送的数据,否则用户可能会删除您的数据库。
  3. 不要使用&lt;b&gt;&lt;font&gt; 标签。现在是 2014 年。使用 HTML5 和 CSS3。
  4. 使用htmlspecialchars(),否则用户将能够攻击您的网站 (XSS)
  5. 如果使用标签,则需要设置输入的id。
  6. 不要使用表格来构建站点。使用浮动 div。

这段代码会很好用:

 <?php
try
{
    $db = new PDO("mysql:dbname=tafe;host=localhost", "root", "***");
}
catch (PDOException $e)
{
    die("Cannot connect to database.");
}
function post($name)
{
    return isset($_POST[$name]) ? $_POST[$name] : "";
}
function html($x)
{
    return htmlentities($x, ENT_QUOTES, "UTF-8");
}
if (post("id"))
{  
    $query = $db->prepare("UPDATE contacts SET Name = :name, Address = :address, Phone = :phone, Mobile = :mobile, Email = :email WHERE ContactID = :id");
    $query->bindParam(":name", post("name"));
    $query->bindParam(":address", post("address"));
    $query->bindParam(":phone", post("phone"));
    $query->bindParam(":mobile", post("mobile"));
    $query->bindParam(":email", post("email"));
    $query->bindParam(":id", post("id"));
    if ($query->execute())
        $message = '<span style="color: green; font-weight: bold;">The contact has been updated.</span><br /><a href="list-contacts.php">Continue</a>';
    else
        $message =  '<span style="color: red; font-weight: bold;">There was an error.</span>';
}
elseif (isset($_GET["ContactID"]))
{
    $query = $db->prepare("SELECT Name, Address, Phone, Mobile, Email FROM contacts WHERE ContactID = :id");
    $query->bindParam(":id", $_GET["ContactID"]);
    if ($query->execute())
    {
        if (!$query->rowCount())
            $message = '<span style="color: red; font-weight: bold;">This contact does not exists.</span>';
        else
        {
            $row = $query->fetch(PDO::FETCH_ASSOC);
            foreach ($row as $k => $v)
                $_POST[$k] = $v;
        }
    }
    else
        $message = '<span style="color: red; font-weight: bold;">There was an error.</span>';
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Contact</title>
        <meta charset="utf-8" />
    </head>
    <body>
        <?php
        if (isset($message))
            echo "<p>".$message."</p>";
        ?>
        <form action="edit-contact.php" method="post"> 
            <label for="name">Name:</label><br />
            <input type="text" name="name" id="name" value="<?php echo html(post("name")) ?>" /><br />
            <label for="address">Address:</label><br />
            <textarea name="address" id="address"><?php echo html(post("address")) ?></textarea><br />
            <label for="phone">Phone:</label><br />
            <input type="text" name="phone" id="phone" value="<?php echo html(post("phone")) ?>" /><br />
            <label for="mobile">Mobile:</label><br />
            <input type="text" name="mobile" id="mobile" value="<?php echo html(post("mobile")) ?>" /><br />
            <label for="email">Email:</label><br />
            <input type="text" name="email" id="email" value="<?php echo html(post("email")) ?>" /><br />
            <input type="submit" name="submit" value="Submit" />      
            <input type="hidden" name="id" value="<?php echo isset($_GET["ContactId"]) ? intval($_GET["ContactId"]) : "0" ?>" />
        </form>
    </body>
</html>

【讨论】:

  • 唯一不包含某种SQL注入的答案,+1!
【解决方案2】:

试试这个

 $qry = "UPDATE contacts 
         SET Name = '" . $Name . "', 
             Address = '" . $Address . "', 
             Phone = '" . $Phone . "', 
             Mobile = '" . $Mobile . "', 
             Email = '" . $Email . "' 
         WHERE ContactID = '" . $ContactID . "' " ;

并更改为该查询

  $qry = "SELECT * FROM contacts WHERE ContactID = '" . $_GET['ContactID']."' " ;

nB:

1- 你应该通过mysql_real_escape_string()转义你的变量

2- 你应该使用 PDO 或 MYSQLI 而不是 MYSQL

【讨论】:

    【解决方案3】:

    试试这个

    $qry = "UPDATE contacts SET 
                    Name = '" . mysql_real_escape_string($Name) . "', 
                    Address = '" . mysql_real_escape_string($Address) . "', 
                    Phone = '" . mysql_real_escape_string($Phone) . "', 
                    Mobile = '" . mysql_real_escape_string($Mobile) . "', 
                    Email = '" . mysql_real_escape_string($Email) . "' 
                    WHERE ContactID =" . $ContactID;
    

    确保在您的 html 表单中有一个隐藏的文本框或名称为“ContactID”的文本框 由于您在查询中使用它,而我在表单中看不到它。

    $ContactID = $_POST["ContactID"];
    

    注意:您正在使用已弃用的 mysql_* 函数,请开始使用 mysqli_* 函数或 PDO

    【讨论】:

    • 试试 echo $qry ;看看你得到了什么并发布它,可能是你的 $ContactID 不正确
    • 您应该将$ContactID 转换为整数,否则也会包含SQL 注入漏洞。
    • 我明白了:更新联系人 SET Name = '',Address = '',Phone = '',Mobile = '',Email = '' WHERE Con​​tactID =Error: You have an error in your SQL 语法;检查与您的 MySQL 服务器版本相对应的手册,以在第 7 行的 '' 附近使用正确的语法 我也尝试过 WHERE Con​​tactID =" .$_POST['ContactID']; 但仍然收到错误消息
    • 是的,这意味着 ContactID 为空,现在在您的表单中,您需要有 ContactID 我在您的表单中没有看到。最后你需要做 $ContactID = (int)$_POST["ContactID"];但在确保您的表单具有 ContactID
    • 谢谢!!!在我调用“$ContactID”的任何时候添加(int)前面,我在页面加载时遇到第二个错误,因为我没有在我的编码中进一步添加 ContactID 前面的(int)。现在都修好了,非常感谢大家的帮助!
    猜你喜欢
    • 2014-01-27
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2016-06-29
    相关资源
    最近更新 更多