PHP sql 查询无法替换更新所有行

问题描述 投票:0回答:1

尝试更新特定 state_id 的表行,插入有效,但出现错误,它没有使用正确的 city_name ....

这就是我们尝试过的方法,请记住我们仍在学习 PHP,因此我们将不胜感激

发生的情况是每一行都使用相同的城市名称而不是实际的城市名称?

$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]);
}

为什么不循环遍历每一个?

php mysqli pdo sql-update
1个回答
0
投票

感谢 @aynber 的一些指导,我们得到了它,我们使用了以下内容

``` $selectquery = "SELECT * FROM

cities
WHERE
state_id
= 9"; $stmt = $conn->准备($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]);
}

© www.soinside.com 2019 - 2024. All rights reserved.