我正在尝试查询所有数据,操纵其中两个字段以创建新值,然后使用此新值更新同一行中的现有字段(woocat)。它运行没有错误,但没有更新发生。我错过了什么?
# Get all data
$query = "SELECT * FROM cities";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($result))
{
$cityid = $row['id'];
$city = $row['city'];
$state_abr = $row['state_abr'];
$state = $row['state'];
$gstate = ucwords($state); // HELLO WORLD!
$gstate = ucwords(strtolower($gstate)); // Hello World!
$gcity = ucwords($city); // HELLO WORLD!
$gcity = ucwords(strtolower($gcity)); // Hello World!
$woocat = ("Towns>$gstate>$gcity");
// FIX THIS to make it work
$query = "UPDATE cities SET woo_cat = '$woocat' WHERE id = '$cityid'";
mysqli_query($con, $query);
}
mysqli_close($con);
答案 0 :(得分:1)
您正在修改循环内的循环变量。当您执行mysqli_query($con, $query);
时,您正在将$row = mysqli_fetch_assoc($result)
的值修改为同一循环中新查询的值,因此它不会起作用,您可以执行以下三项操作: