PHP SQL更新数组 [英] PHP SQL Update array

查看:77
本文介绍了PHP SQL更新数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初对以下内容感到满意,为了将第1行和第2行更新为相同的值(status = 1)

  if($ _POST){
$ sql =UPDATE table SET status = 1,
WHERE id IN(1,2);;
db() - > query($ sql); (db() - > query($ sql)){
echo< b>良好< / b>的

;
}
else {
echo< b>没有好的< / b>;
}
}

但是现在我想用不同的值进行更新,即 - 行1到状态1,行2到状态2,行3到状态3.



关掉蝙蝠,我知道我需要
1。使用一个数组并循环三次。
2.将数组值传递给$ sql



我想这将是这样的,但我仍然在学习PHP。

  $ array_id = array(1,2,3); 
$ array_status = array(1,2,3);
$ b $ if($ _POST){
$ sql =UPDATE table SET status = $ array_status
WHERE id = $ array_id;;
db() - > query($ sql);

if(db() - > query($ sql)){
echo< b>更新成功< / b>;
}
else {
echo< b>更新不成功< / b>;
}
}

我会如何去做这件事情?如果您想使用INSERT语法更新多个行(在单个查询中),您可以这样做:


解决方案

b
$ b

  REPLACE table(id,status)VALUES(1,1),(2,2),(3,3)

请注意, id 必须是主键或唯一的,否则REPLACE会插入一个新行。
还要注意,REPLACE不是SQL标准,只能在MySQL中使用。


I originally was satisfied with the following in order to update row 1 and row 2 to the same value (status=1)

if ($_POST){
    $sql ="UPDATE table SET  status = 1,
            WHERE id IN (1,2 );";
    db()->query($sql);

    if(db()->query($sql)){
        echo "<b>Good</b>";
    } 
    else{
        echo "<b>No Good</b>";
    }
}

But now I want to update with different values, ie- row 1 to status 1, row 2 to status 2, and row 3 to status 3.

Off the bat, I know I need to 1. Use an array and loop through it three times. 2. pass in the array value into the $sql

I figure it would be something like this but I am still learning PHP..

$array_id = array(1, 2, 3);
$array_status = array(1, 2, 3);

if ($_POST){
    $sql ="UPDATE table SET  status = $array_status
            WHERE id = $array_id;";
    db()->query($sql);

    if(db()->query($sql)){
        echo "<b>Update Successful</b>";
    } 
    else{
        echo "<b>Update Unsuccessful</b>";
    }
}

How would I go about making this happen?

解决方案

If you want to update multiple rows (in single query) using the INSERT syntax, you can do this:

REPLACE table(id,status) VALUES(1,1),(2,2),(3,3)

Notice that id must be Primary Key or Unique, otherwise the REPLACE will insert a new row. Notice also that REPLACE isn't SQL standard, and works only in MySQL.

这篇关于PHP SQL更新数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆