Mysqli INSERT 命令后跟 UPDATE

Mysqli INSERT command followed by an UPDATE

我想在一个 table 中插入数据,并通过 mysqli 中的准备语句在另一个中更新数据。尝试以下操作只会执行 INSERT 命令:

已编辑:

if($stmt=$mysqli->prepare("SELECT bids_id, bid, fruit_volume FROM basket ORDER BY bid DESC")) {
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($bids_id, $bid, $fruit_volume);                      
    while($stmt->fetch()) { 
        $stack = array($bids_id, $bid, $fruit_volume);
        array_push($all_fruits, $stack);    
    } 
    $stmt->free_result();
}
foreach ($all_fruits as $fruits) {
   if ($_POST["offer"] == $fruits[1] && $volume < $fruits[2]) {
        $stmt2 = $mysqli->prepare("INSERT INTO oranges (username, price, volume, date) VALUES (?, ?, ?, ?)");
        $stmt2->bind_param('sdis', $user, $price, $volume, $today); 
        $stmt2->execute();

        $stmt3 = $mysqli->prepare("UPDATE basket SET fruit_volume = ? WHERE bids_id = ?");
        $stmt3->bind_param('ii', 800, 1);
        $stmt3->execute();  

    }
}
$mysqli->close();

试试这个

foreach ($all_fruits as $fruits) {
   if ($_POST["offer"] == $fruits[1] && $volume < $fruits[2]) {
        $stmt2 = $mysqli->prepare("INSERT INTO oranges (username, price, volume, date) VALUES (?, ?, ?, ?)");
        $stmt2->bind_param('sdis', $user, $price, $volume, $today); 
        $stmt2->execute();


        $stmt3 = $mysqli->prepare("UPDATE basket SET fruit_volume = ? WHERE bids_id = ?");
        $stmt3->bind_param('ii', 800, 1);
        $stmt3->execute();            
    }
}
$mysqli->close();

bind_param 通过引用而不是值传递,因此您需要在变量中包含这些值才能被引用

$a=800;
$b=1;

foreach ($all_fruits as $fruits) {
   if ($_POST["offer"] == $fruits[1] && $volume < $fruits[2]) {
        $stmt2 = $mysqli->prepare("INSERT INTO oranges (username, price, volume, date) VALUES (?, ?, ?, ?)");
        $stmt2->bind_param('sdis', $user, $price, $volume, $today); 
        $stmt2->execute();

        $stmt3 = $mysqli->prepare("UPDATE basket SET fruit_volume = ? WHERE bids_id = ?");
        $stmt3->bind_param('ii',$a, $b);
        $stmt3->execute();  

    }
}