MySQL更新或插入或死亡查询 [英] MySQL update or insert or die query
问题描述
执行这样的操作是否有效,但我从来没有看到超过1个或运算符:
Is it valid to do something like this, I never see more than 1 or operator:
$insert = 'INSERT into fhours (' .$cols . ') VALUES ('.$query.')';
$update = sprintf("UPDATE fhours SET %s WHERE fname='$fname' AND lname='$lname'", $field_list);
$result = $db->query($update) or $db->query($insert) or die('uhoh');`
推荐答案
这有两个问题.
首先,您可以使用参数化查询. 看看PDO,这将极大地帮助您.不仅对于多个插入来说这更快,而且您不必担心SQL注入那么多.
The first is that you can be using parameterized queries. Look at PDO, this will help you greatly. Not only is this faster for multiple inserts, but you don't have to worry about SQL injection so much.
第二个是您可以使用 MySQL的
The second is that you can use MySQL's ON DUPLICATE KEY UPDATE
to take care of this issue for you. Otherwise, when your query fails, you don't know why it failed. It may not have been a duplicate key issue at all!
除此之外,从or
的角度来看,代码还不错.
Other than that, the code from the standpoint of or
is just fine.
这篇关于MySQL更新或插入或死亡查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!