Sparrow Posted July 18, 2008 Share Posted July 18, 2008 Hello everyone, Okay I am trying to create a login area for my website. So I set up the login form the user info database and the validate.php. However I keep on having errors in my validiate.php when I test it out. I get past some but this one I can't get past. Here is the error:Parse error: syntax error, unexpected T_ELSE in /home/unitedai/public_html/validate.php on line 32 here is the php script 10<?php 11 session_start(); 12 $db_user = ******; 13 $db_pass = *******; 14 $user_name = $_POST['user_name']; 15 $password = $_POST['password']; 16 17 //connect to the DB and select the "dictator" database 18 $connection = mysql_connect('localhost', $db_user, $db_pass) or 19die(mysql_error()); 20 mysql_select_db('dictators', $connection) or die(mysql_error()); 21 22 //set up the query 23 $query = "SELECT * FROM users 24 WHERE user_name='$user_name' AND password='$password'"; 25 //run the query and get the number of affected rows 26 $result = mysql_query($query, $connection) or die('error making query'); 27 $affected_rows = mysql_num_rows($result); 28 //if there's exactly one result, the user is validated. Otherwise, he's 29invalid; 30 if($affected_rows == 1); 31 {print 'validated'; 32 else{print 'not valid'; 33?> Note: Nubers on side are just for reference . They are not part of the code. Link to comment https://forums.phpfreaks.com/topic/115357-please-help/ Share on other sites More sharing options...
Sparrow Posted July 18, 2008 Author Share Posted July 18, 2008 Anybody? Link to comment https://forums.phpfreaks.com/topic/115357-please-help/#findComment-593321 Share on other sites More sharing options...
unkwntech Posted July 18, 2008 Share Posted July 18, 2008 30 if($affected_rows == 1); 31 {print 'validated'; 32 else{print 'not valid'; 33?> should be 30 if($affected_rows == 1); 31 {print 'validated';}//<------ 32 else{print 'not valid';} //<------ 33?> You forgot to close the if and the else. Link to comment https://forums.phpfreaks.com/topic/115357-please-help/#findComment-593322 Share on other sites More sharing options...
samshel Posted July 18, 2008 Share Posted July 18, 2008 If condition does not have semi colon at the end 30 if($affected_rows == 1); //remove semi colon Link to comment https://forums.phpfreaks.com/topic/115357-please-help/#findComment-593324 Share on other sites More sharing options...
unkwntech Posted July 18, 2008 Share Posted July 18, 2008 I would segest using an IDE with syntax highlighting like PHPDesigner. It will help with things like this. Link to comment https://forums.phpfreaks.com/topic/115357-please-help/#findComment-593326 Share on other sites More sharing options...
Sparrow Posted July 18, 2008 Author Share Posted July 18, 2008 Okay thank you guys. That error is gone now, but now I have a new one: Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/unitedai/public_html/validate.php:6) in /home/unitedai/public_html/validate.php on line 7 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/unitedai/public_html/validate.php:6) in /home/unitedai/public_html/validate.php on line 7 Access denied for user 'unitedai_uniteda'@'localhost' to database 'dictators' Here is the full php doc: <html> <head> <title>validate.php</title> </head> <body> <?php session_start(); $db_user = ****; $db_pass = ****; $user_name = $_POST['user_name']; $password = $_POST['password']; //connect to the DB and select the "dictator" database $connection = mysql_connect('localhost', $db_user, $db_pass) or die(mysql_error()); mysql_select_db('dictators', $connection) or die(mysql_error()); //set up the query $query = "SELECT * FROM users WHERE user_name='$user_name' AND password='$password'"; //run the query and get the number of affected rows $result = mysql_query($query, $connection) or die('error making query'); $affected_rows = mysql_num_rows($result); //if there's exactly one result, the user is validated. Otherwise, he's invalid; if($affected_rows == 1) {print 'validated';} else{print 'not valid';} $user_name = $_POST['user_name']; $password = $_POST['password']; ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/115357-please-help/#findComment-593390 Share on other sites More sharing options...
unkwntech Posted July 18, 2008 Share Posted July 18, 2008 Theres a sticky in this forum about sessions.... session_start MUST BE CALLED BEFORE ANYTHING IS SENT TO THE USER Even whitespace will cause session_start to fail. put <?php session_start(); ?> at the top and remove the session_start from the middle. Link to comment https://forums.phpfreaks.com/topic/115357-please-help/#findComment-593391 Share on other sites More sharing options...
JasonLewis Posted July 18, 2008 Share Posted July 18, 2008 session_start() should always be at the top of the page, before any output and whitespace. Try this: <?php session_start(); ?> <html> <head> <title>validate.php</title> </head> <body> <?php $db_user = ****; $db_pass = ****; $user_name = $_POST['user_name']; $password = $_POST['password']; //connect to the DB and select the "dictator" database $connection = mysql_connect('localhost', $db_user, $db_pass) or die(mysql_error()); mysql_select_db('dictators', $connection) or die(mysql_error()); //set up the query $query = "SELECT * FROM users WHERE user_name='$user_name' AND password='$password'"; //run the query and get the number of affected rows $result = mysql_query($query, $connection) or die('error making query'); $affected_rows = mysql_num_rows($result); //if there's exactly one result, the user is validated. Otherwise, he's invalid; if($affected_rows == 1) {print 'validated';} else{print 'not valid';} $user_name = $_POST['user_name']; $password = $_POST['password']; ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/115357-please-help/#findComment-593394 Share on other sites More sharing options...
Sparrow Posted July 18, 2008 Author Share Posted July 18, 2008 Thank you so much. That fixed it. New error: Access denied for user 'unitedai_uniteda'@'localhost' to database 'dictators'. I don't get it I inputed the right database username and password so why is it being denied? Link to comment https://forums.phpfreaks.com/topic/115357-please-help/#findComment-593502 Share on other sites More sharing options...
JasonLewis Posted July 18, 2008 Share Posted July 18, 2008 Make sure the user exists, then make sure the user has permission to access that database. Link to comment https://forums.phpfreaks.com/topic/115357-please-help/#findComment-593533 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.