Jump to content

MySQL only withdraws one row


PHPiSean

Recommended Posts

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>

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.