$ _POST数据如下所示:
(
[data] => ["1","2","3","4"]
)
,解码后的数组如下所示:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
其中每个值表示我想在MySQL表中更新的行的ID。
这是我的PHP脚本:
<?php
//Report all PHP errors
error_reporting(-1);
$username="username";
$password="password";
$database="tgpgigs";
//Connect to database
$conn = new mysqli(localhost, $username, $password, $database);
//Check connection
if ($conn->connect_error) {
die("Failed to connect to MySQL: " . $conn->connect_error);
} else {
echo "Connected to db";
}
//OUTPUT POST DATA
$array = $_POST['data'];
//CHECK DATA
$deData = json_decode($array);
print_r($deData);
//UPDATE MySQL
foreach($deData as $key => $value) {
echo $value;
$upSQL = "UPDATE gigdata SET archived='1' WHERE id='".$value."'";
}
目前,foreach循环正确地回显每个$值,但是在数据库中更新它们没有乐趣。感谢您提前提供任何帮助。
答案 0 :(得分:0)
改变你的foreach周期:
foreach($deData as $key => $value) {
echo $value;
$upSQL = "UPDATE gigdata SET archived='1' WHERE id='".$value."'";
$conn->query($upSQL);
}
我希望这对你有所帮助。