Search Posts

How to select all rows, and update field in row within the same while loop?

how to – How to select all rows, and update field in row within the same while loop?

I’m trying to query all data, manipulate 2 of the fields to create a new value, and then update an existing field (woocat) within the same row with this new value. It runs without error, but no updating happens. What am I missing?

# 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);

Source: Stackoverflow

Leave a Reply

Your email address will not be published. Required fields are marked *