Jump to content

Recommended Posts

I have this error. My code:

<?php

include ('config.php');

session_start();

 

$username = $_POST['username'];

$password = $_POST['password'];

 

if ($username && $password)

{

 

 

$query = mysql_query("SELECT * FROM users WHERE username='$username'");

 

$numrows = mysql_num_rows($query);

 

if ($numrows!=0)

(

//code to login

while ($row = mysql_fetch_assoc($query));

{

$dbusername = $row['username'];

$dbpassword = $row['password'];

}

//check to see if they match!

if ($username == $dbusername && $password==$dbpassword)

{

echo "You are logged in! <a href='member.php'>Click here</a>";

$_SESSION['username'] == $username;

}

else

echo "Wrong password!";

}

else

die ("User not found!");

echo = $numrows;

}

else

die ("Write your username and password!");

 

?>

Link to comment
https://forums.phpfreaks.com/topic/196626-unexpected-t_while/
Share on other sites

Here's a cleaned up version of your code.

<?php
include ('config.php');
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
if ($username && $password)
{
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$query = mysql_query("SELECT * FROM `users` WHERE `username`='$username' LIMIT 1;");
$numrows = mysql_num_rows($query);
if ($numrows!=0)
{
	//code to login
	while ($row = mysql_fetch_assoc($query))
	{
		$dbpassword = $row['password'];
	}
	//check to see if they match!
	if ($password==$dbpassword)
	{
		$_SESSION['username'] == $username;
		header("location:member.php");
		exit();
	}
	else{
		echo "Wrong password!";
	}
}
else{
	echo $numrows;
	die ("User not found!");
}
}
else
die ("Write your username and password!");

?>

I changed

if ($numrows !=0)
(

to

if ($numrows !=0)
{

removed the semicolon at the end of the while line

 

fixed the indentation, bracketed the else statements, removed the check to see if $user=$dbuser, because the query would have come up empty if it didn't.

I also sanitized the user input so you aren't susceptible to injection attacks, cleaned up the query, so it only checks for one result, so it will run faster. I'm thinking I did more, but I lost track (boss came in, and I got side-tracked)

Link to comment
https://forums.phpfreaks.com/topic/196626-unexpected-t_while/#findComment-1032487
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.