Jump to content

Check for Existing User


Go to solution Solved by DavidAM,

Recommended Posts

Heya everyone,

 

Still working on learning PHP in my college course and I ran into another problem. I'm kind of stumped because the assignment gave me some extra code, told me exactly where to place it and it is not working. It's kind of frustrating lol. Anyhow, perhaps someone could explain why this is not working.

 

This is the code they told me to put into my script.

 

Instructions:

Add the following code to check to see if the guest already exists in the database. Place the code immediately above the INSERT query as part of your validation routines.

//Check to see if guest already exists in the database 
$query = "SELECT * from guests WHERE fname='$fname' AND lname='$lname' AND email='$email'"; 
if ($result = mysqli_query($connect,$query) or die(mysqli_error($connect))) { 
echo "You have already signed my guestbook. Thanks!"; 
} else { 
YOUR EXISTING INSERT QUERY AND ECHO STATEMENTS DISPLAYING THE QUERY RESULTS ARE HERE 
}

So I did that and here is what it looks like:

<?php
//Set variable for page title prior to loading header which needs the page title variable.
$pagetitle = 'Student, PHP';
//Add in header.
include ('includes/header.php');
//Requires the database connection script.
require ('includes/connect.php');
?>


<!-- Sticky form displays -->

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<legend>Please sign my guest book!</legend>
<label for="fname">First: </label><input type="text" name="fname" size="25" maxlength="25" 
value="<?php if(isset($_POST['fname'])) echo $_POST['fname'];?>"/>
<br/>
<label for="lname">Last: </label><input type="text" name="lname" size="25" maxlength="25"
value="<?php if(isset($_POST['lname'])) echo $_POST['lname'];?>"/>
<br/>
<label for="email">Email: </label><input type="text" name="email" size="25" maxlength="50"
value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>"/>
<br/>
<label for="password">Password: </label><input type="password" name="password" size="10" maxlength="40"
value="<?php if(isset($_POST['password'])) echo $_POST['password'];?>"/>
<br/>
<label for="confirmpass">Confirm Password: </label><input type="password" name="confirmpass" size="10" maxlength="40"
value="<?php if(isset($_POST['confirmpass'])) echo $_POST['confirmpass'];?>"/>
<br />
<p><b>Comments</b></p> 
<textarea name="comment" cols="40" rows="10" maxlength="255"><?php if(isset($_POST['comment'])) echo $_POST['comment'];?></textarea>
<br/> 
<input type="reset" value="Reset"/> <input type="submit" name="submit" value="Submit" />
<br/>
</fieldset>
</form> 



<?php
//Your PHP code to process the submitted form goes here.
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
	// Perform Validation
	if (!empty($_POST['fname'])){
		if (!empty($_POST['lname'])){
			if (!empty($_POST['email'])){
			$email = $_POST['email'];
				if (strstr($email, '@', true)){
					if (!empty($_POST['password'])){
						if (!empty($_POST['confirmpass'])){
							if ($_POST['password'] == $_POST['confirmpass']){
								if (!empty($_POST['comment'])){
									$comment = mysqli_real_escape_string($connect, trim($_POST['comment']));
								} else if (empty($_POST['comment'])){
									$comment = NULL;
								}
								// assign and trim variables.
								$fname = mysqli_real_escape_string($connect, trim($_POST['fname']));
								$lname = mysqli_real_escape_string($connect, trim($_POST['lname']));
								$email = mysqli_real_escape_string($connect, trim($_POST['email']));
								$password = mysqli_real_escape_string($connect, trim($_POST['password']));
								// Display connection error if any.
								if (mysqli_connect_errno()) {
  									echo "Failed to connect to MySQL: " . mysqli_connect_error();
  								}
								 
								//Check to see if guest already exists in the database 
								$query = "SELECT * from guests WHERE fname='$fname' AND lname='$lname' AND email='$email'"; 
								if ($result = mysqli_query($connect,$query) or die(mysqli_error($connect))) { 
									echo "You have already signed my guestbook. Thanks!"; 
								} else { 
									// Enter user input into database
									$query = "INSERT INTO guests (fname, lname, email, password, comment)
									VALUES ('$fname','$lname','$email','$password','$comment')";
								
									// If error kill script and display error.
									if (!mysqli_query($connect,$query))
   									{
  									die('Error: ' . mysqli_error($connect));
   									}
								
 									// Display success messaage.
									echo "<h1><p class='message'>Greetings $fname</p></h1>";
									echo "<p class='message'>Thank you for signing my guest book!</p>";
									
									// Close database connection.
									mysqli_close($connect);
									
								}
	// If validation fails display.							
							} else if ($_POST['password'] != $_POST['confirmpass']){
								echo '<p class="message">Your password confirmation does not match.</p>';}					
						} else {
							echo '<p class="message">Please confirm your password.</p>';}
					} else {
						echo '<p class="message">Please enter a password.</p>';}			
				} else {
					echo '<p class="message">Please enter a valid email address.</p>';}			
			} else {
				echo '<p class="message">Please enter an email address.</p>';}			
		} else {
			echo '<p class="message">Please enter your last name.</p>';}		
	} else {
		echo '<p class="message">Please enter your first name.</p>';}
}



//Add in footer.
include ('includes/footer.php');
?>

At the far right of my little pyramid scheme here you can see that I put the required code where it asked me to in the instructions.

The problem I have now is that I can not longer enter any new users into the database, it always tells me "You have already signed my Guestbook."

 

If anyone can help me out here I would really appreciate it. I just don't understand exactly what the problem is. As always thanks just for looking.

 

Best Regards,

Nightasy

Link to comment
https://forums.phpfreaks.com/topic/279639-check-for-existing-user/
Share on other sites

  • Solution
mysqli_query only returns false if the query fails to execute. When a query executes and returns no data, the function/method returns a valid object. The code you were told to insert is not valid for the intention of checking if data exists. You would need to check the number of rows returned in the result.

Ah, that makes perfect sense. So naturally the if statement is always going to be true because the query is always going to return data whether the data is empty or not, which is why it is always telling me the user already exists.

 

Am I understanding that right?

Basically, yes. Typically, after calling *_query() you would call *_fetch() to get a row of the data. That function would return false when there are no more rows to process. Have a look at the manual page link in my last post. In this case, since you don't really want the data, you can call *num_rows to see if it is zero (not found) or greater than zero (one or more found).

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.