PHP->sql UPDATE 行,其中来自同一个表中的其他 2 行的数据合并在一起

发布于 2024-11-18 14:49:23 字数 429 浏览 1 评论 0原文

桌子=位置 字段 = 地址、街道、郊区

地址字段最初为空,街道和郊区已填充。我尝试使用街道和郊区数据更新地址字段,但它不起作用。只允许我更新一个字段。使用下面的代码,地址字段将更新为 0...但是,如果我从更新中删除其中一个变量,那么它将仅使用街道或郊区正确更新。

我这样做的原因是因为向我提供数据的方式是这种格式,并且我手动执行此操作需要很长时间。

while ($row=$result->fetch_assoc()) {

    $street=$row['street'];
        $suburb=$row['suburb'];

        $mysqli->query("UPDATE address SET address = $street+$suburb");
        echo($street);
}
}

Table = location
Fields = address, street, suburb

address fields are initially empty, street and suburb are populated. I have tried to update the address field with the street and suburb data but its not working. only allows me to update with one field. With the code below the address field would be updated with 0's... But if i remove one of the variables from the update then it will update correctly with just either the street or suburb.

The reason why im doing this is because the way the data is given to me is in that format and would take too long for me to manually do it.

while ($row=$result->fetch_assoc()) {

    $street=$row['street'];
        $suburb=$row['suburb'];

        $mysqli->query("UPDATE address SET address = $street+$suburb");
        echo($street);
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风启觞 2024-11-25 14:49:23

不要忘记在 UPDATE 查询中放置 WHERE 子句,除非您希望整个表具有相同的地址列。试试这个

while ($row=$result->fetch_assoc()) {

        $street=$row['street'];
        $suburb=$row['suburb'];
        $address=$street." ".$suburb;
        $id=$row['id'];

        $mysqli->query("UPDATE address SET address = `".address."` WHERE id=".$id);

}

Don't forget to put a WHERE clause in your UPDATE query, unless you want your whole table to have the same adress column. Try this

while ($row=$result->fetch_assoc()) {

        $street=$row['street'];
        $suburb=$row['suburb'];
        $address=$street." ".$suburb;
        $id=$row['id'];

        $mysqli->query("UPDATE address SET address = `".address."` WHERE id=".$id);

}
熊抱啵儿 2024-11-25 14:49:23

Try

 $mysqli->query("UPDATE address SET address ='". $street." ".$suburb."' WHERE street = '$street' AND subrub = '$subrub'");

+ 不是 php 中的连接运算符。因此,请尝试使用 .

另外,如果您没有 WHERE 子句,那么所有行都会更新!

Try

 $mysqli->query("UPDATE address SET address ='". $street." ".$suburb."' WHERE street = '$street' AND subrub = '$subrub'");

+ is not a concatenation operator in php. So try using .

Also, if you dont have the WHERE clause, then all the rows will get updated!!!

獨角戲 2024-11-25 14:49:23

尝试使用:

while ($row=$result->fetch_assoc()) {

    $street=$row['street'];
        $suburb=$row['suburb'];
    $address = $street.$suburb;

        $mysqli->query("UPDATE address SET address = '$address'");
        echo($street);
}
}

Try with:

while ($row=$result->fetch_assoc()) {

    $street=$row['street'];
        $suburb=$row['suburb'];
    $address = $street.$suburb;

        $mysqli->query("UPDATE address SET address = '$address'");
        echo($street);
}
}
岁吢 2024-11-25 14:49:23

您不必将结果获取到 PHP,然后对每一行运行 UPDATE。您现在似乎正在向数据库发送一个 SELECT大量(数千或数百万,取决于表的大小)UPDATE 语句。

您可以使用一个 UPDATE 语句更新整个表(所有行!):

"UPDATE location SET address = CONCAT(street, ' ', suburb) ;" 

如果(稍后)您的某些行已经在 address 列,您只能更新地址为空的行:

"UPDATE location
 SET address = CONCAT(street, ' ', suburb) 
 WHERE address = ''
    OR address IS NULL ;"  

您还应该检查 address 字段的大小是否大于或至少等于 CHAR_LENGTH(street) + CHAR_LENGTH(郊区)+ 1

You don't have to fetch results to PHP and then run an UPDATE for every row. It seems you are now sending one SELECT and numerous (thousands or millions, depends on the size of your table) of UPDATE statements to the database.

You can update the whole table (all rows!) with one UPDATE statement:

"UPDATE location SET address = CONCAT(street, ' ', suburb) ;" 

If (later on) some of your rows have already data in the address column, you can update only rows with empty address:

"UPDATE location
 SET address = CONCAT(street, ' ', suburb) 
 WHERE address = ''
    OR address IS NULL ;"  

You should also check that the size of address field is bigger or at least equal to the maximum of CHAR_LENGTH(street) + CHAR_LENGTH(suburb) + 1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文