Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Checking a MYSQL database if a user has submitted a code once already...

    Hi,

    Ive written a simple competition form that, upon submission, gets added to a mysql database. Part of the competition is submitting a short secret found code that can only be submitted once per user.

    Is there a way that, prior to successful submission, i can check if that email address has submitted that particular code already? ...and then notify the user with a message or take him to a web page that tells him so?

    thank you so much in advance!

    Johnny

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,285
    Thanks
    23
    Thanked 611 Times in 610 Posts
    Try using this:

    PHP Code:
    <?php
    //Get the user_email and the user_code

    $sql="SELECT email, code FROM table_name WHERE user_email = $user_email AND user_code = $user_code";
    $result mysqli_query($con,$sql);
    $rowcount mysqli_num_rows($result);
    if(
    $rowcount == 1) {
        echo 
    "You have used this code before";
    }else{
        
    //go ahead and enter into the DB
    }
    mysqli_close($con);

    ?>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Don't do that, do a count: SELECT count(*) FROM table_name WHERE user_email='$user_email' AND user_code='$user_code'. Without the count, the resultset will store ever record which matches in memory and transfers that by request. Instead, you simply pull the $record = mysqli_fetch_row($result); and then check if $row[0] is > 0.

    As a side note, this isn't a problem with one record since there is very little data to transfer, but its something you should get into the habit of doing instead of full resultset fetches. Just like using SELECT *, you never want to take more than you need (ie: don't use SELECT * unless you plan on using everything).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Even better is to get rid of the SELECT and just do an INSERT call. If the insert fails because the record already exists then you know that they have already used that code.

    You just need to have the two columns together defined as needing to be unique.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

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