Jump to content


Photo

suppress unique key error


  • Please log in to reply
6 replies to this topic

#1 isaac_cm

isaac_cm
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 22 October 2006 - 07:14 PM

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

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 22 October 2006 - 07:18 PM

you need to check their data ahead of time and redirect them accordingly. for instance:
<?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.
  }
}
?>

good luck!
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 22 October 2006 - 08:51 PM

Alternatively, you can use INSERT IGNORE, depending on what you're actually doing.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#4 isaac_cm

isaac_cm
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 22 October 2006 - 11:46 PM

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

#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 23 October 2006 - 01:15 AM

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


are you using PHP5? if so, you can always create your own exception and throw it when the query fails.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 24 October 2006 - 02:30 AM

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

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#7 isaac_cm

isaac_cm
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 25 October 2006 - 11:26 PM

I can not understand , how you throw exception can you give me an example




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users