Hello,
I am using MySQL and PHP and I want to give a user-friendly error message when somebody tries to enter a duplicate record.
So instead of Duplicate entry ' ' for key 2, I want it to say something like ' this record has already been entered'.
Is there a way of writing user-friendly messages by seeing what kind of error occured or is the only way to do a select query on the database to see if the record exists. I don't want to do that if there's a better, less costly way of doing it.
Thanks
Hannah
Giving user-friendly error messages for database errors
Moderator: General Moderators
Probably best to execute a query to check to see if the value is already in the table before you execute your INSERT statement.
Some pseudo code
Mark
Some pseudo code
Code: Select all
// Query to check for existing data
if (data_already_exists) {
print "This data already exists in the DB";
} else {
// INSERT data into the DB
}you could do iot in one query, but it isn't very flexible
If the Query failed, it would output "this record has already been entered".
This would be just plain text, not formatted, and the message would occur even if the error wasn't with duplicate info, if would display the same message for ALL error on that query.
I would go with my first option, for more flexible.
Mark
Code: Select all
$result = mysql_query($query) or die ("this record has already been entered");This would be just plain text, not formatted, and the message would occur even if the error wasn't with duplicate info, if would display the same message for ALL error on that query.
I would go with my first option, for more flexible.
Mark