简体   繁体   中英

update table column of every row in mysql

I have a table in sql which has 3 columns named id , title and story_url .

I am trying to make SEO friendly URL s. so what I am trying to do is using title and replace the white spaces and special chars with dash(-) and save it into other column story_url . story_url is empty atm.

Update all my table rows or insert that created url in story_url .

If I do it inside while loop then it only adds one last value in all rows.

function seoUrl($string) {

    $string = strtolower($string);
    $string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
    $string = preg_replace("/[\s-]+/", " ", $string);
    $string = preg_replace("/[\s_]/", "-", $string);
    return $string;
}

while($row = mysqli_fetch_array($run)) {
    $title = $row['title'];
    $story = $row['story'];
    $id = $row['id'];

    $url = seoUrl($title);
    $query = "UPDATE table SET story_url='$url'";
    mysqli_query($conn,$query);
}

Your UPDATE query needs to have WHERE clause.

Replace your query with below:

$query = "UPDATE table SET story_url='$url' WHERE id='$id'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM