realjho Posted November 15, 2013 Share Posted November 15, 2013 hello any body can help me with my codes? im new in php. my if else code is not working properly. it only read the else statement whether all the required input fields are empty or not. i always get the output of "Fill all the required fields!!" even if there are no empty fields. if a change the order of the condition it says that your registered but the data did not insert in my dayabase. <?phpif($connect=@mysql_connect("localhost","root",""))echo "";elsedie("Unable to Connect to the Database".mysql_error());$connect=mysql_select_db("olreport");if(isset($_POST['save'])){ $id = $_POST["userid"];$user = $_POST['username'];$pass = $_POST['password'];$uname = $_POST['name'];$pos= $_POST['position'];$divi = $_POST['div'];$divihd = $_POST['divhead']; if( !empty($id) && !empty($user) && !empty( $pass ) && !empty($uname) && !empty($pos) && !empty($divi) && !empty($divhd)) mysql_query("insert into signup(userid, username, password, name, position, division, divhead)values('".$id."','".$user."','".$pass."','".$uname."','".$pos."','".$divi. "','".$divihd."')"); echo "<script>alert('You are Now Registered!!')</script>"; } else if( empty($id) || empty($user) || empty( $pass ) || empty($uname) || empty($pos) || empty($divi) || empty($divhd)) { echo "<script>alert('Fill all the required fields!!')</script>"; } include('signup.php')?> Quote Link to comment Share on other sites More sharing options...
KaiSheng Posted November 15, 2013 Share Posted November 15, 2013 (edited) Your code is messed up. Problem lies with your connection part. if($connect=@mysql_connect("localhost","root",""))echo "";elsedie("Unable to Connect to the Database".mysql_error());$connect=mysql_select_db("olreport") -- You should not put a condition in a connection. use or die(); instead. -- I believe you knew this format if () { } else if () { } else { } Edited November 15, 2013 by KaiSheng Quote Link to comment Share on other sites More sharing options...
realjho Posted November 15, 2013 Author Share Posted November 15, 2013 my connection was okay. i have already save some data without field restriction. when i put the if else condition thats when the problem came in. when if else is not there saving is easy. what would be the other problem aside from connection? Quote Link to comment Share on other sites More sharing options...
KaiSheng Posted November 15, 2013 Share Posted November 15, 2013 Because the connection part, there isn't any brackets for the 'if'. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted November 15, 2013 Share Posted November 15, 2013 @KaiSheng, there's nothing technically wrong with the if/else statement for the OP's connection code. you simply don't know all the possible php syntaxes and you keep knocking topics off track with your posts. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted November 15, 2013 Share Posted November 15, 2013 you actually need to debug what your code is doing. find out why it is not taking the execution path you expect. what values do all of those variables have in them? also, here's some hints - 1) don't repeat yourself (DRY) and always try to use POSITIVE logic - if( empty($id) || empty($user) || empty( $pass ) || empty($uname) || empty($pos) || empty($divi) || empty($divhd)) { // your code for when this condition is true echo "<script>alert('Fill all the required fields!!')</script>"; } else { // your code for when this condition is false } 2) you should also form your sql query statement in a php variable so that you can echo/log it for debugging purposes. when you do this and echo the sql query statement, does it contain the expected values? 3) you ALWAYS need to have error checking logic in your programs to test if your query executed or not before trying to use the result from a query. in the case of your "You are Now Registered" message, you should only display that if the query ran without any errors and the row was actually inserted. 4) a person's userid is generally the autoincrement field in your database and you wouldn't have that as a from field or a value you put into the sql query statement. Quote Link to comment 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.