Jump to content

unexpected T_WHILE


Linjon

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.