简体   繁体   English

MySQLi 查询循环遍历数组并更新多行

[英]MySQLi query to loop through array and update multiple rows

I have an array like:我有一个数组,如:

$postdata[1] = 'This';
$postdata[2] = 'That';
$postdata[3] = 'The other';

And I want to loop through the array and update all of the rows where ID corresponds to the array key.我想遍历数组并更新ID对应于数组键的所有行。 Like:喜欢:

foreach ($postdata as $key => $value) {
  if ($key == 1) {
    $update = $db->query("UPDATE site_email_templates SET Content='$postdata[1]' WHERE ID = 1");
  } else if ($key == 2) {
    $update = $db->query("UPDATE site_email_templates SET Content='$postdata[2]' WHERE ID = 2");
  } else if ($key == 3) {
    $update = $db->query("UPDATE site_email_templates SET Content='$postdata[3]' WHERE ID = 3");
  }
}

What would be the simplest way to do this, not particularly knowing how many array keys there are, and keeping it all in one query?执行此操作的最简单方法是什么,而不是特别知道有多少个数组键,并将其全部保存在一个查询中?

You need to use prepared statements in order to avoid errors and vulnerabilities of all sorts and also to get some minor performance gain您需要使用准备好的语句以避免各种错误和漏洞,并获得一些小的性能提升

$stmt = $db->prepare("UPDATE site_email_templates SET Content=? WHERE ID = ?");
$stmt->bind_param("ss", $content, $id);
foreach ($postdata as $id => $content)
{
    $stmt->execute();
}

Reference: How can I prevent SQL injection in PHP?参考: 如何防止 PHP 中的 SQL 注入?

Note: My answer is based on the PDO driver which in many aspects is better than mysqli.注意:我的回答是基于 PDO 驱动程序,它在许多方面都优于 mysqli。 If you need mysqli solution please check the other answer provided by @Your Common Sense如果您需要 mysqli 解决方案,请查看@Your Common Sense 提供的其他答案

The code below is tested on real environment and served with prepared statement preventing SQL-injection:下面的代码在真实环境中进行了测试,并与防止 SQL 注入的准备语句一起提供:

$sql = "UPDATE `site_email_templates` SET `Content` = (:content) WHERE `Id` = (:id)";
$stmt = $dbConn->prepare($sql);
foreach ($postdata as $id => $content)
{
    $stmt->execute([':id' => $id, ':content' => $content]);
}

For more details about SQL injection you can read more:有关 SQL 注入的更多详细信息,您可以阅读更多内容:
https://www.owasp.org/index.php/SQL_Injection https://www.owasp.org/index.php/SQL_Injection

You can use the variable $key as ID : 您可以使用变量$key作为ID

foreach ($postdata as $key => $value) {
    $update = $db->query("UPDATE site_email_templates SET Content='".$value."' WHERE ID = ".$key);
}
foreach ($postdata as $key => $value) {
  if(!empty($postdata[$key])){
      $update = $db->query("UPDATE site_email_templates SET Content='".$value."' WHERE ID = ".$key);
  }
}

use for loops for循环

$count = count($postdata);

for($i=1;$i<=$count; $i++)
{
    $data = '$postdata'.$i;
    $update = $db->query("UPDATE site_email_templates SET Content='$data' WHERE ID = 1");
}

This should work: 这应该工作:

foreach ($postdata as $key => $value) {
    $update = $db->query("UPDATE site_email_templates SET Content='".$value."' WHERE ID =".$key);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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