while循环中下面的代码只有第一行是更新字段计数,而我想要完成所有行

时间:2017-01-24 20:11:56

标签: php mysql while-loop

<?php 
   if($action=='sefaresh'){
    $cookiname=$_COOKIE['mybasket'];
    $stmt = $db->prepare("SELECT `idsabad`,`cookiname`,`idmahsool`,`tedad`,`pardakht` FROM `sabad` WHERE `cookiname`=?");
    $stmt->bind_param("s", $cookiname);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($idsabad11, $cookiname1, $idmahsool1, $tedad1, $pardakht1);
    while ($stmt->fetch()) {
        $id = $idsabad11;
        $stmt = $db->prepare("UPDATE `sabad` SET `tedad`=? WHERE `idsabad`=?");
        $stmt->bind_param("ii", $_POST['tedad'.$idmahsool1], $id );
        $stmt->execute();
    }
}
?>

1 个答案:

答案 0 :(得分:1)

问题是由于语句对象$stmt。您正在覆盖$stmt循环中的外部语句对象while()以执行UPDATE操作,即$db->prepare("UPDATESET ... sabad while()将返回一个不同的语句对象,您将其分配给$stmt,从而覆盖原始/外部$stmt。这就是为什么一行得到更新的原因。

因此,请按以下方式更改while()循环,

while ($stmt->fetch()) {
    $id = $idsabad11;
    $stmt1 = $db->prepare("UPDATE `sabad` SET `tedad`=? WHERE `idsabad`=?");
    $stmt1->bind_param("ii", $_POST['tedad'.$idmahsool1], $id );
    $stmt1->execute();
    $stmt1->close();
}