我有4列。我试图只更新与值相等(等于)的列的值进入循环。
for ($a=0; $a<$i ; $a++) {
$sql= "update table1 set
col1= CASE when col1 like '$distval[$a]' then col1='$arr[$a]' else col1 end,
col2=CASE when col2 like '$distval[$a]' then col2='$arr[$a]' else col2 end,
col3=CASE when col3 like '$distval[$a]' then col3='$arr[$a]' else col3 end,
col4=CASE when col4 like '$distval[$a]' then col4='$arr[$a]' else col4 end";
mysqli_query($conn, $sql);
}
让我解释一下代码:
$ distval:是包含我想要用用户输入的值更新的所有四列的不同值的数组。
$ arr:是与'$ distval'相同数字的另一个文本字段数组。现在,在第一个文本字段中输入的值应更新表中的所有值,如(等于)'$ distvalue'的第一个值。
代码不会在没有任何错误的情况下更新任何内容。
答案 0 :(得分:1)
删除colX ='$ arr [$ a]'然后你只需'$ arr [$ a]'
$sql= "update table1 set
col1= CASE when col1 like '%". $distval[$a]. "%' then '$arr[$a]' else col1 end,
col2= CASE when col2 like '%". $distval[$a]. "%' then '$arr[$a]' else col2 end,
col3= CASE when col3 like '%". $distval[$a]. "%' then '$arr[$a]' else col3 end,
col4= CASE when col4 like '%". $distval[$a]. "%' then '$arr[$a]' else col4 end";
答案 1 :(得分:0)
=
对then
没有意义。正确的case
看起来像是:
update table1
set col1 = (CASE when col1 like '$distval[$a]' then '$arr[$a]' else col1 end),
col2 = (CASE when col2 like '$distval[$a]' then '$arr[$a]' else col2 end),
col3 = (CASE when col3 like '$distval[$a]' then '$arr[$a]' else col3 end),
col4 = (CASE when col4 like '$distval[$a]' then '$arr[$a]' else col4 end);
我强烈建议您使用参数化查询,而不是直接将值转储到查询字符串中。