dongrouli2667 2016-01-20 00:49
浏览 54
已采纳

PHP MYSQL错误1064 [关闭]

I can't tell what is wrong with this because I've used similar queries the same way and there isn't an issue. It is giving me the following error:

Multi query failed: (1064) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UPDATE stock SET quantity='160' WHERE id='2'' at line 1

If someone could take a look and see if i'm missing something:

$root = $_SERVER['DOCUMENT_ROOT']."/wfo-inv";

require ($root.'/assets/config.php');

$id = $_GET['id'];

$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security
$name = mysqli_real_escape_string($link, $_POST['name']);
$type = mysqli_real_escape_string($link, $_POST['type']);
$quantity = mysqli_real_escape_string($link, $_POST['quantity']);
$quantitysold = mysqli_real_escape_string($link, $_POST['quantitysold']);
$price = mysqli_real_escape_string($link, $_POST['price']);
$location = mysqli_real_escape_string($link, $_POST['location']);
$batch = mysqli_real_escape_string($link, $_POST['batch']);
$thc = mysqli_real_escape_string($link, $_POST['thc']);
$cbd = mysqli_real_escape_string($link, $_POST['cbd']);

$quantityfinal = $quantity - $quantitysold;

if($quantityfinal >= "1") {
        $sql = "INSERT INTO outgoing (name, type, quantity, price, location, batch, thc, cbd) VALUES ('$name', '$type', '$quantitysold', '$price', '$location', '$batch', '$thc', '$cbd')";
        $sql .= "UPDATE stock SET quantity='$quantityfinal' WHERE id='$id'";
} else {
        $sql = "INSERT INTO outgoing (name, type, quantity, price, location, batch, thc, cbd) VALUES ('$name', '$type', '$quantitysold', '$price', '$location', '$batch', '$thc', '$cbd')";
        $sql .= "DELETE FROM stock WHERE id='$id'";
}

if (!$link->multi_query($sql)) {
    echo "Multi query failed: (" . $link->errno . ") " . $link->error;
}

do {
    if ($res = $link->store_result()) {
        var_dump($res->fetch_all(MYSQLI_ASSOC));
        $res->free();
        header("Location: ../index.php");
    }
} while ($link->more_results() && $link->next_result());



// close connection
mysqli_close($link);
  • 写回答

2条回答 默认 最新

  • doupian9490 2016-01-20 00:59
    关注

    According to the docs, the queries passed to mysqli::multiquery() need to be concatenated with a semicolon, something like

    $sql = "INSERT INTO outgoing (name, type, quantity, price, location, batch, thc, cbd) VALUES ('$name', '$type', '$quantitysold', '$price', '$location', '$batch', '$thc', '$cbd')";
    $sql .= "; "; // <- Add this line here
    $sql .= "UPDATE stock SET quantity='$quantityfinal' WHERE id='$id'";
    

    (It doesn't have to be on a separate line, of course; I just did that to make it obvious.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?