Problem with an error message?

I need to make an error message appear if a user inputs an invalid username/password but the error message now displays whenever I open the program, even before I type anything in?

Here’s my code
[php]<?php
session_start();
ob_start(); // begin buffering the output
?>

Maintaining State With Sessions Form 2 Using PHP Sessions Ouput Form

<?php $un = $_POST['username']; $pw = $_POST['password']; // Server Connection variables $host = 'localhost'; $user = 'root'; $passwd = ''; $database = 'program3'; $table_name = 'user_passwords'; $qry1 = "Select * from $table_name where username = '$un' AND password = '$pw'"; $connect = mysql_connect($host, $user, $passwd); if (!$connect) { die ("Cannot connect to the $server using $user"); } else { // Select the database mysql_select_db($database, $connect); } if (session_id() == $_SESSION["sid"]) { $result = mysql_query($qry1, $connect) or die(mysql_error()); $row = mysql_fetch_array($result); if ($un = $row['username'] && $pw = $row['password']) {print "Welcome, you have successfully made it to the Julian Date Calculator";} else { $_SESSION["form2error"]= "Invalid Username and/or Password"; header('location: sessions_input.php'); } } else { $_SESSION["form2error"]= "Please enter a valid username and password before proceeding to screen 2"; header('location: sessions_input.php'); } ob_end_flush(); // output the data in the buffer ?>
Select Month: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

<?php print "Select Day: "; print ''; for ($i=1; $i<32; $i++){ print "$i"; } print '' ?>

Select Year: 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099




[/php]

Well, simple IF error! Note this is YOUR code, I just reformatted it to be a little more readable with the IF-ELSE’s more obvious… Should point out the error!

if (session_id() == $_SESSION[“sid”]) {
$result = mysql_query($qry1, $connect) or die(mysql_error()); (OKAY!)

$row = mysql_fetch_array($result);

if ($un = $row[‘username’] && $pw = $row[‘password’])
{print “Welcome, you have successfully made it to the Julian Date Calculator”;} (OKAY!)
else
{ $_SESSION[“form2error”]= “Invalid Username and/or Password”; (OKAY!)
header(‘location: sessions_input.php’); } } (OKAY!) (BUT, ends both the ELSE and IF

else (Hmmmm, where did this come from? No IF with this one!)
{ $_SESSION[“form2error”]= “Please enter a valid username and password before proceeding to screen 2”;

***** Brings us up to this next line! So, it is NOT inside anything and gets executed…
header(‘location: sessions_input.php’); }

I am sure that is it… Good luck…

Thank you. Any ideas on how to format it to make it work?

Well, I thought it was obvious, but, sometimes the obvious is the hardest to fix… LOL…

A normal IF-ELSE is like this:
If (condition) {
… some results…
}else{
… some other results…
}

…OR…
If (condition) {
…some results…
}elseif(condition){
…some other results for second condition…
}else{
…Results if all conditions failed matching…
}

So, First remove the extra “}” from after the header command so the rest of the IF is followed,
and figure out you logic to why there was a 3rd result without an IF… Since I do not know why you
have 3 results but only 2 options, this does not make sense. 3 results usually means 2 IFs and one
default if the first two IF’s do not match. Hope that helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service