由于唯一约束,PHP MySQL INSERT失败 [英] PHP MySQL INSERT fails due to unique constraint

查看:130
本文介绍了由于唯一约束,PHP MySQL INSERT失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在插入时,我捕获到唯一约束mysql_errno()1062.

On insert I am catching the unique constraint mysql_errno() 1062.

这很好,但是我想找到现有的行以恢复或修改它.

This works fine but I want to find the existing row to re-instate or modify it.

是否存在在插入失败时获取行ID的方法?我尝试了mysql_insert_id(),但意识到那只会返回我要插入(或插入失败)的行,因此,我得到0.

Is there are method to obtain the row id on insert fail? I tried mysql_insert_id() but realised that would only return the row I'm inserting (or failed to insert) therefore, I get 0.

除了发出另一个mysql_query并仅对重复值执行选择之外,别无选择吗?

Is there no option but to issue another mysql_query and simply perform a select on the duplicate value?

我只想确保没有更好,更快,更经济的方式来做到这一点.

I just want to make sure there is no better, quicker, more economical way to do this.

推荐答案

如果您尝试在添加新值或更新现有值时插入一行,则在重复键更新中插入...语法如果涉及到约束,则REPLACE INTO将先删除,然后再插入.

If you are attempting to insert a row if new or update existing values then REPLACE INTO is what you need. Also consider INSERT ... ON DUPLICATE KEY UPDATE Syntax if there are constraints involved as REPLACE INTO will DELETE and then INSERT.

这篇关于由于唯一约束,PHP MySQL INSERT失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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