Odd PHP/mysqli bug

  • Thread starter Deleted member 66701
  • Start date

Deleted member 66701

D

Deleted member 66701

****FIXED**** Odd PHP/mysqli bug

*******FIXED - See post 2 - noob mistake!**********


I have a form that submits a user email address to be stored on the database (newsletter sign up). When submitting it first checks to see if the email already exists on the database, if it does it sends a message "already exists" and if it doesn't, it writes the email address to a database.

The weird bug is after the first submission and the email address is written, if you refresh the page, it will let you write a duplicate email address - i.e. the "if exists" check returns false, even though the email address IS on the database. If you refresh the page again and try submitting the email address for a third time, the "if exists" check returns true and the "already exists" message is returned. All subsequent attempts to use the same email address returns true for "if exists" as you would expect.

So what's going on? Why does the "if exists" check not return true for the second try but all subsequent tries?

Here is the HTML code:-

Code:
<script type="text/javascript">
//Newsletter registration routine
$(document).on("submit","#emailform",function() {
	//random number to make each AJAX call appear unique
	var randNum = Math.floor(Math.random() *10000000);
  
	$.ajax({
		url: "mng_user.php?rand=" + randNum,
		dataType: 'text',
		type: 'POST',
		data:  '&mode=emailsub' + '&email_addr=' + $("[name='email_addr']").val(),
   	success:function(result) {
     	$("#emailreplace").html('').append(result);
   	}
   
 });
	return false;  //stops form submitting
});

          <h4>Newsletter</h4>
          <div class="footer_search">
            <div id="emailreplace">
            <form action="mng_user.php" method="post" id="emailform">
              <fieldset class="input">
                <p id="login-form-username">
                    <input id="email" name="email_addr" type="email" value="Enter your email" onfocus="this.value = '';" 
                    	onblur="if (this.value == '') {this.value = 'Enter your email';}" required>                   
                    <input id="go" type="submit" value="Go">
                </p>
              </fieldset>
            </form>
            </div>
          </div>

And the PHP code:
Code:
if ($_POST['mode']=="emailsub") {  //if emailsub

	$email = mysqli_query($dbconnect, 
				"SELECT * FROM `EMAIL`
				WHERE `e_email_address` = '{$_POST['email_addr']}'");
	
	if(mysqli_num_rows($email)>1) {
?>
<p style="color:white">Email is already registered</p>
<?php	
	} else {  //Insert email into database
	
		$registeremail = mysqli_query($dbconnect,
						"INSERT INTO `EMAIL`
						(`e_email_address`,
						`e_newsletter_sub`)
						VALUES
						('{$_POST['email_addr']}','Y')");
		if($registeremail){
			?>
        	<p style="color:white">Email subscription successful</p>
            <?php } else { ?>
            <p style="color:white">Email subscription not successful</p>
            <?php 
		}
	}
}//end email sub routine
 
Last edited by a moderator:
Associate
Joined
26 Dec 2003
Posts
2,260
Location
UK
You're checking "if mysqli_num_rows($email)>1", which won't return true until the email address appears twice in the database.

Try "if (mysqli_num_rows($email) > 0)".

Also, I hope you're sanitising your user data :)
 
Last edited:

Deleted member 66701

D

Deleted member 66701

OP
Oh bloody hell! What a noob mistake!

Just needed a second pair of eyes - funny how you can look at something for ages and miss the obvious mistakes!

Thanks!

I'm not sanitising atm, next job on list - need to do some more reading up on it first.
 
Last edited by a moderator:
Back
Top Bottom