我在sql中有一个表,该表有3个列,分别名为id
,title
和story_url
。
我正在尝试制作SEO friendly URL
。所以我想做的是使用title
和replace
white spaces
和special 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'";