dotkpay Posted June 21, 2010 Share Posted June 21, 2010 I have been writing php scripts for a short while and I just encountered something unusual. The php statements in particular files, notably the conditional statements are executed upside down. As in the statements at the bottom of the page are parsed before some in the middle of the script most especially when it comes to the 'if' statements. Some 'if' statements are skipped even when the condition is true and then the 'elseif' statements are executed. What could be the problem? :DThanks in advance. ;) Quote Link to comment https://forums.phpfreaks.com/topic/205490-execution-of-statements/ Share on other sites More sharing options...
joel24 Posted June 22, 2010 Share Posted June 22, 2010 are you including() or requiring() other files? will have to see the code to find any problems. Quote Link to comment https://forums.phpfreaks.com/topic/205490-execution-of-statements/#findComment-1075310 Share on other sites More sharing options...
dotkpay Posted June 22, 2010 Author Share Posted June 22, 2010 Yes am requiring() other files. Quote Link to comment https://forums.phpfreaks.com/topic/205490-execution-of-statements/#findComment-1075311 Share on other sites More sharing options...
dotkpay Posted June 22, 2010 Author Share Posted June 22, 2010 This is the code. It is actually a script that conducts user registration by picking data posted from a form. What puzzles me is that the last statement of redirecting to 'complete.php' can get executed before anything else and therefore rendering much of the script useless. <?php require("connect.php"); $first = "{$_POST['first']}"; $last = "{$_POST['last']}"; $email = "{$_POST['email']}"; $phone = "{$_POST['phone']}"; $username = "{$_POST['username']}"; $password = "{$_POST['password']}"; $password2 = "{$_POST['password2']}"; // Disarm user entries $first = stripslashes($first); $last = stripslashes($last); $email = stripslashes($email); $phone = stripslashes($phone); $username = stripslashes($username); $password = stripslashes($password); $password2 = stripslashes($password2); $first = mysql_real_escape_string($first); $last = mysql_real_escape_string($last); $email = mysql_real_escape_string($email); $phone = mysql_real_escape_string($phone); $username = mysql_real_escape_string($username); $password = mysql_real_escape_string($password); $password2 = mysql_real_escape_string($password2); // If any field is NULL if ($first==NULL) { header("location:first.php"); } elseif ($last==NULL) { header("location:last.php"); } elseif ($email==NULL) { header("location:email.php"); } elseif ($phone==NULL) { header("location:phone.php"); } elseif ($username==NULL) { header("location:username.php"); } elseif ($password==NULL) { header("location:nopassword.php"); } elseif ($password2==NULL) { header("location:nopassword2.php"); } // If username then password is too short elseif (strlen($username)<5) { header("location:shortusername.php"); } elseif (strlen($password)<6) { header("location:shortpassword.php"); } // If passwords don't match elseif (!$password == $password2) { header("location:passwords.php"); } // If username = password elseif ($usernameoo == $password) { header("location:same.php"); } // If username already exists $result = mysql_query("SELECT * FROM profiles"); while($row = mysql_fetch_array($result)) { if ($row["username"]==$_POST['username']) { header("location:usernameexists.php"); } } // If all is well $encrypt_password=md5($password); mysql_query ("INSERT INTO profiles (username, first, last, email, phone) VALUES ('$username','$first','$last','$email','$phone')"); mysql_query ("INSERT INTO users (user, password) VALUES ('$username','$encrypt_password')"); mysql_close($connect); header("location:complete.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/205490-execution-of-statements/#findComment-1075313 Share on other sites More sharing options...
joel24 Posted June 22, 2010 Share Posted June 22, 2010 It looks like your mysql queries are failing, for the meantime put or die(mysql_error()) after the mysql_queries to see if they are executing correctly. You'll want to remove this line when the site is ready for publishing. mysql_query ("INSERT INTO profiles (username, first, last, email, phone) VALUES ('$username','$first','$last','$email','$phone')") OR die("1" . mysql_error()); mysql_query ("INSERT INTO users (user, password) VALUES ('$username','$encrypt_password')") OR die("2" . mysql_error()); mysql_close($connect); header("location:complete.php"); Or you could have something like the following $sql1 = "INSERT INTO profiles (username, first, last, email, phone) VALUES ('$username','$first','$last','$email','$phone')"; $sql2 = "INSERT INTO users (user, password) VALUES ('$username','$encrypt_password')"; if (@mysql_query($sql1) && @mysql_query($sql2)) { mysql_close($connect); header("location:complete.php"); } else { //error with mysql query. Change this to a nice neat error when production site is live exit(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/205490-execution-of-statements/#findComment-1075462 Share on other sites More sharing options...
salathe Posted June 22, 2010 Share Posted June 22, 2010 Calling header('Location: ...') does not immediately redirect; the remainder of the page is still executed. You should call exit immediately after the call to header if you want processing to stop and the redirect to occur at that point. Quote Link to comment https://forums.phpfreaks.com/topic/205490-execution-of-statements/#findComment-1075492 Share on other sites More sharing options...
dotkpay Posted June 22, 2010 Author Share Posted June 22, 2010 Thanks salathe, my problem is solved, thanks also go to joel24. Quote Link to comment https://forums.phpfreaks.com/topic/205490-execution-of-statements/#findComment-1075823 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.