这里我组合了两个数组来更新Employee表中名为Age的列。但不幸的是,列的值没有得到更新。
下面的$names[]
和$ages[]
是两个数组。
foreach (array_combine($names, $ages) as $e => $f)
{
$sql = "UPDATE `Employee` SET `Age`= '" . $e . "' WHERE `A`= '" . $f . "'";
$query= mysql_query($sql);
}
如果我输入打印命令
echo $sql // its printing the correct command, like
UPDATE `Employee` SET `Age`= '41' WHERE `A`= '" . Samuel . "'
UPDATE `Employee` SET `Age`= '46' WHERE `A`= '" . Sonal . "'
我知道有一个问题与数组到更新sql查询,但我不知道如何纠正它。
这个查询可以这样写…
$sql = "
UPDATE Employee
SET Age
= $e WHERE A
= '$f';
";
Also, (and obviously), don't store someone's age. Store their DOB. And note that the mysql_ extension is now deprecated.
I think you want to flip $e
and $f
in you foreach()
loop $sql
-
foreach (array_combine($names, $ages) as $e => $f){
$sql = "UPDATE `Employee` SET `Age`= '" . $f . "' WHERE `A`= '" . $e . "'";
$query= mysql_query($sql);
}
这将给你
UPDATE `Employee` SET `Age`= '41' WHERE `A`= 'Samuel'
UPDATE `Employee` SET `Age`= '46' WHERE `A`= 'Sonal'
代替
UPDATE `Employee` SET `Age`= 'Samuel' WHERE `A`= '41'
UPDATE `Employee` SET `Age`= 'Sonal' WHERE `A`= '46'