gpt4 book ai didi

php - 更新mysql中每一行的表列

转载 作者:行者123 更新时间:2023-11-29 19:11:50 25 4
gpt4 key购买 nike

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

我正在尝试制作 SEO 友好的 URL。所以我想做的是使用 title 并用 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'";

关于php - 更新mysql中每一行的表列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43021811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com