0

在这里,我组合了两个数组来更新 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 查询中的数组存在问题,但我不知道如何纠正它。

4

2 回答 2

0

That query can be written this way...

$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.

于 2013-05-13T07:56:26.970 回答
0

我认为您想翻转$e$fforeach()循环中$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'
于 2013-05-13T06:22:34.830 回答