Current location: Hot Scripts Forums » Programming Languages » PHP » validation not working


validation not working

Reply
  #1 (permalink)  
Old 05-26-05, 07:35 AM
buzzby buzzby is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
validation not working

i have 3 sets of validation.

1) validation to check if the email field is filled in - works fine

2) validation is the email format is right - works fine

3) validation to check if email already exists in database - does not work at all. here is the 3 validation scripts.
PHP Code:

if (isset($_POST['submit'])) 

{
$user="";
$host="localhost";
$password="";
$database="";
$connection mysql_connect($host,$user,$password) or die ("could not connect to server"); 
$db mysql_select_db($database,$connection);
if (empty(
$_POST['email'])) //validation to check if the email field has been filled in

echo 
"<table border=5 bordercolor=006633 bgcolor=#EDF0EC width=750 cellpadding=4 cellspacing=0 align=center>";
echo 
"<tr><td>";
echo 
"<img src = 'images/topimage.jpg'";
echo 
"</td></tr>";
echo 
"<tr><td>";
echo 
"<span class='textstylesbold'>";
echo 
"<br>You may have forgotten to enter your email address, please do so now."
echo 
"</span>";
echo 
"<form>";
echo 
"<input type='button' value='Go Back' onclick='history.back(-1)' class='newColor2'>";
echo 
"</form>";
echo 
"</td></tr></table>";
exit;
}
if(!
preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/"$_POST['email'])) //validation to check the format of an email address

echo 
"<table border=5 bordercolor=006633 bgcolor=#EDF0EC width=750 cellpadding=4 cellspacing=0 align=center>";
echo 
"<tr><td>";
echo 
"<img src = 'images/topimage.jpg'";
echo 
"</td></tr>";
echo 
"<tr><td>";
echo 
"<span class='textstylesbold'>";
echo 
"<br>Please enter a valid e-mail address."
echo 
"</span>";
echo 
"<form>";
echo 
"<input type='button' value='Go Back' onclick='history.back(-1)' class='newColor2'>";
echo 
"</form>";
echo 
"</td></tr></table>";
exit;
}
 


$sql "insert into questionaire (name, email, communicate_email, spelling_mistakes, nondictionary_words, openclose_emails, respond_email, emphasise_words, smart_emails, multiple_emails, file_emails, struggletosend, copy_email, printdocsfromemails, savedocsfromemails, changedocsfromemails, sendarequest, resendchangedemails, deleteoldemails, emailtoprinter) values ('{$_POST['name']}','{$_POST['email']}','{$_POST['communicate_email']}','{$_POST['spelling_mistakes']}','{$_POST['nondictionary_words']}','{$_POST['openclose_emails']}','{$_POST['respond_email']}','{$_POST['emphasise_words']}','{$_POST['smart_emails']}','{$_POST['multiple_emails']}','{$_POST['file_emails']}','{$_POST['struggletosend']}','{$_POST['copy_email']}','{$_POST['printdocsfromemails']}','{$_POST['savedocsfromemails']}','{$_POST['changedocsfromemails']}','{$_POST['sendarequest']}','{$_POST['resendchangedemails']}','{$_POST['deleteoldemails']}','{$_POST['emailtoprinter']}')";
$result mysql_query($sql) or print(mysql_error());
$emailcheck "SELECT * FROM questionaire WHERE email='{$_POST['email']}'"
$checkemailexists mysql_query($emailcheck);
 
if(
mysql_num_rows($checkemailexists) == 0) { //Check if the database found any matching emails 
echo "<table border=5 bordercolor=006633 bgcolor=#EDF0EC width=750 cellpadding=4 cellspacing=0 align=center>";
echo 
"<tr><td>";
echo 
"<img src = 'images/topimage.jpg'";
echo 
"</td></tr>";
echo 
"<tr><td>";
echo 
"<span class='textstylesbold'>";
echo 
"<br>Email Address is already there."
echo 
"</span>";
echo 
"<form>";
echo 
"<input type='button' value='Go Back' onclick='history.back(-1)' class='newColor2'>";
echo 
"</form>";
echo 
"</td></tr></table>";
exit;

Reply With Quote
  #2 (permalink)  
Old 05-26-05, 09:57 AM
dennispopel dennispopel is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 263
Thanks: 0
Thanked 0 Times in 0 Posts
$emailcheck = "SELECT * FROM questionaire WHERE email='{$_POST['email']}'";

replace with

$emailcheck = "SELECT * FROM questionaire WHERE email='" . $_POST['email'] . "'";

It cannot match things like {Array['email']} - you did use the interploation of variables wrong. You had to use something like

$emailcheck = "SELECT * FROM questionaire WHERE email='$_POST[email]'";

However, the syntax I gave is the most readable.
__________________
onPHP5.com - PHP5: Articles, News, Tutorials, Interviews, Software and more
Reply With Quote
  #3 (permalink)  
Old 05-26-05, 10:12 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
actually, his syntax IS correct and it will parse properly!
just try it
the syntax "$array[assoc]" will work only if you had one dimension array
if you had $array['assoc']['level2'] running "$array[assoc][level2]" will give parse error.. so the solution is "{$array['assoc']['level2']}" or of course concatenation..

this is actually the only way to parse arrays, even with one dimension, if you use the heredoc style! no other way will work.

take a look here:
http://www.php.net/manual/en/language.types.string.php
(you can actually use something like: echo "this is my ${variable}")

I think surrounding the array with braces is more readable.. matter of opinions
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]

Last edited by NeverMind; 05-26-05 at 10:16 AM.
Reply With Quote
  #4 (permalink)  
Old 05-26-05, 12:39 PM
Jaffizzle Jaffizzle is offline
Newbie Coder
 
Join Date: May 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
you are inserting $_POST['email']

then selecting where $_POST['email'] exists

there wouldnt be a point where num_rows = 0

could that be the problem?
Reply With Quote
  #5 (permalink)  
Old 05-26-05, 01:44 PM
dennispopel dennispopel is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 263
Thanks: 0
Thanked 0 Times in 0 Posts
Hm... It seems to me you already insert the email and then check if there is one...

$sql =
"insert into questionaire (name, email, communicate_email, spelling_mistakes,
nondictionary_words, openclose_emails, respond_email, emphasise_words,
smart_emails, multiple_emails, file_emails, struggletosend, copy_email,
printdocsfromemails, savedocsfromemails, changedocsfromemails, sendarequest,
resendchangedemails, deleteoldemails, emailtoprinter)
values
('{$_POST['name']}', '{$_POST['email']}','{$_POST['communicate_email']}','{$_POST['spelling_mistakes']}','{$_POST['nondictionary_words']}','{$_POST['openclose_emails']}','{$_POST['respond_email']}','{$_POST['emphasise_words']}','{$_POST['smart_emails']}','{$_POST['multiple_emails']}','{$_POST['file_emails']}','{$_POST['struggletosend']}','{$_POST['copy_email']}','{$_POST['printdocsfromemails']}','{$_POST['savedocsfromemails']}','{$_POST['changedocsfromemails']}','{$_POST['sendarequest']}','{$_POST['resendchangedemails']}','{$_POST['deleteoldemails']}','{$_POST['emailtoprinter']}')";

// here you insert it!
$result = mysql_query($sql) or print(mysql_error());

// This will return at least one row!
$emailcheck = "SELECT * FROM questionaire WHERE email='{$_POST['email']}'";
$checkemailexists = mysql_query($emailcheck);

Does your code always fail?
__________________
onPHP5.com - PHP5: Articles, News, Tutorials, Interviews, Software and more
Reply With Quote
  #6 (permalink)  
Old 05-26-05, 01:47 PM
dennispopel dennispopel is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 263
Thanks: 0
Thanked 0 Times in 0 Posts
To NeverMind:

Hmm... never new of this Thanks for the hint, but I think it's safer to keep to the concatenation. Highlighting editors are of a great help then. Don't you think?
__________________
onPHP5.com - PHP5: Articles, News, Tutorials, Interviews, Software and more
Reply With Quote
  #7 (permalink)  
Old 05-26-05, 02:55 PM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
np
but highlighting editors can also identify variables inside strings
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #8 (permalink)  
Old 05-27-05, 07:24 AM
buzzby buzzby is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
i have tried what was said and still the answer is the same. what i had before works on my own site. http://www.dcobbinah.co.uk if you look at the email subscription you will be able to enter a unique email address. when you try and enter it again you will see that you cannot enter it because its in the database already. but now i try and do the same thing and now it is not working. not sure what to do
Reply With Quote
  #9 (permalink)  
Old 05-27-05, 12:01 PM
Jaffizzle Jaffizzle is offline
Newbie Coder
 
Join Date: May 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
oh so thats what you are trying to do...

if thats the case what me and dennis said is the problem.

Normally, you should run the select query first to check if the email exists already.

if it doesnt exist then insert the value into the DB

else if it does exist return error statement.
Reply With Quote
  #10 (permalink)  
Old 05-28-05, 06:01 PM
buzzby buzzby is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
as you will see here the code clearly shows the SELECT QUERY first before the insert into database but still no joy. actually what happens is that the validation to check if anything is entered into the field is present. the validation to check if the email is in a correct format is there. both of these work. but the validation to check if the email address already exists doesnt work at all. yet all 3 validation scripts are before the inser script. this is my problem


PHP Code:

$connection mysql_connect($host,$user,$password) or die ("could not connect to server"); 

$db mysql_select_db($database,$connection);

$emailcheck "SELECT * FROM questionaire WHERE email='$email'";  
$checkemailexists mysql_query($emailcheck);
if(
mysql_num_rows($checkemailexists) == 1) { //Check if the database found any matching emails 
echo "<table border=5 bordercolor=006633 bgcolor=#EDF0EC width=750 cellpadding=4 cellspacing=0 align=center>";
echo 
"<tr><td>";
echo 
"<img src = 'images/topimage.jpg'";
echo 
"</td></tr>";
echo 
"<tr><td>";
echo 
"<span class='textstylesbold'>";
echo 
"<br>Email Address is already there."
echo 
"</span>";
echo 
"<form>";
echo 
"<input type='button' value='Go Back' onclick='history.back(-1)' class='newColor2'>";
echo 
"</form>";
echo 
"</td></tr></table>";
exit;
}else{
if (empty(
$_POST['email'])) //validation to check if the email field has been filled in

echo 
"<table border=5 bordercolor=006633 bgcolor=#EDF0EC width=750 cellpadding=4 cellspacing=0 align=center>";
echo 
"<tr><td>";
echo 
"<img src = 'images/topimage.jpg'";
echo 
"</td></tr>";
echo 
"<tr><td>";
echo 
"<span class='textstylesbold'>";
echo 
"<br>You may have forgotten to enter your email address, please do so now."
echo 
"</span>";
echo 
"<form>";
echo 
"<input type='button' value='Go Back' onclick='history.back(-1)' class='newColor2'>";
echo 
"</form>";
echo 
"</td></tr></table>";
exit;
}
if(!
preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/"$_POST['email'])) //validation to check the format of an email address

echo 
"<table border=5 bordercolor=006633 bgcolor=#EDF0EC width=750 cellpadding=4 cellspacing=0 align=center>";
echo 
"<tr><td>";
echo 
"<img src = 'images/topimage.jpg'";
echo 
"</td></tr>";
echo 
"<tr><td>";
echo 
"<span class='textstylesbold'>";
echo 
"<br>Please enter a valid e-mail address."
echo 
"</span>";
echo 
"<form>";
echo 
"<input type='button' value='Go Back' onclick='history.back(-1)' class='newColor2'>";
echo 
"</form>";
echo 
"</td></tr></table>";
exit;
}
}
$sql "insert into questionaire (name, email, communicate_email, spelling_mistakes, nondictionary_words, openclose_emails, respond_email, emphasise_words, smart_emails, multiple_emails, file_emails, struggletosend, copy_email, printdocsfromemails, savedocsfromemails, changedocsfromemails, sendarequest, resendchangedemails, deleteoldemails, emailtoprinter) values ('{$_POST['name']}','{$_POST['email']}','{$_POST['communicate_email']}','{$_POST['spelling_mistakes']}','{$_POST['nondictionary_words']}','{$_POST['openclose_emails']}','{$_POST['respond_email']}','{$_POST['emphasise_words']}','{$_POST['smart_emails']}','{$_POST['multiple_emails']}','{$_POST['file_emails']}','{$_POST['struggletosend']}','{$_POST['copy_email']}','{$_POST['printdocsfromemails']}','{$_POST['savedocsfromemails']}','{$_POST['changedocsfromemails']}','{$_POST['sendarequest']}','{$_POST['resendchangedemails']}','{$_POST['deleteoldemails']}','{$_POST['emailtoprinter']}')";
$result mysql_query($sql) or print(mysql_error()); 
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Email Validation - Not working (help) andrewvideo JavaScript 1 05-09-05 10:42 AM
Validation oracle_mik JavaScript 4 04-04-05 07:50 PM
server side validation using php jaishalg PHP 3 03-06-05 05:55 AM
VBScript not working on mac Jokier Visual Basic 1 04-14-04 08:47 AM
calendar working until months changed bitesize JavaScript 1 01-13-04 01:50 PM


All times are GMT -5. The time now is 06:46 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.