我遇到了一个问题,尝试从XML提要中获取数据并使用新数据更新数据库。
我设法获取数据并将其放入数组,但是由于foreach()而失败,请参见下文。我花了很多时间,无法解决。
我从答案Simple UPDATE MySQl table from php array中获取了SQL UPDATE代码
<?php
$array = "Array ( [0] => Array ( [name] => PARNELL [spaces] => 123 ) [1] => Array ( [name] => ILAC [spaces] => 884 ) [2] => Array ( [name] => JERVIS [spaces] => 844 ) [3] => Array ( [name] => ARNOTTS [spaces] => 352 ) )";
$link = mysqli_connect("localhost", "root", "", "database");
/* check connection */
if (mysqli_connect_error()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
foreach ($array as $key => $carparks) {
$name = mysql_real_escape_string($carparks->name);
$spaces = mysql_real_escape_string($carparks->spaces);
$id = intval($carparks->id);
$sql = "UPDATE data SET name='$name', spaces='$spaces' date='NOW()' WHERE name=$name" ;
$result = mysql_query($sql);
if ($result === FALSE) {
die(mysql_error());
}
}
print_r($array);
mysqli_close($link);
?>
任何人都可以提供我在哪里出错的任何见识。