[英]MYSQL php Select if not exists doesnt work
When my DB holds the data already it should echo "fail" 当我的数据库已经保存数据时,它应该回显“失败”
my Code is : 我的代码是:
if (!mysqli_query($con,"INSERT IGNORE INTO name(username, passwort, email, active, loc, image)
VALUES ('$name', '$pass','$mail' , 0, '$loc', 'mo.png')"))
{
echo "fail";
}else{
echo "sucess";
}
the weird thing is if my Username ecist it do what it should do, no insert. 奇怪的是,如果我的用户名ecist做了它应该做的事,没有插入。 But my echo "fail" is not called, but why?
但是我的回声“失败”没有被调用,但是为什么呢?
Very, very simply: "insert ignore" doesn't generate an error - so your "fail" never occurs. 非常非常简单:“插入忽略”不会产生错误-因此您的“失败”永远不会发生。
Suggestion: one alternative might be to try "ON DUPLICATE KEY UPDATE". 建议:一种替代方法是尝试“在重复的密钥更新上”。
Here is the man page: 这是手册页:
http://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html http://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html
Here are more details: 以下是更多详细信息:
How do I update if exists, insert if not (AKA "upsert" or "merge") in MySQL? 如何在MySQL中更新是否存在(如果存在),如果不存在则插入(又称为“ upsert”或“ merge”)?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.