jdock1 Posted June 23, 2010 Share Posted June 23, 2010 Ok I am just quickly coding a registration form. My problem, im using !empty to validate the form. Then use if and else statements to execute further code if all fields are cool. But when I load the page, the errors display. I thought using the code in this form would solve that; <?php $first = @$_POST['first']; $last = @$_POST['last']; $username = @$_POST['username']; $password = @$_POST['password']; $db = mysqli_connect('localhost', 'root', '', 'users') or die('Could Not Connect!'); $query = "INSERT INTO users (first, last, username, password) VALUES ('$first', '$last', '$username', SHA('$password'))"; if (isset($_POST['submit'])) { if (!empty($first) || !empty($last) || !empty($username) || !empty($password)) { mysqli_query($db, $query) or die('Could Not Query the Database.'); echo "Signup complete."; } } else { echo "Error: Not all fields were filled out."; } ?> I have been coding all day & Im sure its probly something small & stupid im missing. But can somebody please tell me wtf I did wrong here? Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/205610-i-swear-im-doing-everything-right-but-obviously-im-not-plz-revise-my-code/ Share on other sites More sharing options...
Ruzzas Posted June 23, 2010 Share Posted June 23, 2010 if (!empty($first)) || (!empty($last)) || (!empty($username)) || (!empty($password)) Quote Link to comment https://forums.phpfreaks.com/topic/205610-i-swear-im-doing-everything-right-but-obviously-im-not-plz-revise-my-code/#findComment-1075925 Share on other sites More sharing options...
jdock1 Posted June 23, 2010 Author Share Posted June 23, 2010 Im getting a parse error now.. Quote Link to comment https://forums.phpfreaks.com/topic/205610-i-swear-im-doing-everything-right-but-obviously-im-not-plz-revise-my-code/#findComment-1075929 Share on other sites More sharing options...
Ruzzas Posted June 23, 2010 Share Posted June 23, 2010 Akward.... try this: if ((!empty($first)) || (!empty($last)) || (!empty($username)) || (!empty($password))) Quote Link to comment https://forums.phpfreaks.com/topic/205610-i-swear-im-doing-everything-right-but-obviously-im-not-plz-revise-my-code/#findComment-1075932 Share on other sites More sharing options...
jdock1 Posted June 23, 2010 Author Share Posted June 23, 2010 Akward.... try this: if ((!empty($first)) || (!empty($last)) || (!empty($username)) || (!empty($password))) Nope! I really dont understand this either... it shouldnt be displaying on page load if isset is being used at all Quote Link to comment https://forums.phpfreaks.com/topic/205610-i-swear-im-doing-everything-right-but-obviously-im-not-plz-revise-my-code/#findComment-1075939 Share on other sites More sharing options...
Ruzzas Posted June 23, 2010 Share Posted June 23, 2010 Could you copy and paste the errors you're getting? Quote Link to comment https://forums.phpfreaks.com/topic/205610-i-swear-im-doing-everything-right-but-obviously-im-not-plz-revise-my-code/#findComment-1075941 Share on other sites More sharing options...
jdock1 Posted June 23, 2010 Author Share Posted June 23, 2010 Im not getting any errors now, just same thing, the server side errors are displaying on page load. When I used your first code I was just getting a parse error on that line. it didnt say what, just said parse error: parse error on line 11 Quote Link to comment https://forums.phpfreaks.com/topic/205610-i-swear-im-doing-everything-right-but-obviously-im-not-plz-revise-my-code/#findComment-1075945 Share on other sites More sharing options...
Ruzzas Posted June 23, 2010 Share Posted June 23, 2010 Shouldn't you be using & not ||? || = or & = and Quote Link to comment https://forums.phpfreaks.com/topic/205610-i-swear-im-doing-everything-right-but-obviously-im-not-plz-revise-my-code/#findComment-1075946 Share on other sites More sharing options...
mrMarcus Posted June 23, 2010 Share Posted June 23, 2010 What does "server side error" mean to you? Post that error, whatever it is. Quote Link to comment https://forums.phpfreaks.com/topic/205610-i-swear-im-doing-everything-right-but-obviously-im-not-plz-revise-my-code/#findComment-1075948 Share on other sites More sharing options...
jdock1 Posted June 23, 2010 Author Share Posted June 23, 2010 What does "server side error" mean to you? Post that error, whatever it is. I meant my custom errors. Ugh I am really losing my mind tonight with all this shit. And I probly should be using and too huh. Im thinking backwards today. Quote Link to comment https://forums.phpfreaks.com/topic/205610-i-swear-im-doing-everything-right-but-obviously-im-not-plz-revise-my-code/#findComment-1075950 Share on other sites More sharing options...
Ruzzas Posted June 23, 2010 Share Posted June 23, 2010 What does "server side error" mean to you? Post that error, whatever it is. I meant my custom errors. Ugh I am really losing my mind tonight with all this shit. And I probly should be using and too huh. Im thinking backwards today. No problem, Happens to me too Quote Link to comment https://forums.phpfreaks.com/topic/205610-i-swear-im-doing-everything-right-but-obviously-im-not-plz-revise-my-code/#findComment-1075951 Share on other sites More sharing options...
mrMarcus Posted June 23, 2010 Share Posted June 23, 2010 You still didn't post which error. I'm going to assume: "Error: Not all fields were filled out."? In which case that has nothing to do with your $_POST fields and everything to do with your submit button. That's the way you have your conditions laid out, anyways. Regardless, throw me a bone. Post the exact error or we you could be here all night. Quote Link to comment https://forums.phpfreaks.com/topic/205610-i-swear-im-doing-everything-right-but-obviously-im-not-plz-revise-my-code/#findComment-1075952 Share on other sites More sharing options...
kenrbnsn Posted June 23, 2010 Share Posted June 23, 2010 I rewrote you code. Take a look at this: <?php $db = mysqli_connect('localhost', 'root', '', 'users') or die('Could Not Connect!'); if (isset($_POST['submit'])) { $err = false; foreach ($_POST as $field => $val) { switch ($field) { case 'first': case 'last': case 'username': case 'password': if (strlen(trim($val)) == 0) { $err = true; } else { $$field = $val; } break; } } if (!$err) { $query = "INSERT INTO users (first, last, username, password) VALUES ('$first', '$last', '$username', SHA($password))"; mysqli_query($db, $query) or die('Could Not Query the Database.'); echo "Signup complete."; } else { echo "Error: Not all fields were filled out."; } } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/205610-i-swear-im-doing-everything-right-but-obviously-im-not-plz-revise-my-code/#findComment-1075954 Share on other sites More sharing options...
Ruzzas Posted June 23, 2010 Share Posted June 23, 2010 I rewrote you code. Take a look at this: <?php $db = mysqli_connect('localhost', 'root', '', 'users') or die('Could Not Connect!'); if (isset($_POST['submit'])) { $err = false; foreach ($_POST as $field => $val) { switch ($field) { case 'first': case 'last': case 'username': case 'password': if (strlen(trim($val)) == 0) { $err = true; } else { $$field = $val; } break; } } if (!$err) { $query = "INSERT INTO users (first, last, username, password) VALUES ('$first', '$last', '$username', SHA($password))"; mysqli_query($db, $query) or die('Could Not Query the Database.'); echo "Signup complete."; } else { echo "Error: Not all fields were filled out."; } } ?> Ken Not bad Quote Link to comment https://forums.phpfreaks.com/topic/205610-i-swear-im-doing-everything-right-but-obviously-im-not-plz-revise-my-code/#findComment-1075957 Share on other sites More sharing options...
jdock1 Posted June 23, 2010 Author Share Posted June 23, 2010 I rewrote you code. Take a look at this: <?php $db = mysqli_connect('localhost', 'root', '', 'users') or die('Could Not Connect!'); if (isset($_POST['submit'])) { $err = false; foreach ($_POST as $field => $val) { switch ($field) { case 'first': case 'last': case 'username': case 'password': if (strlen(trim($val)) == 0) { $err = true; } else { $$field = $val; } break; } } if (!$err) { $query = "INSERT INTO users (first, last, username, password) VALUES ('$first', '$last', '$username', SHA($password))"; mysqli_query($db, $query) or die('Could Not Query the Database.'); echo "Signup complete."; } else { echo "Error: Not all fields were filled out."; } } ?> Very good, thanks.. I try not to get into loops I am still new at all this. Im taking it one step at a time but the code does make sense to me. I just need to drill all that into my brain before I can code it from scratch. Thanks again. Ken Quote Link to comment https://forums.phpfreaks.com/topic/205610-i-swear-im-doing-everything-right-but-obviously-im-not-plz-revise-my-code/#findComment-1075988 Share on other sites More sharing options...
TapeGun007 Posted June 23, 2010 Share Posted June 23, 2010 I want to be like kenrbnsn someday when I grow up... I hope that's soon since I'm already 41.... Quote Link to comment https://forums.phpfreaks.com/topic/205610-i-swear-im-doing-everything-right-but-obviously-im-not-plz-revise-my-code/#findComment-1075991 Share on other sites More sharing options...
kenrbnsn Posted June 23, 2010 Share Posted June 23, 2010 Take my advice. Learn about foreach loops and arrays, as well as switch statements. They can make your coding much easier. Avoid using the "@" to suppress errors and learn how to avoid the errors in the first place. Ken Quote Link to comment https://forums.phpfreaks.com/topic/205610-i-swear-im-doing-everything-right-but-obviously-im-not-plz-revise-my-code/#findComment-1075995 Share on other sites More sharing options...
Ruzzas Posted June 23, 2010 Share Posted June 23, 2010 I want to be like kenrbnsn someday when I grow up... I hope that's soon since I'm already 41.... Believe it or not, I'm 14 Quote Link to comment https://forums.phpfreaks.com/topic/205610-i-swear-im-doing-everything-right-but-obviously-im-not-plz-revise-my-code/#findComment-1076004 Share on other sites More sharing options...
kenrbnsn Posted June 23, 2010 Share Posted June 23, 2010 I want to be like kenrbnsn someday when I grow up... I hope that's soon since I'm already 41.... Gee, thanks.. I won't tell you my age, but let's just say that when I learned PHP (over 10 years ago) I was older than you are now. When I first saw the Internet it was called the ARPAnet and had about 50 computers attached to it, plus I started programming using punch cards... Ken Quote Link to comment https://forums.phpfreaks.com/topic/205610-i-swear-im-doing-everything-right-but-obviously-im-not-plz-revise-my-code/#findComment-1076123 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.