douao1854 2019-04-02 11:06
浏览 253
已采纳

INSERT INTO ... ON DUPLICATE KEY UPDATE不更新PHP和mySQL中的重复值

I'm trying to use the funcion INSERT INTO ... ON DUPLICATE KEY to insert some data in a mySQL table. And if there's already a user registered with the same email, i want to update the values.

So I implemented the code below after searching in this discussions: link-1, link-2, and other links...

I also watched the manuals link-4

$query=mysqli_query($conn,"INSERT INTO theElisa_signUp (name, email, telephone, user, psw, role, expire_date)
VALUES ('$name', '$email', 'not-set', '$username', '$password', '$role', '$expire_date')
ON DUPLICATE KEY
UPDATE name = '$name', user ='$username', psw = '$password', expire_date = '$expire_date' ");

In fact, the code can correctly insert a new user. But if I try to update a user that already exist, it creates a new record. So i have two user registered with the same email (I need to update the user if already there's the email in the table)

I hope this question respect the standards and I've already done all the researches in stackoverflow but none of the answers could help me. So please don't mark my question as duplicate.

P.s. I'm actually a web designer with zero experience in coding and this project is beyond my capabilities. But unfortunately I have to solve this question. Thank you.

  • 写回答

2条回答 默认 最新

  • doumu9019 2019-04-02 11:12
    关注

    The column email should be UNIQUE.

    ALTER TABLE `theElisa_signUpADD` UNIQUE (email);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能