sql - basic php else if not working -
i have php validation user signup form. it's validating input if correct else @ end, checks see if username in use , if not creates record in database. reason last else doesn't activated , refreshes data still in input boxes. can't find problem anywhere!!
if(isset($_post['user'])) { $firstname = sanitisestring($_post['firstname']); $surname = sanitisestring($_post['surname']); $user = sanitisestring($_post['user']); $pass = sanitisestring($_post['pass']); $email = sanitisestring($_post['email']); $dateofbirth = sanitisestring($_post['dateofbirth']); $gender = sanitisestring($_post['gender']); $test_arr = explode('-',$dateofbirth); if($firstname == "" || $surname =="" || $user == "" || $pass == "" || $email == "" || $dateofbirth == "" || $gender == "") {$error = "not fields entered</br></br>";} else if(!filter_var($email, filter_validate_email)) {$error = "email format invalid</br></br>";} else if(count($test_arr) == 3) { if (!checkdate($test_arr[0], $test_arr[1], $test_arr[2])) {$error = "enter date in format: mm-dd-yyyy</br></br>";} } else if(count($test_arr) <> 3) {$error = "enter date in format: mm-dd-yyyy</br></br>";} else { $result = querysql("select * members user='$user'"); if($result->num_rows) {$error = "that username exists</br></br>";} else { querysql("insert members values('','$firstname','$surname','$user','$pass','$email','$dateofbirth','$gender')"); die("<h4>account created</h4>please log in.</br></br>"); } } }
first thing comment on incredible amount of nested logic script has; it's not uncommon lose control of flow when you're if / else branching gets out of control.
example restructure
if (isset($_post['user'])) { // prep $error = ''; // sanitize foreach( $_post $varname => $value ) { // doing minification on stackoverflow $$varname = sanitisestring($_post[$varname]); // validate if ( empty($$varname) ) $error .= "not fields entered<br /><br />"; } // valid email? if ( !filter_var($email, filter_validate_email) ) $error .= "email format invalid<br /><br />"; // validate date $datearray = explode('-', $dateofbirth); if (!checkdate($datearray[0], $datearray[1], $datearray[2])) { $error .= "enter date in format: mm-dd-yyyy</br></br>"; } $result = querysql("select * members user='$user'"); if ($result->num_rows) { $error .= "that username exists</br></br>"; } if ( !empty($error) ) die($error); querysql("insert members values('','$firstname','$surname','$user','$pass','$email','$dateofbirth','$gender')"); die("<h4>account created</h4>please log in.</br></br>"); }
some other things note conflicting logic count($test_arr) == 3
, count($test_arr) <> 3
. , value of $result->num_rows
may not 0, expecting.
Comments
Post a Comment