jackie11 Posted January 23, 2007 Share Posted January 23, 2007 Hi everyoneI am looking for a little help getting my my form to validate and insert info into my db, I am first trying to validate a form submitted by a user, if an error is found, error messages are displayedIf no errors are found it prints what info they have sucessfully added to the dbeverything works until I add in the validation script then everything goes pear shaped can anyone have a look at my script and see where I am going wrongIt would be most appriciated Jackie[code]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <title>Employee Index</title></head><body><h1 style="text-align: center;"><img src="etc/Logo_pic_1.JPG"></h1><hr style="width: 100%; height: 2px;"><?php $user="root";$host="localhost";$password="";$database = "employee_index";mysql_connect("localhost", "root", "") or die(mysql_error());mysql_select_db("employee_index") or die(mysql_error());if (isset($_POST['Surname'])) {$errors = array();if(empty($_POST['Office_location'])){echo "The Office Location field is empty, try again!<br>\n";}if(empty($_POST['Expertise'])){echo "The Expertise field is empty, try again!<br>\n";}if(empty($_POST['Hobbies'])){echo "The Hobbies field is empty, try again! <br>\n";}if(empty($_POST['Surname'])){echo "Surname field is empty, try again! <br>\n";}if(empty($_POST['Forename'])){echo "Forename field is empty, try again! <br>\n";}if(empty($_POST['Job_title'])){echo "Job Title field is empty, try again! <br>\n";}if(empty($_POST['DOB'])){echo "DOB field is empty, try again!<br>\n";}if(empty($_POST['Telephone'])){echo "Telephone field is empty, try again!<br>\n";}if(empty($_POST['Email'])){echo "Email field is empty, try again!<br><br>\n";} if (!preg_match('|^[a-z]+$|i', $_POST['Surname'])) $errors[] = "Surname can contain only letters!"; if (!preg_match('|^[a-z]+$|i', $_POST['Forename'])) $errors[] = "Forename can contain only letters!"; if (!preg_match('|^[0-9]+$|', $_POST['Telephone'])) $errors[] = "Telephone can contain only digits!";if (!preg_match('|^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$|i', $_POST['Email'])) $errors[] = "You must provide a valid email address!";if (!preg_match('|^[0-9]+$|', $_POST['Office_location'])) $errors[] = "Office Location contain only digits!"; if (count($errors) < 1) { } else {$msg = "<p class=\"error\">The following errors have occurred: " . implode('<br />', $errors) . "</p>\n"; }}echo isset($msg) ? $msg : '';} else {$imagename = basename($_FILES['Picture']['name']);$sqlquery = "INSERT INTO employee (Surname, Forename, Job_title, Office_location, Telephone, Email, Expertise, Hobbies, DOB, Picture)VALUES ('$_POST[Surname]','$_POST[Forename]','$_POST[Job_title]','$_POST[Office_location]','$_POST[Telephone]','$_POST[Email]','$_POST[Expertise]','$_POST[Hobbies]','$_POST[DOB]','$imagename')";$result = mysql_query($sqlquery)or die (mysql_error());print "<html><body><center>";print "<p><B>Thank You - You have just entered the following information successfully!</B><p>";print "Surname : $_POST[Surname]<br>";print "Forename : $_POST[Forename]<br>";print "Job Titile : $_POST[Job_title] <br>";print "Office Location : $_POST[Office_location]<br>";print "Telephone : $_POST[Telephone] <br>";print "Email :$_POST[Email] <br>";print "Expertise : $_POST[Expertise]<br>";print "Hobbies : $_POST[Hobbies] <br>";print "DOB : $_POST[DOB]<br>";print "</body></html>";$target_path = "../test_2/pics/";$target_path = $target_path . basename( $_FILES['Picture']['name']); if(move_uploaded_file($_FILES['Picture']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['Picture']['name']). " has been uploaded";} else{ echo "There was an error uploading your picture file, please try again!";}}mysql_close();?><hr style="width: 100%; height: 2px;"><p style="text-align: center;"><a href="index.html"><img style="border: 0px solid ; width: 94px; height: 47px;" alt="Home" src="etc/Home_button.JPG"></a></p><hr style="width: 100%; height: 2px;"></body></html>[/code] Link to comment https://forums.phpfreaks.com/topic/35395-problems-validating-form-info-before-inserting-into-db/ Share on other sites More sharing options...
Orio Posted January 23, 2007 Share Posted January 23, 2007 Try replacing the "|" with "/". I never seen the preg delimiters as "|".Examples:[code]|^[0-9]+$| becomes /^[0-9]+$/|^[a-z]+$|i becomes /^[a-z]+$/i[/code]Orio. Link to comment https://forums.phpfreaks.com/topic/35395-problems-validating-form-info-before-inserting-into-db/#findComment-167382 Share on other sites More sharing options...
simcoweb Posted January 23, 2007 Share Posted January 23, 2007 Personally i've not seen where the line can end with a curly bracket instead of the required ;Here's the format I use for errors and it never has an issue:[code]<?phpif ($username=="") { $err.= "Please provide a username<br/>"; } if (!$email) { $err.= "Please provide your email address<br>"; } if ($email) { if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) { $err.= $email. " is not a valid email address.<br/>"; } } if ($password=="") { $err.= "Please provide password<br/>"; } if ($confirmPass=="") { $err.= "Please confirm your password.<br/>"; } if ($confirmPass != $password) { $err.= "Your passwords do not match. Please re-enter your passwords."; }?>[/code]etc. etc. Notice it is checking several things like IF it's empty or IF it doesn't match, etc. Link to comment https://forums.phpfreaks.com/topic/35395-problems-validating-form-info-before-inserting-into-db/#findComment-167390 Share on other sites More sharing options...
jackie11 Posted January 23, 2007 Author Share Posted January 23, 2007 Thanks for the replies, however the if statements and validations themselves work ok as I have used them in another form, the problem I am having is linking them with inserting the info into the db,i.e. - if errors are detected display error message and no info is inserted into dbor - if no errors detected insert info into db and display print ststaements of what has been insertedany suggestions?Jackie Link to comment https://forums.phpfreaks.com/topic/35395-problems-validating-form-info-before-inserting-into-db/#findComment-167404 Share on other sites More sharing options...
Orio Posted January 23, 2007 Share Posted January 23, 2007 Well, you had an empty if over there, try this:[code]<?php//validation...if (count($errors) > 1){ $msg = "<p class=\"error\">The following errors have occurred: " . implode('<br />', $errors) . "</p>\n"; echo $msg;}else{ $imagename = basename($_FILES['Picture']['name']); $sqlquery = "INSERT INTO employee (Surname, Forename, Job_title, Office_location, Telephone, Email, Expertise, Hobbies, DOB, Picture) VALUES ('$_POST[Surname]','$_POST[Forename]','$_POST[Job_title]','$_POST[Office_location]','$_POST[Telephone]','$_POST[Email]', '$_POST[Expertise]','$_POST[Hobbies]','$_POST[DOB]','$imagename')"; $result = mysql_query($sqlquery) or die (mysql_error()); print "<html><body><center>"; print "<p><B>Thank You - You have just entered the following information successfully!</B><p>"; print "Surname : $_POST[Surname]<br>"; print "Forename : $_POST[Forename]<br>"; print "Job Titile : $_POST[Job_title] <br>"; print "Office Location : $_POST[Office_location]<br>"; print "Telephone : $_POST[Telephone] <br>"; print "Email :$_POST[Email] <br>"; print "Expertise : $_POST[Expertise]<br>"; print "Hobbies : $_POST[Hobbies] <br>"; print "DOB : $_POST[DOB]<br>"; print "</body></html>"; $target_path = "../test_2/pics/"; $target_path = $target_path . basename( $_FILES['Picture']['name']); if(move_uploaded_file($_FILES['Picture']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['Picture']['name']). " has been uploaded"; } else { echo "There was an error uploading your picture file, please try again!"; }}mysql_close();?><hr style="width: 100%; height: 2px;"><p style="text-align: center;"><a href="index.html"><img style="border: 0px solid ; width: 94px; height: 47px;" alt="Home" src="etc/Home_button.JPG"></a></p><hr style="width: 100%; height: 2px;"></body></html>[/code]Orio. Link to comment https://forums.phpfreaks.com/topic/35395-problems-validating-form-info-before-inserting-into-db/#findComment-167408 Share on other sites More sharing options...
simcoweb Posted January 23, 2007 Share Posted January 23, 2007 Ok, I think I understand. This bit of code confuses me, however:[code]<?php if (count($errors) < 1) { } else {$msg = "<p class=\"error\">The following errors have occurred: " . implode('<br />', $errors) . "</p>\n"; }}?>[/code]Basically what it's saying is IF the number of errors is less than 1... blah blah. But the blah blah doesn't get to happen because you immediately 'else' it. It's in place of the 'else' that you would execute your error display code then after that you'd 'else' it. Link to comment https://forums.phpfreaks.com/topic/35395-problems-validating-form-info-before-inserting-into-db/#findComment-167415 Share on other sites More sharing options...
simcoweb Posted January 23, 2007 Share Posted January 23, 2007 I need to type faster, Orio ;) Link to comment https://forums.phpfreaks.com/topic/35395-problems-validating-form-info-before-inserting-into-db/#findComment-167417 Share on other sites More sharing options...
jackie11 Posted January 23, 2007 Author Share Posted January 23, 2007 HiI have changed the code to: [code]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <title>Employee Index</title></head><body><h1 style="text-align: center;"><img src="etc/Logo_pic_1.JPG"></h1><hr style="width: 100%; height: 2px;"><?php $user="root";$host="localhost";$password="";$database = "employee_index";mysql_connect("localhost", "root", "") or die(mysql_error());mysql_select_db("employee_index") or die(mysql_error());if (isset($_POST['Surname'])) {$errors = array();if(empty($_POST['Office_location'])){echo "The Office Location field is empty, try again!<br>\n";}if(empty($_POST['Expertise'])){echo "The Expertise field is empty, try again!<br>\n";}if(empty($_POST['Hobbies'])){echo "The Hobbies field is empty, try again! <br>\n";}if(empty($_POST['Surname'])){echo "Surname field is empty, try again! <br>\n";}if(empty($_POST['Forename'])){echo "Forename field is empty, try again! <br>\n";}if(empty($_POST['Job_title'])){echo "Job Title field is empty, try again! <br>\n";}if(empty($_POST['DOB'])){echo "DOB field is empty, try again!<br>\n";}if(empty($_POST['Telephone'])){echo "Telephone field is empty, try again!<br>\n";}if(empty($_POST['Email'])){echo "Email field is empty, try again!<br><br>\n";} if (!preg_match('|^[a-z]+$|i', $_POST['Surname'])) $errors[] = "Surname can contain only letters!"; if (!preg_match('|^[a-z]+$|i', $_POST['Forename'])) $errors[] = "Forename can contain only letters!"; if (!preg_match('|^[0-9]+$|', $_POST['Telephone'])) $errors[] = "Telephone can contain only digits!";if (!preg_match('|^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$|i', $_POST['Email'])) $errors[] = "You must provide a valid email address!";if (!preg_match('|^[0-9]+$|', $_POST['Office_location'])) $errors[] = "Office Location contain only digits!"; if (count($errors) > 1){ $msg = "<p class=\"error\">The following errors have occurred: " . implode('<br />', $errors) . "</p>\n"; echo $msg;}else{ $imagename = basename($_FILES['Picture']['name']); $sqlquery = "INSERT INTO employee (Surname, Forename, Job_title, Office_location, Telephone, Email, Expertise, Hobbies, DOB, Picture) VALUES ('$_POST[Surname]','$_POST[Forename]','$_POST[Job_title]','$_POST[Office_location]','$_POST[Telephone]','$_POST[Email]', '$_POST[Expertise]','$_POST[Hobbies]','$_POST[DOB]','$imagename')"; $result = mysql_query($sqlquery) or die (mysql_error()); print "<html><body><center>"; print "<p><B>Thank You - You have just entered the following information successfully!</B><p>"; print "Surname : $_POST[Surname]<br>"; print "Forename : $_POST[Forename]<br>"; print "Job Titile : $_POST[Job_title] <br>"; print "Office Location : $_POST[Office_location]<br>"; print "Telephone : $_POST[Telephone] <br>"; print "Email :$_POST[Email] <br>"; print "Expertise : $_POST[Expertise]<br>"; print "Hobbies : $_POST[Hobbies] <br>"; print "DOB : $_POST[DOB]<br>"; print "</body></html>"; $target_path = "../test_2/pics/"; $target_path = $target_path . basename( $_FILES['Picture']['name']); if(move_uploaded_file($_FILES['Picture']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['Picture']['name']). " has been uploaded"; } else { echo "There was an error uploading your picture file, please try again!"; }}mysql_close();?><hr style="width: 100%; height: 2px;"><p style="text-align: center;"><a href="index.html"><img style="border: 0px solid ; width: 94px; height: 47px;" alt="Home" src="etc/Home_button.JPG"></a></p><hr style="width: 100%; height: 2px;"></body></html>[/code]but I am now getting the error message:Parse error: parse error, unexpected $end in C:\Program Files\xampp\htdocs\test_2\script_1.php on line 105Any suggestions???Jackie Link to comment https://forums.phpfreaks.com/topic/35395-problems-validating-form-info-before-inserting-into-db/#findComment-167435 Share on other sites More sharing options...
Orio Posted January 23, 2007 Share Posted January 23, 2007 There was a missing closing bracket.[code]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <title>Employee Index</title></head><body><h1 style="text-align: center;"><img src="etc/Logo_pic_1.JPG"></h1><hr style="width: 100%; height: 2px;"><?php $user="root";$host="localhost";$password="";$database = "employee_index";mysql_connect("localhost", "root", "") or die(mysql_error());mysql_select_db("employee_index") or die(mysql_error());if (isset($_POST['Surname'])){ $errors = array(); if(empty($_POST['Office_location'])){echo "The Office Location field is empty, try again!<br>\n";} if(empty($_POST['Expertise'])){echo "The Expertise field is empty, try again!<br>\n";} if(empty($_POST['Hobbies'])){echo "The Hobbies field is empty, try again! <br>\n";} if(empty($_POST['Surname'])){echo "Surname field is empty, try again! <br>\n";} if(empty($_POST['Forename'])){echo "Forename field is empty, try again! <br>\n";} if(empty($_POST['Job_title'])){echo "Job Title field is empty, try again! <br>\n";} if(empty($_POST['DOB'])){echo "DOB field is empty, try again!<br>\n";} if(empty($_POST['Telephone'])){echo "Telephone field is empty, try again!<br>\n";} if(empty($_POST['Email'])){echo "Email field is empty, try again!<br><br>\n";} if (!preg_match('|^[a-z]+$|i', $_POST['Surname'])) $errors[] = "Surname can contain only letters!"; if (!preg_match('|^[a-z]+$|i', $_POST['Forename'])) $errors[] = "Forename can contain only letters!"; if (!preg_match('|^[0-9]+$|', $_POST['Telephone'])) $errors[] = "Telephone can contain only digits!"; if (!preg_match('|^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$|i', $_POST['Email'])) $errors[] = "You must provide a valid email address!"; if (!preg_match('|^[0-9]+$|', $_POST['Office_location'])) $errors[] = "Office Location contain only digits!"; if (count($errors) > 1) { $msg = "<p class=\"error\">The following errors have occurred: " . implode('<br />', $errors) . "</p>\n"; echo $msg; } else { $imagename = basename($_FILES['Picture']['name']); $sqlquery = "INSERT INTO employee (Surname, Forename, Job_title, Office_location, Telephone, Email, Expertise, Hobbies, DOB, Picture) VALUES ('$_POST[Surname]','$_POST[Forename]','$_POST[Job_title]','$_POST[Office_location]','$_POST[Telephone]','$_POST[Email]', '$_POST[Expertise]','$_POST[Hobbies]','$_POST[DOB]','$imagename')"; $result = mysql_query($sqlquery) or die (mysql_error()); print "<html><body><center>"; print "<p><B>Thank You - You have just entered the following information successfully!</B><p>"; print "Surname : $_POST[Surname]<br>"; print "Forename : $_POST[Forename]<br>"; print "Job Titile : $_POST[Job_title] <br>"; print "Office Location : $_POST[Office_location]<br>"; print "Telephone : $_POST[Telephone] <br>"; print "Email :$_POST[Email] <br>"; print "Expertise : $_POST[Expertise]<br>"; print "Hobbies : $_POST[Hobbies] <br>"; print "DOB : $_POST[DOB]<br>"; print "</body></html>"; $target_path = "../test_2/pics/"; $target_path = $target_path . basename( $_FILES['Picture']['name']); if(move_uploaded_file($_FILES['Picture']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['Picture']['name']). " has been uploaded"; } else { echo "There was an error uploading your picture file, please try again!"; } }}mysql_close();?><hr style="width: 100%; height: 2px;"><p style="text-align: center;"><a href="index.html"><img style="border: 0px solid ; width: 94px; height: 47px;" alt="Home" src="etc/Home_button.JPG"></a></p><hr style="width: 100%; height: 2px;"></body></html>[/code]Orio. Link to comment https://forums.phpfreaks.com/topic/35395-problems-validating-form-info-before-inserting-into-db/#findComment-167465 Share on other sites More sharing options...
jackie11 Posted January 23, 2007 Author Share Posted January 23, 2007 Thats great it works a treat, thank you all for all your help!!!Jackie :) :) :) :) :) :) :) :) Link to comment https://forums.phpfreaks.com/topic/35395-problems-validating-form-info-before-inserting-into-db/#findComment-167510 Share on other sites More sharing options...
simcoweb Posted January 23, 2007 Share Posted January 23, 2007 Jackie, you should mark this topic as 'Solved' :) Link to comment https://forums.phpfreaks.com/topic/35395-problems-validating-form-info-before-inserting-into-db/#findComment-167520 Share on other sites More sharing options...
Orio Posted January 23, 2007 Share Posted January 23, 2007 The "Topic Solved" mod is not available yet, since phpfreaks upgraded their SMF version.It will be soon :) I am glad I could help jackie.Orio. Link to comment https://forums.phpfreaks.com/topic/35395-problems-validating-form-info-before-inserting-into-db/#findComment-167522 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.