gpt4 book ai didi

php - 使用 for 循环和数组 php 更新多行

转载 作者:行者123 更新时间:2023-11-29 00:19:35 26 4
gpt4 key购买 nike

我有这个代码:

$id1=array($aa1,$aa2,$aa3,$aa4,$aa5,$aa6,$aa7);
$rank1=array($a1,$a4,$a7,$a10,$a13,$a16,$a19);
require_once("connection.php");
for($i=0;$i<7;$i++){
$sql = "update live_tracking set swim_rank = '"$rank1[$i]."' where id = '".$id1[$i]."'";
}

我想更新我的 mysql 数据库的多行,但是使用上面的代码只更新了第一个索引。有什么建议吗?

最佳答案

你只能使用这个来执行一个查询:

$sql = "update live_tracking set swim_rank = '".$rank1[$i]."' WHERE id IN (";
for($i=0;$i<7;$i++){
$sql .= $id1[$i].",";
}

$sql .= ")";

$query = mysql_query($sql) or die("error : ".mysql_error());

if($query){
echo "success";
}

关于php - 使用 for 循环和数组 php 更新多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21572989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com