MySQL更新或插入或死亡查询 [英] MySQL update or insert or die query

查看:90
本文介绍了MySQL更新或插入或死亡查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行这样的操作是否有效,但我从来没有看到超过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屋!

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