berry05 Posted December 8, 2008 Share Posted December 8, 2008 i googled it and found over 100+ similar problems but couldnt find out how to fix it... here's the code that's giving me problems... <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } $sql_check = "SELECT username FROM register WHERE username = '$_POST[username]'"; $check = mysql_num_rows('$sql_check'); if($check > 0) { echo "username is already in use"; } else { BLA BLA BLA, ... mysql_select_db("register", $con); $sql="INSERT INTO register (username, email, password) VALUES ('$_POST[username]','$_POST[email]','$_POST[password]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "register success!!!"; mysql_close($con) ?> Quote Link to comment https://forums.phpfreaks.com/topic/136002-parse-error-syntax-error-unexpected-t_else-in-cwampwwwtestinsertphp-on-l/ Share on other sites More sharing options...
berry05 Posted December 8, 2008 Author Share Posted December 8, 2008 the error is on line 12 Quote Link to comment https://forums.phpfreaks.com/topic/136002-parse-error-syntax-error-unexpected-t_else-in-cwampwwwtestinsertphp-on-l/#findComment-709065 Share on other sites More sharing options...
premiso Posted December 8, 2008 Share Posted December 8, 2008 ...ummm <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } $sql_check = "SELECT username FROM register WHERE username = '$_POST[username]'"; $check = mysql_num_rows('$sql_check'); if($check > 0) { echo "username is already in use"; }else { mysql_select_db("register", $con); $sql="INSERT INTO register (username, email, password) VALUES ('$_POST[username]','$_POST[email]','$_POST[password]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "register success!!!"; } ?> Should fix it, you never ended the } the else and had that BLA BLA BLA uncommented. Also mysql_close does not need to be called, it is closed automatically after script executes. Quote Link to comment https://forums.phpfreaks.com/topic/136002-parse-error-syntax-error-unexpected-t_else-in-cwampwwwtestinsertphp-on-l/#findComment-709070 Share on other sites More sharing options...
berry05 Posted December 8, 2008 Author Share Posted December 8, 2008 now i get... Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\test\insert.php on line 10 register success!!! lol....any solutions? Quote Link to comment https://forums.phpfreaks.com/topic/136002-parse-error-syntax-error-unexpected-t_else-in-cwampwwwtestinsertphp-on-l/#findComment-709089 Share on other sites More sharing options...
Yesideez Posted December 8, 2008 Share Posted December 8, 2008 Change line 6 to this: $sql_check = mysql_query("SELECT username FROM register WHERE username = '".addslashes($_POST['username'])."'"); Then the next line to this: $check = mysql_num_rows($sql_check); Quote Link to comment https://forums.phpfreaks.com/topic/136002-parse-error-syntax-error-unexpected-t_else-in-cwampwwwtestinsertphp-on-l/#findComment-709090 Share on other sites More sharing options...
premiso Posted December 8, 2008 Share Posted December 8, 2008 mysql_query needs to be ran. Also you need to select the DB <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("register", $con); $sql_check = "SELECT username FROM register WHERE username = '" . mysql_real_escape_string($_POST['username']) . "'"; // needs to be done like this or with { } around the $_POST or else it will not work right. $check = mysql_num_rows(mysql_query($sql_check)); // mysql_query for the check if($check > 0) { echo "username is already in use"; }else { // mysql_select_db("register", $con); move this up top Quote Link to comment https://forums.phpfreaks.com/topic/136002-parse-error-syntax-error-unexpected-t_else-in-cwampwwwtestinsertphp-on-l/#findComment-709092 Share on other sites More sharing options...
Yesideez Posted December 8, 2008 Share Posted December 8, 2008 premiso - I've always had issues with mysql_real_escape_string but that's another topic... Quote Link to comment https://forums.phpfreaks.com/topic/136002-parse-error-syntax-error-unexpected-t_else-in-cwampwwwtestinsertphp-on-l/#findComment-709097 Share on other sites More sharing options...
premiso Posted December 8, 2008 Share Posted December 8, 2008 premiso - I've always had issues with mysql_real_escape_string but that's another topic... function escapeString($string) { return (get_magic_quotes_gpc()) ? mysql_real_escape_string(stripslashes($string)):mysql_real_escape_string($string); } Chances are it is because you have get_magic_quotes_gpc on, try using that function instead, which should escape a string properly. Quote Link to comment https://forums.phpfreaks.com/topic/136002-parse-error-syntax-error-unexpected-t_else-in-cwampwwwtestinsertphp-on-l/#findComment-709098 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.