Jump to content


Photo

Checking Data in MYSQL before an insert.


  • Please log in to reply
3 replies to this topic

#1 superdude

superdude
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 24 August 2006 - 07:03 PM

I am trying to make a form that certain fields will be checked against the data in mysql, this is to avoid duplicates.  Below is the code I am trying to use.  It is to verify that a phone number is not already in database.  i first checked that it was there, and then i tried to check it against the database, however it is simply going to my not uploaded warning.  What can I do?  Is there maybe a better way to do this?  as this is one of many checks like this. Note: this is not a primary nor will be.

if(empty($_POST['home_phone'])){$errors[]='Home Phone Required';
    }else{
    $hp=trim($_POST['home_phone']); }
    if($hp){
      //connects to databse
      require_once('./db_config.php');
      $hpq="SELECT home_phone FROM leads where home_phone='$hp'";
      $hpresult= mysql_query($hpq) or trigger_error("Query: $hpq\n<br/>MYSQL Error:".mysql_errno());
      if (mysql_num_rows($hpresult)==0){$home_phone=trim($_POST['home_phone']);}Else{
      $errors[]='Duplicate Lead';}
      ;}


  //If no errors
  if(empty($errors))

#2 mr. big

mr. big
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 24 August 2006 - 07:28 PM

This part looks good to me. I'm pretty much a noob but I use a nearly identical code, only the last line of mine makes the insertion query using $hp instead of giving $_POST['home_phone'] a second variable name.

Might be in another part of the code.
"Every monkey thinks its children are beautiful."

#3 superdude

superdude
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 24 August 2006 - 08:15 PM

Here is the coding modified a bit, still no better result.  Seems to always come up an error, even when one does not exsist.  Hmmmmm...

//home phone
    if(empty($_POST['home_phone'])){$errors[]='Home Phone Required';
    }else{
        if($_POST['home_phone']);{
      //connects to databse
      require_once('./db_config.php');
      $hpq="SELECT home_phone FROM leads where home_phone='$home_phone'";
      $hpresult= mysql_query($hpq) //or trigger_error("Query: $hpq\n<br/>MYSQL Error:".mysql_errno())
      ;}
      if(mysql_num_rows($hpresult)==0){
      $home_phone=trim($_POST['home_phone']);}Else
      {$errors[]='Duplicate Lead';}
      }

#4 fenway

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

Posted 28 August 2006 - 10:00 PM

An error how?
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users