mysqli更新无法正常工作

时间:2014-02-25 20:49:33

标签: php mysql sql mysqli

        $sql3 = "SELECT order_id FROM orders WHERE order_code = '$order_code'";
        $result3 = $conn->query($sql3) or exit("Error code ({$conn->errno}): {$conn->error}");

        $row = mysqli_fetch_assoc($result3);
        $order_id = $row['order_id'];       

        $deliv_date = date('Y-m-d');

        $sql = "UPDATE orders SET deliv_date = $deliv_date
                        WHERE order_id = $order_id";
        $result = $conn->query($sql) or exit("Error code ({$conn->errno}): {$conn->error}");



        $sql1 = "INSERT INTO invoice VALUES (0,'$order_code','$deliv_date','','$order_id')";

        $result1 = $conn->query($sql1) or exit("Error code ({$conn->errno}): {$conn->error}");

以上是我正在使用的代码。最后一行的插入工作正常,并获取正确的$ order_code,$ deliv_date和$ order_id。更新没有错误,但字段“deliv_date”永远不会设置为$ deliv_date。我只是无法从逻辑上看出问题所在。谁能发现它?我一直在上墙。

感谢。

2 个答案:

答案 0 :(得分:4)

您需要将日期用引号括起来:

$sql = "UPDATE orders SET deliv_date = $deliv_date
                    WHERE order_id = $order_id";

应该是

$sql = "UPDATE orders SET deliv_date = '$deliv_date' <-- HERE
                    WHERE order_id = $order_id";

答案 1 :(得分:1)

更改

$sql = "UPDATE orders SET deliv_date = $deliv_date
                        WHERE order_id = $order_id";

$sql = "UPDATE orders SET deliv_date = '$deliv_date'
                        WHERE order_id = $order_id";