我想从array更新查询。问题是当我运行代码时,数据库中的更新数据只是第一个数据..
例如..
数组由整数类型= [1,2]
组成当我运行代码时,数据库中的数据只运行了第一个数据(只有1个)..我该如何解决这个问题?
PHP文件
$query_edit_paket = "select nama_menu from menu where id_menu = 149 ";
$query_exec_edit_paket = mysql_query($query_edit_paket) or die(mysql_error());
$nama_menu_query = reset(mysql_fetch_assoc($query_exec_edit_paket));
$_fbexclude = mysql_query("select id_paket from detail_paket where menu_paket = '$nama_menu_query' ");
$fbexcludearray = array();
while ($row = mysql_fetch_assoc($_fbexclude)) {
$fbexcludearray[] = $row['id_paket'];
}
$excludes = implode(',', $fbexcludearray);
echo $excludes;
for($i=0;$i<count($excludes);$i++)
{
if($excludes != "")
{
$strSQL = "update paket set ketersediaan_paket ='kosong' ";
$strSQL .="WHERE id_paket = '$excludes' ";
$objQuery = mysql_query($strSQL);
}
}
谢谢!
答案 0 :(得分:1)
尝试使用IN
变化
$strSQL .="WHERE id_paket = '$excludes' ";
到
$foo = '';
for($i=0; $i < count($fbexcludearray); $i++){
$foo.= $i == 0? $fbexcludearray[$i]: ','.$fbexcludearray[$i];
}
$strSQL .="WHERE id_paket IN ($foo) ";
答案 1 :(得分:1)
如果您要打印$ strSQL,您会看到您的查询将是这样的
update paket set ketersediaan_paket ='kosong' WHERE id_paket = '1,2'
这不是您想要的查询。你想要的更像是这样的
update paket set ketersediaan_paket ='kosong' WHERE id_paket = '1' OR WHERE id_paket = '2'
因此,您需要重新构建for循环或更改查询。