Jump to content

How to customize mysql error messages? (get key with duplicate entry)


DavidT

Recommended Posts

Hi everyone.

I have a form to insert data in a mysql database, with some fields that are supposed to be unique.

If there is a duplicate key I show user an error message, however I?d like to avoid show them the mysql error text itself, to not show informations about the database structure.

If I use errno, instead, of course I could write a custom message. However, is there a straightforward way which key has a duplicate entry, so that I can point it out to the user?

 

I could find it out in some way (using some queries to check or by splitting the error message), but it looks to me like a very clumsy way to do something so simple. Isn?t it?

 

Thanks in advance.

Link to comment
Share on other sites

I don't think there is any way to determine which unique fields were possibly duplicated based on the error. I would just attempt the insert and trap the error if it occurs. Then if there is an error run a select query to get current records matching any of the unique values and loop through to determine which ones are duplicated.

Link to comment
Share on other sites

Oh, missed the part about determining which field(s) is/are duplicated. Yeah, I'm pretty sure even if there were multiple UNIQUE conflicts, the full mysql_error() message would only list the first field that causes a UNIQUE key conflict.

Link to comment
Share on other sites

Oh, missed the part about determining which field(s) is/are duplicated. Yeah, I'm pretty sure even if there were multiple UNIQUE conflicts, the full mysql_error() message would only list the first field that causes a UNIQUE key conflict.

 

Yeah, that's the way it behaves

Link to comment
Share on other sites

Thank you all!

 

It?s a good point that the error will show only the first field where a duplicate entry occurs. Given that, I suppose the best way is to add a code to my validation function that checks if the posted values are already in the database.

 

So, something like:

 

foreach ($unique_fields as $u) {
    $conditions[] = " `$u` = ".$PostedValue[$u];
}
//get db entries where some unique field has the same value as the one posted by the user
$strSQL = "SELECT * FROM `table_name` WHERE ".implode(' OR ',$conditions);  

And then it will be easy to see which fields has duplicate value.

However, is there any straightforward way to find out which unique fields the table has? Can I use "DESCRIBE"?

Link to comment
Share on other sites

Yep, DESCRIBE does work.

 

 

 

$invalid_fields = array();
$table = 'myTable';

		//Check for duplicate value in unique keys
		$res = mysql_query('DESCRIBE '.$table);
		$unique_fields = array();

		//Find which fields have to be unique
		while($row = mysql_fetch_assoc($res)) {
		    //Check if it is a unique field
		    if ($row['Key'] == 'UNI') $unique_fields[] = $row['Field'];
		}

		//Check data user posted for the unique fields
		foreach ($unique_fields as $u) {
			 $conditions[] = " `$u` = '".$FormData[$u]."'";
		}

		//get db entries where some unique field has the same value as the one posted by the user
		$strSQL = "SELECT * FROM `$table` WHERE ".implode(' OR ',$conditions);
		$res = mysql_query($strSQL);

		if ($res) while ($a = mysql_fetch_assoc($res)) {
			//Loop the fields to see if some has the same value
			foreach($unique_fields as $u) {
				if ($FormData[$u] == $a[$u]) {
					$invalid_fields[$u] = 'duplicate';
				}
			}
		} else { //query failed }

		if (empty($invalid_fields)) $valid = true;
		else $valid = false;

Link to comment
Share on other sites

Well, you *can* generate a single query to return a singe record with a value to determine if any duplicates exist in any of the columns you are interested in:

 

SELECT SUM(IF(field1='$value1', 1, 0)) as field1Dupe,
       SUM(IF(field2='$value2', 1, 0)) as field2Dupe,
       SUM(IF(field3='$value3', 1, 0)) as field3Dupe, 
FROM table_name

 

You will receive a single record with a 0 in each column that does not have a duplicate, else there will be a count of the number of duplicates in that column. Since you are not going to allow duplicates the values would only be a 0 or 1, so you can treat the returned values as Booleans.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.