perky416 Posted March 4, 2011 Share Posted March 4, 2011 Hi everyone. I have a registration form on my website that returns errors if the form isnt filled out correctly. An example of the code that returns the errors is: // if first name is blank return error if (!$first_name) { echo "Please enter your first name!<br />"; } // if first name contains invalid charcters return error if (preg_match('/[^a-zA-Z\s]/', $first_name)) { echo "Your first name contains invalid charcters!<br />"; } // set first name maximum length if ($first_name && !preg_match('/[^a-zA-Z0-9\s]/', $first_name)) { if (strlen($first_name) >25) { echo "The maximum length for first name is 25 characters!<br />"; } } Iv wrote it like this so that i can get multiple error messages appearing at the same time. When it comes to writing the form data to the database im using the following: if ($first_name && !preg_match('/[^a-zA-Z\s]/', $first_name) && strlen($first_name) <26 && $last_name && !preg_match('/[^a-zA-Z\s]/', $last_name) && strlen($last_name) <26 && $dob_day != "Day" && $dob_month != "Month" && $dob_year != "Year" && $gender != "Select" && $email && filter_var($email, FILTER_VALIDATE_EMAIL) && $confirm_email && $email == $confirm_email && $email_count == 0 && $town && !preg_match('/[^a-zA-Z0-9\s]/', $town) && strlen($town) < 26 && $location != "Select" && $postcode && !preg_match('/[^a-zA-Z0-9\s]/', $postcode) && $username && !preg_match('/[^a-zA-Z\s]/', $username) && strlen($username) >2 && strlen($username) <25 && $username_count == 0 && $password && strlen($password) >5 && strlen($password) <26 && $confirm_password && $password == $confirm_password) { // code to write to database here } I was wondering if there was a better way to confirm that all the form data is ok, and if so write the data to the database? Im new to php and would love to learn a way to make this easier instead of typing out loads of lines of code. Thanks to anyone that can help me. Quote Link to comment https://forums.phpfreaks.com/topic/229619-looking-for-a-better-way-to-insert-values-into-database/ Share on other sites More sharing options...
Pikachu2000 Posted March 4, 2011 Share Posted March 4, 2011 Store your field validation errors in an array, then if the array is empty(), insert the record into the DB. Alternately, you could set a 'flag' variable if a field doesn't validate that would not allow the insert if it's present. Quote Link to comment https://forums.phpfreaks.com/topic/229619-looking-for-a-better-way-to-insert-values-into-database/#findComment-1183046 Share on other sites More sharing options...
doddsey_65 Posted March 4, 2011 Share Posted March 4, 2011 example for above: $error = array(); if (!$first_name) { $error[] = "Please enter your first name!"; } if(empty($error)) { // code to write to database here } else { echo explode('<br />', $error); } Quote Link to comment https://forums.phpfreaks.com/topic/229619-looking-for-a-better-way-to-insert-values-into-database/#findComment-1183059 Share on other sites More sharing options...
perky416 Posted March 5, 2011 Author Share Posted March 5, 2011 Thanks for info both of you, however when iv tried to implement it using the example, when the form fields are empty the only error message im getting is "Array". <?php $first_name = $_POST['first_name']; $submit = $_POST['submit']; if ($submit) { $error = array(); if (!$first_name) { $error[] = "Please enter your first name!"; } if(empty($error)) { // code to write to database here } else { echo explode('<br />', $error); } } ?> <html> <form action="test.php" method="POST"> <input type="text" name="first_name" /> <input type="submit" name="submit" /> </form> </html> Have i done it correctly? Iv never used an array before. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/229619-looking-for-a-better-way-to-insert-values-into-database/#findComment-1183069 Share on other sites More sharing options...
Pikachu2000 Posted March 5, 2011 Share Posted March 5, 2011 'explode' should be 'implode'. Quote Link to comment https://forums.phpfreaks.com/topic/229619-looking-for-a-better-way-to-insert-values-into-database/#findComment-1183076 Share on other sites More sharing options...
doddsey_65 Posted March 5, 2011 Share Posted March 5, 2011 sorry, my bad hes right Quote Link to comment https://forums.phpfreaks.com/topic/229619-looking-for-a-better-way-to-insert-values-into-database/#findComment-1183081 Share on other sites More sharing options...
perky416 Posted March 5, 2011 Author Share Posted March 5, 2011 PERFECT!!!!! Thanks so much for your help. You have shown me a much better and more efficient way to write my script. I really appreciate it. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/229619-looking-for-a-better-way-to-insert-values-into-database/#findComment-1183213 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.