简体   繁体   English

更新mysql中每一行的表列

[英]update table column of every row in mysql

I have a table in sql which has 3 columns named id , title and story_url . 我在sql中有一个表,该表有3列,分别名为idtitlestory_url

I am trying to make SEO friendly URL s. 我试图使SEO friendly URL 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 . 所以我想做的是使用title并将white spacesspecial chars replace为dash(-)并将其保存到其他column story_url story_url is empty atm. story_url是空的atm。

Update all my table rows or insert that created url in story_url . 更新我所有的表行,或将创建的url插入story_url

If I do it inside while loop then it only adds one last value in all rows. 如果我在while循环中执行此操作,则它只会在所有行中添加一个最后一个值。

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. 您的UPDATE查询需要具有WHERE子句。

Replace your query with below: 将查询替换为以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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