Jump to content

suppress unique key error


isaac_cm

Recommended Posts

you need to check their data ahead of time and redirect them accordingly. for instance:
[code]
<?php
if (isset($_POST['submit'])) {
  $name = mysql_real_escape_string(trim($_POST['name']));
  $sql = mysql_query("SELECT * FROM myTable WHERE name = '$name'");
  if (mysql_num_rows($sql) > 0) {
    // that name has already been entered, redirect them or show an error
  } else {
    // that name is unique. go ahead and insert it.
  }
}
?>
[/code]

good luck!
[quote author=isaac_cm link=topic=112352.msg456028#msg456028 date=1161560772]
thanks alot, I know I can check first with "select" but I would prefer "insert ignore" it works prefectly with me, I want to know also if there is any other way

many thanks guys
[/quote]

are you using PHP5? if so, you can always create your own exception and throw it when the query fails.
[quote author=isaac_cm link=topic=112352.msg456028#msg456028 date=1161560772]
thanks alot, I know I can check first with "select" but I would prefer "insert ignore" it works prefectly with me, I want to know also if there is any other way

many thanks guys
[/quote]
Well, there are really only two ways, both discussed above: either you check the DB directly, or you try and see what the DB throws back.  For the latter, you have the DB not care, or you can have your PHP script care.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.