Handle error for duplicate entries - PHP MySQL

后端 未结 5 1727
礼貌的吻别
礼貌的吻别 2020-11-28 09:48

I have a PHP form which enters data into my MySQL database. My primary key is one of the user-entered values. When the user enters a value that already exists in the table,

5条回答
  •  一向
    一向 (楼主)
    2020-11-28 10:21

    You can check the return value from mysql_query when you do the insert.

    $result = mysql_query("INSERT INTO mytable VALUES ('dupe')");
    
    if (!$result) {
        echo "Enter a different value";
    } else {
        echo "Save successful.";
    }
    

提交回复
热议问题