php 更新 MySQL 中的整行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10575776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:30:01  来源:igfitidea点击:

Update an entire row in MySQL

phpmysql

提问by grasshopper

I'm new to MySQL and I want to update an entire row in MySQL with a new array, so far all the examples of the Update query involves specifying the column name and a new value for that column, like this:

我是 MySQL 的新手,我想用新数组更新 MySQL 中的整行,到目前为止,更新查询的所有示例都涉及为该列指定列名和新值,如下所示:

"UPDATE tablename SET columnname = '".$new_value."' WHERE columnname = '".$value."'";

How can I update an entire record with the update query or should I use the replace query?

如何使用更新查询更新整个记录,还是应该使用替换查询?

Any advice will be appreciated.

任何建议将被认真考虑。

Edit: Is there a query which doesn't require to specify all the column names and new column values?

编辑:是否有不需要指定所有列名和新列值的查询?

Basically I want to have a query that looks something like this:

基本上我想要一个看起来像这样的查询:

Update entirerow with thisarray where primarykeycolumn='thisvalue'

使用 thisarray 更新整个行,其中 primarykeycolumn='thisvalue'

回答by zerkms

To do that you need to

要做到这一点,你需要

  1. Enumerate all the values
  2. Know the primary key column and value
  1. 枚举所有值
  2. 知道主键列和值

So the final query would look like

所以最终的查询看起来像

UPDATE tablename
   SET col1 = 'val1', col2 = 'val2' ...
 WHERE id = id_value

There is no any magic command for updating the "whole row" in sql other than I shown above. And REPLACEis definitely not what you need here.

除了上面显示的之外,没有任何魔法命令可以更新 sql 中的“整行”。而且REPLACE绝对不是您在这里需要的。

回答by AlienWebguy

It depends if you want to keep the ID or not, assuming the ID is autoincrement.

这取决于您是否要保留 ID,假设 ID 是autoincrement.

REPLACE INTO mytable VALUES( new array ) ....will update the ID as well, since it really just emulates a DELETEand INSERT.

REPLACE INTO mytable VALUES( new array ) ....也会更新 ID,因为它实际上只是模拟 aDELETEINSERT

If you want to keep the ID, use an UPDATE mytable SET foo='bar', baz='bat' WHERE id=12

如果要保留 ID,请使用 UPDATE mytable SET foo='bar', baz='bat' WHERE id=12

As an FYI, REPLACEis generally convenient for mapping tables where the unique field or composite primary key isn't an autoincrement.

作为仅供参考,REPLACE对于唯一字段或复合主键不是自动增量的映射表通常很方便。

回答by Benjamin Cox

You can certainly do it all in one query. You just add more this = that clauses separated by commas:

您当然可以在一个查询中完成所有操作。您只需添加更多以逗号分隔的 this = that 子句:

"UPDATE tablename 
SET column1name = '".$new_value1."', 
    column2name = '".$new_value2."', 
    column3name = '".$new_value3."' 
WHERE columnname = '".$value."'"

回答by LJ Wilson

That is the correct way.

那是正确的方法。

UPDATE TABLENAME SET COLUMNAME = VALUE, COLUMN2NAME = VALUE, ETC WHERE CONDITION

回答by user2333811

Yes there is a similar way...

是的,有一种类似的方式......

$updateSQL = sprintf("UPDATE hotel <br>SET hotel_name=%s, contact_person_1=%s <br>WHERE hotel_id=%s",<br>
                       $_POST['hotel_name'],<br>
                       $_POST['contact_person_1'],<br>
                       $_POST['hotel_id']);

mysql_select_db($database_hotelbookingryan, $hotelbookingryan);
$Result1 = mysql_query($updateSQL, $hotelbookingryan) or die(mysql_error());