Current location: Hot Scripts Forums » Programming Languages » PHP » If/else statement working...but not working


If/else statement working...but not working

Reply
  #1 (permalink)  
Old 10-14-03, 07:22 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
If/else statement working...but not working

I'm using an if/else statement on a page named "results.php". When a user logs in from a form, they add a name and password, which are passed to "results.php" with no problem. When the name or password are incorrect, they get the "else" statement, showing that there is an error. This part is fine. However, when the name and password are correct, "results.php" displays as it should, but the "else" (error) statement is displaying at the top of the page. Heres what I'm using...

$student_number = $_POST['student_number'];
$unique_pass = $_POST['unique_pass'];

If($student_number=="student_number" && $unique_pass=="unique_pass")
{
print "Access Confirmed";<br>
}
Else
{ print "Access Denied<br>";
print "Your Student Number or Password is incorrect.<br>";
print "Please try again. Login Page";
}
?>
Any suggestions?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 10-15-03, 05:22 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
Quote:
but the "else" (error) statement is displaying at the top of the page.
can you explain this ? what exactly do you see ?

and note that you wrote :
PHP Code:

print "Access Confirmed";<br
and the <BR> should be inside the quote ..like this :
PHP Code:

print "Access Confirmed<br>"
and also use the single quote instead of the double one as long as you don't have variables inside .. using double quote makes your script slower .. try this one instead :
PHP Code:

<?php

$student_number 
$_POST['student_number'];
$unique_pass $_POST['unique_pass'];

If (
$student_number=="student_number" && $unique_pass=="unique_pass") {
echo 
'Access Confirmed<br>';
} else { 
echo 
'Access Denied<br>';
echo 
'Your Student Number or Password is incorrect.<br>';
echo 
'Please try again. Login Page';
}
?>
also as you can see I used echo instead of print .. but it doesn't matter anyway ..
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 10-15-03, 06:39 AM
ermau's Avatar
ermau ermau is offline
Wannabe Coder
 
Join Date: Aug 2003
Location: Florida, USA
Posts: 240
Thanks: 0
Thanked 0 Times in 0 Posts
Because I seriously doubt that everyone's number is "student_number" and their pass is "unique_pass". You need to retrieve their number and password from a database or whatever and then compare their entered items to those items.
__________________
PHP / mySQL Developer
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 10-15-03, 06:47 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
Quote:
Originally Posted by ermau
Because I seriously doubt that everyone's number is "student_number" and their pass is "unique_pass". You need to retrieve their number and password from a database or whatever and then compare their entered items to those items.
that's for sure ,, but I think he showed us only an example of his script ...
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 10-15-03, 10:45 AM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
I'm not familiar with if/else statements, so sorry if this is a pretty basic thing. The "results.php" is a query that matches the student_number and unique_pass entered from a form, and shows that users information. ( student_number and unique_pass are 2 fields of the db table). The query and results work fine. If the wrong name/password is entered, the else statement,

Access Denied.
Your Student Number or Password is incorrect.
Please try again.

is displayed instead, with nothing else. That part is fine also.

What isnt quite right is that, if the username and password are correct, then

Access Denied.
Your Student Number or Password is incorrect.
Please try again.

is still displaying at the top of the results.php page, then the rest of the results page is displayed just fine. The "access confirmed" part is no big deal, I really don't want it to display when the user name and password are correct. This is a sample code someone gave to me, so I was trying to work it in here. Hope that makes it a little clearer. I tried the code as you added above, but it still does the same thing.

Last edited by mdhall; 10-15-03 at 11:01 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 10-15-03, 02:22 PM
ermau's Avatar
ermau ermau is offline
Wannabe Coder
 
Join Date: Aug 2003
Location: Florida, USA
Posts: 240
Thanks: 0
Thanked 0 Times in 0 Posts
Can you post all of your exact code from that file?
__________________
PHP / mySQL Developer
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 10-15-03, 02:37 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
OK

The form input...

Student Enrollment Number:<br>
<input type=text name="student_number" size=30><p>
Password:<br>
<input type=text name="unique_pass" size=30><p>
<input type=submit name=submit value="View Records">

The reviewrecords.php page...

php
include("dbinfo.inc.php");//connection string
$student_number = $_POST['student_number'];
$unique_pass = $_POST['unique_pass'];
If ($student_number=="student_number" && $unique_pass=="unique_pass") {
echo 'Access Confirmed<br>'; //not necessary to display
}
Else
{
echo 'Access Denied<br>';
echo 'Your Student Number or Password is incorrect.<br>';
echo 'Please try again. Login Page';
}
$dbh=mysql_connect ("localhost", "$username", "$password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("$database");
$query=("SELECT * FROM studentinfo WHERE student_number='$student_number' AND unique_pass='$unique_pass'");
$result = mysql_query($query);
$num=mysql_num_rows($result);

$i = 0;

echo "<table width=500 border=1 cellpadding=5 cellspacing=0><tr>";
while ($i < $num):
$id=mysql_result($result,$i,"id");
$fname=mysql_result($result,$i,"fname");
$lname=mysql_result($result,$i,"lname");
$student_number=mysql_result($result,$i,"student_n umber");
$unique_pass=mysql_result($result,$i,"unique_pass" );
$grade_level=mysql_result($result,$i,"grade_level" );
$total_days=mysql_result($result,$i,"total_days");
$total_attend=mysql_result($result,$i,"total_atten d");
$total_absent=mysql_result($result,$i,"total_absen t");

echo "<td>$fname $lname<br>
$student_number<br>
$grade_level<br>
$unique_pass<br>
$total_days<br>
$total_attend<br>
$total_absent
echo "</td></tr>";
$i++;
endwhile;
/php
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 10-15-03, 08:33 PM
ermau's Avatar
ermau ermau is offline
Wannabe Coder
 
Join Date: Aug 2003
Location: Florida, USA
Posts: 240
Thanks: 0
Thanked 0 Times in 0 Posts
Ya, that's your problem, your doing it all wrong. You should be checking $num, if it equals 0 then they dont exist or haven't logged in with the right password, otherwise they have.
__________________
PHP / mySQL Developer
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 10-15-03, 08:39 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
Quote:
Originally Posted by ermau
Ya, that's your problem, your doing it all wrong. You should be checking $num, if it equals 0 then they dont exist or haven't logged in with the right password, otherwise they have.
How would I set that up? Like I said, this is a new area for me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 10-15-03, 09:02 PM
ermau's Avatar
ermau ermau is offline
Wannabe Coder
 
Join Date: Aug 2003
Location: Florida, USA
Posts: 240
Thanks: 0
Thanked 0 Times in 0 Posts
You've already done it....

Remove this:
PHP Code:

If ($student_number=="student_number" && $unique_pass=="unique_pass") { 

echo 
'Access Confirmed<br>'//not necessary to display
}
Else

echo 
'Access Denied<br>'
echo 
'Your Student Number or Password is incorrect.<br>'
echo 
'Please try again. Login Page'

And add this:
PHP Code:

if ($num == 0)

{
echo 
'denied';
}
else
{
echo 
'welcome';

After:
PHP Code:

$query=("SELECT * FROM studentinfo WHERE student_number='$student_number' AND unique_pass='$unique_pass'");

$result mysql_query($query);
$num=mysql_num_rows($result); 
__________________
PHP / mySQL Developer
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
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
If then statement acting up! geneane ASP 1 09-30-03 05:32 PM


All times are GMT -5. The time now is 12:43 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.