PHP MYSQL 错误 1064

PHP MYSQL Error 1064

我不知道这有什么问题,因为我以同样的方式使用过类似的查询,没有问题。它给我以下错误:

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

如果有人可以看看我是否遗漏了什么:

$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);

将该行中两个变量周围的撇号替换为左花括号和右花括号,这样应该可以修复该行。

"update table set column={$var} where column={$othervar}"

According to the docs, 传递给 mysqli::multiquery() 的查询需要用分号连接,例如

$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'";

(当然,它不必在单独的行上;我这样做只是为了让它显而易见。)