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!
Link to comment
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.
Link to comment
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.
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.