I have set up a MySQL table multiple columns, one of which acts as a unique index. This column corresponds to one of my form inputs so that I can prevent duplicate row entries.
Being new to PHP, my question is: when the MySQL duplicate entries error is called, what would be the most succinct way of manipulating it so that I can display a custom message or return the user to the original form?
I’m guessing that mysql_errno and header(); would be the way to go but I’m struggling to understand how.
You don’t want regular users to see the mysql_error that is displayed, so a custom message is preferred here. When you’re using a mysql_query with an insert or update statement it will return true on success and false otherwise.
You could do something like this to solve it:
You could of course modify this so that the user is redirected somewhere upon success or failure. If you want to use the
headerfunction to redirect a user make sure that there is not output on the page before the function is called.