Jump to content

Archived

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

isaac_cm

suppress unique key error

Recommended Posts

Hi everybody,
I Have a unique field key in my table and I want if the visitor enter the same data in that field to not see the mysql error about unique field , is there a way to direct the visitor to another page

thanks

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Alternatively, you can use INSERT IGNORE, depending on what you're actually doing.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites

×

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.