简体   繁体   English

通过PHP更新多个MYSQL行

[英]Updating multiple MYSQL rows via PHP

I am fairly new to PHP, I know the basics but not sure how to update multiple rows at once in PHP? 我对PHP相当陌生,我了解基础知识,但不确定如何在PHP中一次更新多行吗? form is similar to this: 形式与此类似:

<form action="sortorder.php" id="sortorder">

<input style='width:20px;' type='text' name='photo' value='1'>
<input style='width:20px;' type='text' name='sortorder' value='1'>

<input style='width:20px;' type='text' name='photo' value='2'>
<input style='width:20px;' type='text' name='sortorder' value='2'>

<input style='width:20px;' type='text' name='photo' value='3'>       
<input style='width:20px;' type='text' name='sortorder' value='3'>

</form>

I need to apply similar code to the following to each 我需要将类似的代码应用于以下内容

UPDATE photos SET sortorder = <input> WHERE id = '1';
UPDATE photos SET sortorder = <input> WHERE id = '2';      

I just have no idea how to take multiple bits of dynamic data and update them all. 我只是不知道如何获取多个动态数据并更新它们。 Can someone point me in the right direction? 有人可以指出我正确的方向吗? I realise I need to put them into an array, but I just don't know how. 我意识到我需要将它们放入一个数组,但是我只是不知道如何。

Thank you in advance. 先感谢您。

Let's say you have the Ids 1,2,3,4. 假设您有ID 1,2,3,4。 You could do something like this: 您可以执行以下操作:

UPDATE photos SET sortorder = <yourvalue> WHERE id IN (1,2,3,4);

Now instead of explicitly writing IN (1,2,3,4) you can dynamically generate the values you want to be updated. 现在,无需显式编写IN (1,2,3,4)您就可以动态生成要更新的值。 That could be through PHP or via SQL: 可以通过PHP或SQL:

UPDATE photos SET sortorder = <yourvalue> 
WHERE id IN 
(SELECT id from anotherTable WHERE otherattribute = someotherValue);

You should note that only text must be in ' - numbers must be written without! 您应该注意,只有文本必须以' -不能写数字! For your example I assumed that id is a number and not a text - if it is indeed a text (or char/varchar), you need to write ... IN ('1','2','3','4') . 对于您的示例,我假设id是一个数字而不是文本-如果确实是文本(或char / varchar),则需要编写... IN ('1','2','3','4') But I would advise you to use integer for your ids. 但我建议您使用整数作为ID。

Update: To get the specific values of your inputs, you can do the following: 更新:要获取输入的特定值,可以执行以下操作:

<input ... name='photo[]' value='1' />
<input ... name='sortorder[]' value='1' />

This way you will get two arrays when you send your form: $_POST[photo] and $_POST[sortorder] . 这样,您在发送表单时将获得两个数组: $_POST[photo]$_POST[sortorder]

Now iterate through both of them (sorry if the syntax isn't correct, didn't use PHP for a long time, but the idea should be clear): 现在遍历这两个代码(很抱歉,如果语法不正确,很长时间没有使用PHP,但是想法应该很清楚):

for($i = 0; $i < photo.length; $i++){
    // your query here
    // something like:
    UPDATE photos SET sortorder = sortorder[i] WHERE id = photo[i];
    // of course you have to sanitize your input!
}

As I said: I'm not sure if that's exactly how you would write this in PHP, but the idea is to go through the arrays and take those values into your query. 就像我说的那样:我不确定这是否就是用PHP编写的方式,但是这个想法是遍历数组并将这些值带入查询中。

As per your query 根据您的查询

UPDATE photos SET sortorder = WHERE id = '1';
UPDATE photos SET sortorder = WHERE id = '2';

id column is varchar and not int and also you have not specified what to store in sortorder. id列是varchar而不是int,并且您还没有指定要按排序顺序存储的内容。

I am giving solution considering following queries 我正在考虑以下查询的解决方案

UPDATE photos SET sortorder = '1' WHERE id = '1';
UPDATE photos SET sortorder = '2' WHERE id = '2';

in that case you should query like : 在这种情况下,您应该查询如下:

UPDATE photos SET sortorder = id;

if sortorder is int and id is varchar then query should be 如果sortorder为int且id为varchar,则查询应为

UPDATE photos SET sortorder = CAST(id as unsigned);

and if you want to update for some specific values only then it should be like 如果您只想更新某些特定值,则应该像

UPDATE photos SET sortorder = CAST(id as unsigned) where id in ('1','2','3');

It seams you wanna make the insert sql more effective. 它表明您想使插入sql更有效。 But actualy, update one row by id per time will shorten the lock time and helps improving concurrence ability. 但是实际上,每次按id更新一行将缩短锁定时间并有助于提高并发能力。 On the other hand, there is no way to update different rows by different conditions in one sql. 另一方面,无法通过一个sql在不同条件下更新不同的行。

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

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