如何使用PHP中的prepare语句使用多个更新查询来存储错误的值? [英] How to use multiple update query using prepare statements in PHP for storing the values from an arrray?

查看:53
本文介绍了如何使用PHP中的prepare语句使用多个更新查询来存储错误的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在单次使用带有prepare语句的单次使用多个值的更新查询.我有一个名为$ popularpackages的数组,我想使用prepare语句一次性更新表记录

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包含(id,value)为(1,0),(2,0),(3,1),....(10,0)

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

这篇关于如何使用PHP中的prepare语句使用多个更新查询来存储错误的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆