Page 1 of 2

Validating Users

Posted: Sat Feb 25, 2012 7:48 am
by RoseGardener
Hello:

I posted about this earlier in the week. Today I came back to review the post - and answer provided by Celauran. For the life of me, I can not find the original topic and posts. So... I am starting all over ... sorry for the double-post if my original topic is still out there somewhere.

At any rate, following is the problem. I'm a newbie to php (although I've 20 years of classical database development experience). I have figured out a few basics, creating input html files, database connect files and very basic posting files. But now I am trying to validate users. Initially my query was about checking for duplicate email addresses, but Celauran posted some script that also validated whether submitted emails were not only duplicated, but valid emails. Following is my coding thus far.

html post page - Title: register.html

Code: Select all

<body>
<form action="register_post.php" method="post">
First Name: <input type="text" name="FName" />
Last Name: <input type="text" name="LName" />
Email: <input type="text" name="EmailAddress" />
<input type="submit" />
</form> 
</body>
register_post.php

Code: Select all

<?php
// include database connection file, if connection doesn't work the include file will throw an error message
include '../####/db_files/db_connect.php';

// test code to find duplicate email addresses
// Query database to check if there are any matching Email addresses 
$query = "SELECT * FROM ContactTbl WHERE EmailAddress='{$_POST['EmailAddress']}'";
$result = mysql_query($query);

// mysql_num_rows tests for the number of matchiing rows.
$num_rows = mysql_num_rows($result);

if ($num_rows>"0")
	echo "Someone has already registered with this email address.";
else

// insert script inserts values from the register.html input fields into the appropriate table fields
$sql="INSERT INTO ContactTbl (FName, LName, EmailAddress)
VALUES
('$_POST[FName]','$_POST[LName]','$_POST[EmailAddress]')";

 if (!mysql_query($sql,$link))
  {
  die('Error: ' . mysql_error());
  }
echo "a record has been added to the database";

//following statement closes connection to databasee
mysql_close($link)
?>
Firstly - thank you Celauran for helping me get to this point. The code does execute as needed. It tells the user if the email address is already in the database. If the user enters a unique email address, then it inserts data into the table.

My only problem is the following message, if the user enters an email address that is already in the database.
Someone has already registered with this email address.Error: Query was empty
Where is the error coming from?????

Re: Validating Users

Posted: Sat Feb 25, 2012 9:24 am
by azycraze
you have put an integer inside quotes
remove the double quotes in the following line
==================

if ($num_rows>"0")

======================
:)

Re: Validating Users

Posted: Sat Feb 25, 2012 10:11 am
by Celauran
Was it this thread?

Re: Validating Users

Posted: Sat Feb 25, 2012 10:48 am
by RoseGardener
Celauran wrote:Was it this thread?
Yes - thank you!!!!!

For some reason that thread is not showing up in "View your posts", nor is it showing up in my UCP under posts. I've just subscribed to the original topic and bookmarked it so, it won't get lost again. But... I do wonder why it's not showing up in "View your posts"???

Also Celauran - thank you for your response in the previous thread. I've not had time to test and play with the code. But ... now that I know where it is, I will certainly take some time this weekend to work with it.

Pavilion

Re: Validating Users

Posted: Sat Feb 25, 2012 10:56 am
by RoseGardener
azycraze wrote:you have put an integer inside quotes
remove the double quotes in the following line
==================

if ($num_rows>"0")

======================
:)

Azycraze - thank you. I had been wondering if integers should be in quotes with php. I did as you suggested..... now my code looks like this:

Code: Select all

$num_rows = mysql_num_rows($result);

if ($num_rows > 0)
	echo "Someone has already registered with this email address.";
else
... And I am still getting this error message...
Someone has already registered with this email address.Error: Query was empty
I also tried eliminating spaces between rows > and 0... like this:

Code: Select all

($num_rows>0)
And got the same result.

Celauran has provided some code in my first thread that I really want to play with and learn from. But... just as importantly... I'd like to figure out where the "Error: query was empty" message is coming from. Any assistance is appreciated.

Thanks Much:

Pavilion

Re: Validating Users

Posted: Sat Feb 25, 2012 11:02 am
by Celauran
RoseGardener wrote:For some reason that thread is not showing up in "View your posts", nor is it showing up in my UCP under posts.
That thread was posted under a different account.

Re: Validating Users

Posted: Sat Feb 25, 2012 11:06 am
by RoseGardener
Celauran wrote:
RoseGardener wrote:For some reason that thread is not showing up in "View your posts", nor is it showing up in my UCP under posts.
That thread was posted under a different account.
:lol: :lol: :lol:

OMG.... I forgot that I registered here last September under RoseGardener..... how absolutely funny. And then when you referenced the thread, I didn't even notice it had been started under Pavilion.

Would it be possible for a Mod to move this thread to the pavilion account and then delete the RoseGardener account. I am sorry ... I truly forgot and would prefer to operate under Pavilion.

Thanks again - Pavilion

Re: Validating Users

Posted: Sun Feb 26, 2012 1:34 am
by azycraze
Try the following code.

Code: Select all

<?php
// include database connection file, if connection doesn't work the include file will throw an error message
include '../####/db_files/db_connect.php';

// test code to find duplicate email addresses
// Query database to check if there are any matching Email addresses 
$query = "SELECT * FROM ContactTbl WHERE EmailAddress='".$_POST['EmailAddress']."'";
$result = mysql_query($query);

// mysql_num_rows tests for the number of matchiing rows.
$num_rows = mysql_num_rows($result);

if ($num_rows > 0)
	{
    echo "Someone has already registered with this email address.";
	}
else
	{
	// insert script inserts values from the register.html input fields into the appropriate table fields
	$sql="INSERT INTO ContactTbl (FName, LName, EmailAddress) VALUES('".$_POST[FName]."','".$_POST[LName]."','".$_POST[EmailAddress]."')";
	if (!mysql_query($sql,$link))
		{
		die('Error: ' . mysql_error());
		}
	else
		{
		echo "a record has been added to the database";
		}
	}
//following statement closes connection to databasee

mysql_close($link);

?>

Hope this will work for you.

Re: Validating Users

Posted: Sun Feb 26, 2012 7:14 am
by Pavilion
azycraze - that works.... thank you. Now for another question... within the following portion of your if statement, how do I redirect the user back to the previous page - which is still holding their data.

Code: Select all

if ($num_rows > 0)
        {
    echo "Someone has already registered with this email address.";
        }
else
azycraze, I'm as interested in method as syntax. Since I'm new to php/html I've lot's of questions about method.

For instance I know that php is server side and going back to the previous page is client side. I know that html is client side. So... is it OK to use html within this php script? How often is mixing html/php in the same document/script/file done? Do most php/html coders do this... or is it the "sloppy way of doing things"???

I'm just trying to get a handle on industry standards.

Thanks Much:

Pavilion

Re: Validating Users

Posted: Sun Feb 26, 2012 9:42 am
by azycraze
why not!!!!!! YOU can use html within php scripts.It can do magic for you, not always but sometimes!!

You can use headers for redirection!
But I would prefer you to use a function for redirection
You can use following function

Code: Select all

//redirect
	
function redirect_to($url){
echo '<script type="text/javascript">
window.location = "'.$url.'"
</script>';
exit("Javascript is turned off, <a href='$url'>click here to go to requested page</a>");
}

For redirection call the function within the if statement.for eg:

Code: Select all

if ($num_rows > 0)
        {
       // echo "Someone has already registered with this email address.";
       redirect_to("http://azywebcoder.blogspot.in");   //use your previous url here
        }

Re: Validating Users

Posted: Sun Feb 26, 2012 10:26 am
by Celauran
Pavilion wrote: How often is mixing html/php in the same document/script/file done? Do most php/html coders do this... or is it the "sloppy way of doing things"???
Both.

Re: Validating Users

Posted: Sun Feb 26, 2012 4:15 pm
by social_experiment
Pavilion wrote:...how do I redirect the user back to the previous page - which is still holding their data.
You can use the header() function as another poster suggested; To 're-display' data in the form you could use isset() like this

Code: Select all

<input type="text" id="nameField" name="nameField" value="<?php echo isset($_POST['nameField']) : $_POST['nameField'] ? ''; ?>" />

Re: Validating Users

Posted: Mon Feb 27, 2012 1:12 pm
by temidayo
Here is an improved redirect function. It tries to make use of PHP(which can sometimes fail if any form of input has been sent to the browser)
then Javascript.

Code: Select all

function redirect($url) { 
       if(headers_sent()) { 
               echo "<script type='text/javascript'>location.href='$url';</script>"; 
       } else { 
               header("Location: $url"); 
       } 
}

Re: Validating Users

Posted: Mon Feb 27, 2012 2:13 pm
by Pavilion
temidayo wrote:Here is an improved redirect function. It tries to make use of PHP(which can sometimes fail if any form of input has been sent to the browser)
then Javascript.

Code: Select all

function redirect($url) { 
       if(headers_sent()) { 
               echo "<script type='text/javascript'>location.href='$url';</script>"; 
       } else { 
               header("Location: $url"); 
       } 
}
Thanks for all your suggestions. As of right now, this is the script within my register_post.php file.

Code: Select all

<?php
// include database connection file, if connection doesn't work the include file will throw an error message
include '../#####/db_files/db_connect.php';

//declaring $url variable
$url = 'http://#######.com/schedule/register.html';

//redirect function, using $url variable. Used to send user back to original screen if a duplicate email address is detected.
function redirect($url) {
       if(headers_sent()) {
               echo "<script type='text/javascript'>location.href='$url';</script>";
       } else {
               header("Location: $url");
       }
}

// test code to find duplicate email addresses
// Query database to check if there are any matching Email addresses
$query = "SELECT * FROM ContactTbl WHERE EmailAddress='".$_POST['EmailAddress']."'";
$result = mysql_query($query);

// mysql_num_rows tests for the number of matchiing rows.
$num_rows = mysql_num_rows($result);

if ($num_rows > 0)
        {
    echo "Someone has already registered with this email address.";
	redirect("$url");   //use redirect function from line 9
        }
else
        {
        // insert script inserts values from the register.html input fields into the appropriate table fields
        $sql="INSERT INTO ContactTbl (FName, LName, EmailAddress) VALUES('".$_POST[FName]."','".$_POST[LName]."','".$_POST[EmailAddress]."')";
        if (!mysql_query($sql,$link))
                {
                die('Error: ' . mysql_error());
                }
        else
                {
                echo "a record has been added to the database";
                }
        }
//following statement closes connection to databasee

mysql_close($link);

?>
Firstly - this whole exercise is an excellent learning experience. So... thank you all for your patience. The above script does redirect the user back to the original html page. However two problems remain.
  • The redirect happens so fast that the user will never see this message. "Someone has already registered with this email address."
  • Although this script does redirect back to the original url, previously entered information is gone. All the fields are blank.
  1. How do I echo a message "Someone has already registered with this email address" in a way that it won't get "lost" in the redirect?
  2. How do I retain previously entered information?
Thanks Much - Pavilion

Re: Validating Users

Posted: Mon Feb 27, 2012 2:21 pm
by Pavilion
social_experiment wrote:
Pavilion wrote:...how do I redirect the user back to the previous page - which is still holding their data.
You can use the header() function as another poster suggested; To 're-display' data in the form you could use isset() like this

Code: Select all

<input type="text" id="nameField" name="nameField" value="<?php echo isset($_POST['nameField']) : $_POST['nameField'] ? ''; ?>" />
Social_Experiment -

Your solution seems to address the issue of retaining previously entered information. But... I'm not sure yow to use this function. Do I use it in the original html file, or in the post.php processing file?

Do I use it within the "If ($num_rows > 0)" statement????

I'm sorry - this is all so new to me. I have a solid grasp of classical database construction and VB... but php/htm/Javascript are all new. I don't even know what a header() function is... going to google right now. :?

Thanks again for your patience - Pavilion