I am trying to select all rows from a table, and update a column's values within the same table (basically an automatic slug/permalink rewrite of a title column). However, when I do that, it just inserts the same "slug/permalink" rewrite from the very first ID in all of the rows, instead of rewriting and inserting the title for each row individually.
<?php
$sql = "SELECT * FROM titles";
$query = mysqli_query($con, $sql) or die (mysqli_error());
while ($row = mysqli_fetch_array($query)) {
$id = $row["id"];
$title = $row["title"];
// title rewrite function goes here
$permalink = preg_replace ..... etc
//add new permalink rewrite to slug column
$sql2 = "UPDATE titles SET permalink='$permalink' WHERE id='$id'";
$query2 = mysqli_query($con, $sql2) or die (mysqli_error());
}
?>
Currently, the outcome is as follows:
ID Title Permalink
1 This is Title 1 this-is-title-1
2 This is Title 2 this-is-title-1
3 This is Title 3 this-is-title-1
I have also tried it without the WHERE id='$id' part, but that does not change anything ...
Any suggestions much appreciated ! Thanks