dalerex Posted October 22, 2008 Share Posted October 22, 2008 hi there, php noob here. i was wondering if someone could help me out of a jam here. i have a script that checks my form if my fields are properly filled or not. while it does indeed check the form, it does not return the errors back to the user so that he/she knows what was wrong. all the script does is it resets the form without telling the user what happened. any ideas? any and all help will be greatly appreciated. the code is below. please tell me what i did wrong. thanks. <?php session_start(); require_once('config.php'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return mysql_real_escape_string($str); } $fname = clean($_POST['fname']); $lname = clean($_POST['lname']); $login = clean($_POST['login']); $password = clean($_POST['password']); $cpassword = clean($_POST['cpassword']); $city = clean($_POST['city']); $country = clean($_POST['country']); $month = clean($_POST['month']); $day = clean($_POST['day']); $year = clean($_POST['year']); $sex = clean($_POST['sex']); $designers = clean($_POST['designers']); $brands = clean($_POST['brands']); $mags = clean($_POST['mags']); $store = clean($_POST['store']); $have = clean($_POST['have']); $icon = clean($_POST['icon']); $style = clean($_POST['style']); $pants = clean($_POST['pants']); $shirts = clean($_POST['shirts']); $dress = clean($_POST['dress']); $shoe = clean($_POST['shoe']); if($fname == '') { $errmsg_arr[] = 'First name is missing'; $errflag = true; } if($lname == '') { $errmsg_arr[] = 'Last name is missing'; $errflag = true; } if($city == '') { $errmsg_arr[] = 'City is missing'; $errflag = true; } if($country == '') { $errmsg_arr[] = 'Country is missing'; $errflag = true; } if($sex == '') { $errmsg_arr[] = 'Sex is missing'; $errflag = true; } if($month == '') { $errmsg_arr[] = 'Month is missing'; $errflag = true; } if($day == '') { $errmsg_arr[] = 'Day is missing'; $errflag = true; } if($year == '') { $errmsg_arr[] = 'Year is missing'; $errflag = true; } if($designers == '') { $errmsg_arr[] = 'Designers missing'; $errflag = true; } if($brands == '') { $errmsg_arr[] = 'Brands missing'; $errflag = true; } if($mags == '') { $errmsg_arr[] = 'Magazines missing'; $errflag = true; } if($store == '') { $errmsg_arr[] = 'Favorite store missing'; $errflag = true; } if($have == '') { $errmsg_arr[] = 'Must Have missing'; $errflag = true; } if($icon == '') { $errmsg_arr[] = 'Icon missing'; $errflag = true; } if($style == '') { $errmsg_arr[] = 'Your Style missing'; $errflag = true; } if($pants == '') { $errmsg_arr[] = 'Pants size missing'; $errflag = true; } if($shirts == '') { $errmsg_arr[] = 'Shirt size missing'; $errflag = true; } if($shoe == '') { $errmsg_arr[] = 'Shoe size missing'; $errflag = true; } if($login == '') { $errmsg_arr[] = 'Login ID missing'; $errflag = true; } if($password == '') { $errmsg_arr[] = 'Password missing'; $errflag = true; } if($cpassword == '') { $errmsg_arr[] = 'Confirm password missing'; $errflag = true; } if( strcmp($password, $cpassword) != 0 ) { $errmsg_arr[] = 'Passwords do not match'; $errflag = true; } if($login != '') { $qry = "SELECT * FROM members WHERE login='$login'"; $result = mysql_query($qry); if($result) { if(mysql_num_rows($result) > 0) { $errmsg_arr[] = 'Login ID already in use'; $errflag = true; } @mysql_free_result($result); } else { die("Query failed"); } } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; session_write_close(); header("location: register-form.php"); exit(); } $qry = "INSERT INTO members(login, passwd, firstname, lastname, month, day, year, sex, city, country, designers, brands, mags, store, have, icon, style, pants, shirts, dress, shoe) VALUES('$login','".md5($_POST['password'])."','$fname','$lname','$month','$day','$year','$sex','$city','$country','$designers','$brands','$mags','$store','$h ave','$icon','$style','$pants','$shirts','$dress','$shoe')"; $result = @mysql_query($qry); if($result) { header("location: register-success.php"); exit(); }else { die("Query failed"); } ?> added code tags ~ CV Quote Link to comment Share on other sites More sharing options...
aeonsky Posted October 22, 2008 Share Posted October 22, 2008 Well... I do not think you have the error array printed anywhere... Where is '$_SESSION['ERRMSG_ARR']' printed? Quote Link to comment Share on other sites More sharing options...
CroNiX Posted October 22, 2008 Share Posted October 22, 2008 We probably need to see the register-form.php page. Just a note: instead of setting $errflag = true on each check, at the very end of your checking you could just: if(sizeof($errmsg_arr) > 0) $errflag = true; since you are starting with an empty array and filling it... Quote Link to comment Share on other sites More sharing options...
.josh Posted October 22, 2008 Share Posted October 22, 2008 posted code isn't really that efficient, but it looks functional, so what does register-form.php look like? do you have session_start() in there? Are you echoing $_SESSION['ERRMSG_ARR'] somewhere in it? p.s.- Please use code tags when posting code. Just a note: instead of setting $errflag = true on each check, at the very end of your checking you could just: if(sizeof($errmsg_arr) > 0) $errflag = true; since you are starting with an empty array and filling it... Or he could just remove the $errflag = true; in all his conditions and do if($errmsg_arr) { // make session var and header } since the array is only created if there's an error. Quote Link to comment Share on other sites More sharing options...
haku Posted October 22, 2008 Share Posted October 22, 2008 You can't pass an array in a session, you need to serialize it first. And without showing us the page that the user is redirected to, we can't tell you why it isn't outputting on the other side. Quote Link to comment Share on other sites More sharing options...
dalerex Posted October 22, 2008 Author Share Posted October 22, 2008 hi all, as requested, i've attached the register-form.php for you guys. again, please bear with me since i am still new to the php game. thanks again for your help. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
.josh Posted October 22, 2008 Share Posted October 22, 2008 You do not have to explicitly use serialize for passing arrays or objects through a session, as of like, v4.2. This: page1.php <?php session_start(); $fruit = array('a' => 'apple','b' => 'orange','c' => 'banana'); $_SESSION['blah'] = $fruit; print_r($_SESSION['blah']); ?> <a href = 'test2.php'>page2.php</a> page2.php <?php session_start(); print_r($_SESSION['blah']); ?> will output the expected result of: Array ( [a] => apple [b] => orange [c] => banana ) Quote Link to comment Share on other sites More sharing options...
dalerex Posted October 23, 2008 Author Share Posted October 23, 2008 i'm sorry CV, but can you please provide more detail to your response? i'm a bit lost at what you said. thanks. 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.