简体   繁体   中英

How to use multiple update query using prepare statements in PHP for storing the values from an arrray?

How to use update query for multiple values in a single shot with prepare statement in a single shot. I have an array with name $popularpackages and i want to use prepare statement to update table records in a single shot

  $packageid= mysqli_real_escape_string($conn, $_POST['packageid']);
  $maxcount = mysqli_real_escape_string($conn, $_POST['loop']);
  for($loop = 0; $loop < $maxcount; $loop++){
           $packageid = $packageid[$loop];
   }
 $sql = $conn->prepare("update tbl_packagedetails set popularpackages =? where packageid =?;");
 $sql->bind_param("ii",popularpackages, $packageid);
 $sql->execute();

$popularpackages contains (id, value) as (1,0),(2,0),(3,1),....(10,0)

You'll have to loop it. But you should begin transaction before, run the loop and executing and only when the loop finishes you should commit. This way you will only have one transaction instead of multiple which is what I think you want to achieve.

$dbh->beginTransaction();

<loop while executing>

$dbh->commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM