SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2005
    0 Post(s)
    0 Thread(s)

    Form & Database processing

    Hi all,

    Is it possible to check an entry against a database before deciding how to process.


    I have a basic contact form, Name, email, message. What I would like to happen is when the user hits the submit button I want to check if the email address already exists in the database. If it doesnt I want to post the info to a new record, if it does i dont want to post anything to the database, in both instances I want to send the form to an email recipient.

    If it is possible to do this please can someone either tell me where I will find a good tutorial which covers it or what search terms should I be searching for. I have tried for ages and just dont get the right results when I try to google.

    Thanks in advance

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Lancaster University, UK
    2 Post(s)
    0 Thread(s)
    What most database gurus will probably tell you is that the best way to do this is by making the email field a unique key. By doing that, it isn't possible to have two records with the same value in the email field.

    You then run the insert as normal and save the return of the query to a value (don't append 'or die(mysql_error());' this time. Then, if the value is equal to false, check if mysql_errno == 1062 (IIRC), then that email already exists in the db, so just continue processing as normal.

    PHP Code:
    $Etc MySQL_Real_Escape_String($_POST['etc']);
    $Query MySQL_Query("INSERT INTO table (Email, etc) VALUES ('{$Email}', '{$Etc}')");
    $Query === false && MySQL_ErrNo() != 1062){ //there was actually an error, not duplicate value...
        //error out...
    //send confirmation email
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts