Syntax Error in Code

I am new to php and am having some trouble with my code. Please help.

if (mysql_num_rows($login) == 1) {
while($row = mysql_fetch_array($sql)){
// Get member ID into a session variable
$id = $row[“id”];
session_register(‘id’);
$_SESSION[‘id’] = $id;
// Get member username into a session variable
$username = $row[“username”];
session_register(‘username’);
$_SESSION[‘username’] = $username;
$_SESSION[‘position’] = $row[‘access’];
if(isset($_SESSION[‘position’] == 2)
{
header(‘Location: admin.php’);
}
else {
// Update last_log_date field for this member now
// Print success message here if all went well then exit the script
header(‘Location: securedpage.php’);
exit();}
} // close while
} else {
// Print login failure message to the user and link them back to your login page
header(‘Location: loginfail.html’);
exit();
}
}

Hi there,

It would help if you told us what we were helping you with?

Login form for website to check username, password, and user level. I will post the whole code.

<?php // Inialize session session_start(); // Include database connection settings include('connect_to_mysql.php'); // Retrieve username and password from database according to user's input $login = mysql_query("SELECT * FROM members WHERE (username = '" . mysql_real_escape_string($_POST['username']) . "') and (password = '" . mysql_real_escape_string(md5($_POST['password'])) . "') and activated='1'"); // Check username and password match if (isset($_SESSION['position'] == 2 and mysql_num_rows($login) == 1) { while($row = mysql_fetch_array($sql)){ // Get member ID into a session variable $id = $row["id"]; session_register('id'); $_SESSION['id'] = $id; // Get member username into a session variable $username = $row["username"]; session_register('username'); $_SESSION['username'] = $username; $_SESSION['position'] = $row['access']; if(isset($_SESSION['position'] == 2) { header('Location: admin.php'); } else { // Update last_log_date field for this member now // Print success message here if all went well then exit the script header('Location: securedpage.php'); exit();} } // close while } else { // Print login failure message to the user and link them back to your login page header('Location: loginfail.html'); exit(); } } ?>

Disregard previous code here is the right code.

<?php // Inialize session session_start(); // Include database connection settings include('connect_to_mysql.php'); // Retrieve username and password from database according to user's input $login = mysql_query("SELECT * FROM members WHERE (username = '" . mysql_real_escape_string($_POST['username']) . "') and (password = '" . mysql_real_escape_string(md5($_POST['password'])) . "') and activated='1'"); // Check username and password match if (mysql_num_rows($login) == 1) { while($row = mysql_fetch_array($sql)){ // Get member ID into a session variable $id = $row["id"]; session_register('id'); $_SESSION['id'] = $id; // Get member username into a session variable $username = $row["username"]; session_register('username'); $_SESSION['username'] = $username; $_SESSION['position'] = $row['access']; if(isset($_SESSION['position'] == 2) { header('Location: admin.php'); } else { // Update last_log_date field for this member now // Print success message here if all went well then exit the script header('Location: securedpage.php'); exit();} } // close while } else { // Print login failure message to the user and link them back to your login page header('Location: loginfail.html'); exit(); } } ?>

Okay, sorry what I meant was, what is the problem? What exactly has brought you here? (Error messages, line numbers, parts of code not quite doing what they should…)

Oh and please wrap your code in php tags to increase legibility

Ok sorry here is the code and the lines with errors.

<?php // Inialize session session_start(); // Include database connection settings include('connect_to_mysql.php'); // Retrieve username and password from database according to user's input $login = mysql_query("SELECT * FROM members WHERE (username = '" . mysql_real_escape_string($_POST['username']) . "') and (password = '" . mysql_real_escape_string(md5($_POST['password'])) . "') and activated='1'"); // Check username and password match if (mysql_num_rows($login) == 1) { while($row = mysql_fetch_array($sql)){ // Get member ID into a session variable $id = $row["id"]; session_register('id'); $_SESSION['id'] = $id; // Get member username into a session variable $username = $row["username"]; session_register('username'); $_SESSION['username'] = $username; $_SESSION['position'] = $row['admin']; if(isset($_SESSION['position'] == 2) ------------ Syntax error on this line { header('Location: admin.php'); } else { // Update last_log_date field for this member now // Print success message here if all went well then exit the script header('Location: securedpage.php'); exit();} } // close while } else { ------------- syntax error on this line // Print login failure message to the user and link them back to your login page header('Location: loginfail.html'); exit(); } } ------------------ syntax error on this line ?>

I need some help correcting the code so that it will work. Thanks

Can someone please help me?

Remove one } at the end.

Also if(isset($_SESSION[‘position’] == 2){ is missing a )
if(isset($_SESSION[‘position’] == 2)){

And its doing 2 checks. Just check for if($_SESSION[‘position’] == 2){

Thats all I can see.

Sponsor our Newsletter | Privacy Policy | Terms of Service