Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    95
    Member #
    28555
    Liked
    1 times
    I am currently working on a registration form for my site.

    On submit, it runs through a script, which is as follows:
    PHP Code:
    <?php
     
    if ($_POST) {

    // Yes it has been Posted.....and should be full of data from the form
    // Need to Validate the data HERE
    //

        
    $firstname=$_POST['firstname'];
        
    $surname=$_POST['surname'];
        
    $gender=$_POST['gender'];
        
    $email=$_POST['email'];
        
    $contact=$_POST['contact'];
        
    $gmc=$_POST['gmc'];
        
    $quals=$_POST['quals'];
        
    $region=$_POST['region'];
        
    $system=$_POST['system'];
        
    $confirm=$_POST['confirm'];

    // Check if the Email has been used before
        
    $query "SELECT * FROM Locums where email = '$email'";
        
    $result mysql_query($query);

    // If Error with the Select.....
        
    if (!$result) {
            echo 
    "Could not access Table";
            die( 
    print_rmysql_error(),true));
        }

    //Select was OK - did it return anything?
        
    $rows mysql_num_rows$result );
        if (
    $rows === true) {
          die(
    "You have already registered with that e-mail address.");

    // *****should Redisplay the page with error.....
    }
     
    // Nothing returned so OK to create this record in the Locums Table

    // Generates random activation code
        
    $activ_code=uniqid(''true);
        
    $query="INSERT INTO Locums(firstname, surname, gender, email, contact, gmc, quals, region, system, confirm, uniquecode) VALUES ('$firstname','$surname','$gender','$email','$contact','$gmc','$quals','$region','$system','$confirm','$activ_code')";
        
    $result=mysql_query($query);

        if (!
    $result) {
            echo 
    "Error Inserting Record";
            die( 
    print_rmysql_errors(),true));
        }

    ?>

    All the data inserts in to the table correctly with a unique activation code etc etc but on testing, the email validation does not seem to be working because it allows me to duplicate an email address.

    Can anyone help with this?

    Thanks.

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    PHP Code:
     
    // Check if the Email has been used before
        
    $query "SELECT * FROM Locums where email = '$email'";
    //die here if there is a problem.
        
    $result mysql_query($query) or die(mysql_error());

    // No need to do this ... if a row is found,  that means it already exists.
    // If Error with the Select.....
    //    if (!$result) {
    //        echo "Could not access Table";
    //        die( print_r( mysql_error(),true));
    //    }

    //Select was OK - did it return anything?
        
    $rows mysql_num_rows$result );
    // if a row was found, the email has already been used.
        
    if ($rows 0) {
          die(
    "You have already registered with that e-mail address.");

    // *****should Redisplay the page with error.....



  4. #3
    Member
    Join Date
    Jul 2011
    Posts
    95
    Member #
    28555
    Liked
    1 times
    Excellent, here he is... Mr mlseim, the man I can always count on to solve my woes.

    How are you? thanks for the reply by the way, works a treat.
    One more thing you may be able to help me with now.

    The error appear on the page, but I would like the error to appear just above the form so that the user doesn't have to go back, they can just amend their errors and re-submit.

    I did find this bit from a previous code but if you could help me implement this, it would be much appreciated.

    PHP Code:
    <?php    
         
    if(!empty($err))  {
           echo 
    "<div class=\"msg\">";
          foreach (
    $err as $e) {
            echo 
    "* $e <br>";
            }
          echo 
    "</div>";    
           }
    ?>

  5. #4
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    That will be tougher because when you submitted the form,
    you execute another script. The refresh erases your form.

    You'll have to either use AJAX (PHP and javascripting), or somehow
    save all of your form variables so you can redisplay the form with
    the boxes still filled-in.

    I think you should go with the all AJAX form w/validation.

    Like these examples ...
    http://www.noupe.com/php/beautiful-forms.html

    Or some of these various techniques:
    http://www.noupe.com/css/47-excellen...css-forms.html

    You can still check if an email is already used.
    But if they type an email and click to the next box, it will tell them
    right away if it's used or not ... even before they submit.

    I guess there are so many ... you need to Google them to find the features you need.


  6. #5
    Member
    Join Date
    Jul 2011
    Posts
    95
    Member #
    28555
    Liked
    1 times
    OK, I may just leave this as is and inform the user that they have to click the back button to rectify their errors.
    Last thing I wanted to look in to is to highlight the input at the time of them completing the form if they leave it empty, on a previous google search for something else I stumbled across this to put in the input:
    PHP Code:
    <?php if($_POST[comments] == ""){ echo "class=\"error\""?>
    and then just have .error textarea {color: #} in the css, will this work?

  7. #6
    Member
    Join Date
    Jul 2011
    Posts
    95
    Member #
    28555
    Liked
    1 times
    Obviously with comments changed to whatever the input name is.

  8. #7
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    Repost the entire thing.
    I can't grasp how the page will refresh after validation.


  9. #8
    Member
    Join Date
    Jul 2011
    Posts
    95
    Member #
    28555
    Liked
    1 times
    I think this will do for now, It checks for duplicate e-mail addresses and wont submit if there is already one there, and if not it will process the form, it has hints but they will still submit blank if the user leaves them out, hopefully they will see the * and realise they must be completed.

    http://tinyurl.com/7qh263p

  10. #9
    Member
    Join Date
    Jul 2011
    Posts
    95
    Member #
    28555
    Liked
    1 times
    Just a few more queries regarding this form.
    On displaying the form, it looks absolutely fine (see link in previous post) and when submitted correctly, the thank you message is also fine, but if a duplicate email address is entered and the error message displays, it cannot be read because my white background does not show and it is as if the script has just stopped loading, there is no footer, only header and black text with the error.

    It is this bit that generates the error:
    PHP Code:
    //Select was OK - did it return anything?
        
    $rows mysql_num_rows$result );
    // if a row was found, the email has already been used.
        
    if ($rows 0) {
          die(
    "You have already registered with that e-mail address. Please click back on your browser to go back and try again.");

    // *****should Redisplay the page with error.....


  11. #10
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    Repost the entire thing ... something else must be causing your error.

    And turn on error reporting so you can see what the error really is.
    If you're not sure what I mean ... Google it: php error reporting



Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

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

Search tags for this page

validate duplicate record in php

Click on a term to search for related topics.
All times are GMT -6. The time now is 04:39 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com