提问人:Skipper 提问时间:11/17/2023 更新时间:11/17/2023 访问量:40
PHP sql查询无法替换更新所有行
PHP sql query not working to replace update all rows
问:
尝试更新特定state_id的表格行,插入有效,但出现错误,它没有使用正确的city_name....
所以这就是我们尝试过的,请记住我们仍在学习 PHP,因此任何帮助将不胜感激
它发生的情况是它对每一行都使用相同的city_name而不是实际的行?
$selectquery = "SELECT * FROM `cities` WHERE `state_id` = 9";
$stmt = $conn->prepare($selectquery);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$city_name = !empty($row['city_name']) ? $row['city_name'] : '';
$new_key = "".$city_name." Businesses, ".$city_name." Business, ".$city_name." Tourism, .....";
$new_desc = "Find the best ".$city_name." Businesses. Find the best ".$city_name." Restaurants, ....";
$st3 = $pdo->prepare('UPDATE cities SET meta_desc = :desc, meta_key = :key WHERE state_id = 9');
$st3->execute([':desc' => $new_desc, ':key' => $new_key]);
}
为什么不循环遍历每一个?
答:
3赞
Skipper
11/17/2023
#1
多亏了@aynber的一些指导,我们得到了它,我们使用了以下内容
$selectquery = "SELECT * FROM `cities` WHERE `state_id` = 9";
$stmt = $conn->prepare($selectquery);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$city_name = !empty($row['city_name']) ? $row['city_name'] : '';
$city_id = !empty($row['city_id']) ? $row['city_id'] : '';
$new_key = "".$city_name." Businesses, ".$city_name." Business, ".$city_name." Tourism, P...";
$new_desc = "Find the best ".$city_name." Businesses. Find the best ".$city_name." Restaurants, ...";
$st3 = $pdo->prepare('UPDATE cities SET meta_desc = :desc, meta_key = :key WHERE city_id = :id AND `state_id` = 9');
$st3->execute([':desc' => $new_desc, ':key' => $new_key, ':id' => $city_id]);
}
我给你竖起大拇指,但不知道怎么做?
评论
0赞
ADyson
11/17/2023
仅供参考,可能只是......等。将空字符串放在文本的开头不会做任何事情(因为它是空的!$new_key = "".$city_name
$new_key = $city_name
评论
WHERE id = :city_id
state_id