添加其他值时{mys}语法违规

时间:2015-08-15 23:51:41

标签: php mysql pdo

即时运行pdo查询更新我在我的表中的东西,它工作得很好,直到我不得不再添加两个值,我每次执行时都会出现语法错误,但是当我删除这两个新值时工作得很好。

这是来自带有帖子的表格

<div class="form-group">
                <label for="hours">Hours:</label>
                <input type="text" id="hours" name="hours" value="<?php echo $inventoryR['hours']; ?>" placeholder="hours"/>
              </div>

              <div class="form-group">
                <label for="condition">condition:</label>
                <input type="text" id="condition" name="condition" value="<?php echo $inventoryR['condition']; ?>" placeholder="condition"/>
              </div>

这是php

include "connect.php";

    $id = $_POST['machineId'];
    $tags = $_POST['tags'];
    $price = $_POST['price'];
    $status = $_POST['status'];
    $info = $_POST['info'];
    $hours = $_POST['hours'];
    $condition = $_POST['condition'];

    $specl1 = $_POST['specl1'];
    $specl2 = $_POST['specl2'];
    $specl3 = $_POST['specl3'];
    $specl4 = $_POST['specl4'];
    $specl5 = $_POST['specl5'];

    $spec1 = $_POST['spec1'];
    $spec2 = $_POST['spec2'];
    $spec3 = $_POST['spec3'];
    $spec4 = $_POST['spec4'];
    $spec5 = $_POST['spec5'];

    $carousel = "";
    $featured = "";
    $rental = "";

    foreach($status as $s)
    {
        if ($s == "carousel")
        {
            $carousel = $s;
        }
        else if ($s == "featured")
        {
            $featured = $s;
        }
        else if($s == "rental")
        {
            $rental = $s;
        }

    }

    $inventoryQ = $conn->prepare("UPDATE inventory SET tags=?, price=?, carousel=?, featured=?, rental=?, info=? WHERE id=?");

    //$query = $conn->prepare("UPDATE inventory SET hours=?, condition=? WHERE id=?");

    $inventoryQ->execute(array($tags, $price, $carousel, $featured, $rental, $info, $id));

    //$query->execute(array($hours, $condition, $id));

    $specsQ = $conn->prepare("INSERT INTO specs
                            (inventory_id, label1, spec1, label2, spec2, label3, spec3, label4, spec4, label5, spec5)
                            VALUES
                            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
                            ON DUPLICATE KEY UPDATE
                            label1 = VALUES(label1),
                            spec1 = VALUES(spec1),
                            label2 = VALUES(label2),
                            spec2 = VALUES(spec2), 
                            label3 = VALUES(label3),
                            spec3 = VALUES(spec3), 
                            label4 = VALUES(label4),
                            spec4 = VALUES(spec4), 
                            label5 = VALUES(label5),
                            spec5 = VALUES(spec5)");
    $specsQ->execute(array($id, $specl1, $spec1, $specl2, $spec2, $specl3, $spec3, $specl4, $spec4, $specl5, $spec5));

并且我有小时和条件作为表格中的varchar(250)。

当我试图用小时和条件执行任何事情时它失败了。当我像现在一样评论它们时,它可以与其他值一起使用。

错误讯息:

  

致命错误:带有消息'SQLSTATE [42000]的未捕获异常'PDOException':语法错误或访问冲突:1064 SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以便在/home/mlerma1/public_html/admin/include/edit.php中使用'condition ='附近的'WHERE id ='65''第1行'使用正确的语法: 52堆栈跟踪:#0 /home/mlerma1/public_html/admin/include/edit.php(52):PDOStatement-&gt;执行(数组)#main {main}抛出/ home / mlerma1 / public_html / admin / include /第52行的edit.php

我还尝试对小时和条件的查询进行评论,只是回显帖子值以查看它是否从表单中获取这些值,并确实输出了这些值。

1 个答案:

答案 0 :(得分:1)

那是因为CONDITION是一个保留字,需要使用下面的后缀来转义。见MySQL Documentation

UPDATE inventory SET `hours`=?, `condition`=? WHERE id=?";