Jump to content

Checking Data in MYSQL before an insert.


superdude

Recommended Posts

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

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

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';}
      }
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.