PHPiSean Posted February 16, 2011 Share Posted February 16, 2011 Sorry for multiple problems in the past hour but it seems like I get help fast here. My login page will only withdraw 1 row of information from MySQL. I don't know why since whenever I enter the query into the terminal it seems to gives me all the information I need. Take a look, and thanks for being so helpful. <?php session_start(); require_once("includes/dbconn.php"); $connection = mysql_select_db('center'); if(!$connection){ echo "couldn't connect to database"; } echo $_SESSION['username']; $user = $_POST['username']; $pass = $_POST['password']; $sql = "select username, password from users"; $result = mysql_query($sql); $row = mysql_fetch_array($result); $isuser = $row['username']; $ispass = $row['password']; if(isset($_POST['submit'])){ if(($user==$isuser)&&($pass==$ispass)) { $_SESSION['username'] = $user; header("Location: home.php"); }else{ echo "Invalid Username or Password"; } } ?> <form name="login" method="post"> <input name="username" type="text"></br> <input name="password" type="password"></br> <input name="submit" type="submit" value="Log In"></br> </form> Quote Link to comment Share on other sites More sharing options...
lalnfl Posted February 16, 2011 Share Posted February 16, 2011 <?php session_start(); require_once("includes/dbconn.php"); $connection = mysql_select_db('center'); if(!$connection){ echo "couldn't connect to database"; } echo $_SESSION['username']; $user = $_POST['username']; $pass = $_POST['password']; $sql = "select username, password from users"; $result = mysql_query($sql); while ($row = mysql_fetch_array($result)){ $isuser = $row['username']; $ispass = $row['password']; } if(isset($_POST['submit'])){ if(($user==$isuser)&&($pass==$ispass)) { $_SESSION['username'] = $user; header("Location: home.php"); }else{ echo "Invalid Username or Password"; } } ?> <form name="login" method="post"> <input name="username" type="text"></br> <input name="password" type="password"></br> <input name="submit" type="submit" value="Log In"></br> </form> Not sure if this is what you were aiming for. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 16, 2011 Share Posted February 16, 2011 I understand you may be new to programming (at least that is what I assume). But, that is not a lot of code. Walk through your code and understand what it is doing. Exactly "HOW" do you expect that code to show/process all the results from the database. You are only grabbing the first result from the db results whith this line $row = mysql_fetch_array($result); You have to continue to use mysql_fetch_array() until you have read all the records from the db result. But, that is the absolute worst way to check if the user exists in the database. Instead of getting all the results and comparing them to the POST values. Use the POST values to see if there is a matching record in the DB! <?php session_start(); m require_once("includes/dbconn.php"); $connection = mysql_select_db('center'); if(!$connection) { echo "couldn't connect to database"; } echo $_SESSION['username']; if(isset($_POST['username'])) { //Login form was posted - try to authenticate //Parse the POSTed data $user = mysql_real_escape_string(trim($_POST['username'])); $pass = mysql_real_escape_string(trim($_POST['password'])); //Create and run query to find user in DB $sql = "SELECT username FROM users WHERE username='{$user}' AND password='{$pass}'"; $result = mysql_query($sql); if(mysql_num_rows($result)>0) { $_SESSION['username'] = trim($_POST['username']); header("Location: home.php"); } else { echo "Invalid Username or Password"; } } ?> <form name="login" method="post"> <input name="username" type="text"></br> <input name="password" type="password"></br> <input name="submit" type="submit" value="Log In"></br> </form> Quote Link to comment Share on other sites More sharing options...
lalnfl Posted February 16, 2011 Share Posted February 16, 2011 Yeah, I didn't look through all of that. Don't read mine. lol Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.