我在sql中有一个表,该表有3个列,分别名为idtitlestory_url

我正在尝试制作SEO friendly URL。所以我想做的是使用titlereplace white spacesspecial chars与dash(-)并将其保存到其他列story_url中。 story_url是空的atm。

更新我所有的表行或在story_url中插入创建的URL。

如果我在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);
}

最佳答案

您的UPDATE查询需要具有WHERE子句。

将查询替换为以下内容:

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

09-30 17:06
查看更多