aub Posted October 6, 2014 Share Posted October 6, 2014 (edited) I need to validate first name, last name, street, suburb, postcode, email, status and date of birth. I tried to do this validation but gave me heaps of error and I messed up more. if (empty($firstname)) {$errors[] =" First Name Can not be Empty <br> ";} if (empty($lastname)) {$errors[] =" Last Name Can not be Empty <br> ";} if (empty($street)) {$errors[] =" Street Can not be Empty <br> ";} if (empty($suburb)) {$errors[] =" Suburb Can not be Empty <br> ";} if (empty($postcode)) {$errors[] =" Postcode Can not be Empty <br> ";} // elseif (!is_numeric($postcode)) {$errors[] =" Postcode must be numeric ";} elseif(!preg_match("/\^\(\[0\-9\]\{5\}\(\[\-\\s\]\?\[0\-9\]\{4\}\)\?\)\$/", $postcode)) {$errors[] =" Please enter a valid post number <br> ";} if( !preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i", $myemail) ) {$errors[] =" You have entered and invalid email address <br> ";} if (empty($DOB)) {$errors[] =" Date only <br> ";} This is my full PHP/MySQL code: <html> <head> <style> body { background-color:#d0e4fe; } h1 { color:orange; text-align:center; } p { font-family:"Times New Roman"; font-size:20px; } table,th,td { border:1px solid black; } table { width:90%; } th { height:40px; } </style> </head> <body> <h1>All Students Information</h1> </body> </html> <?php function renderForm($first, $last, $st, $sub, $pcode, $em, $sta, $dofb, $error) { ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>Add Student</title> </head> <body> <?php if ($error != '') { echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; } echo "<p><b>Add new student details</b></p>"; ?> <form action="" method="post"> <div> <strong>First name: </strong> <input type="text" name="FNAME" value="<?php echo $first; ?>" /><br/> <strong>Last name: </strong> <input type="text" name="LNAME" value="<?php echo $last; ?>" /><br/> <strong>Street: </strong> <input type="text" name="STREET" value="<?php echo $st; ?>" /><br/> <strong>Suburb: </strong> <input type="text" name="SUBURB" value="<?php echo $sub; ?>" /><br/> <strong>Postcode: </strong> <input type="text" name="POSTCODE" value="<?php echo $pcode; ?>" /><br/> <strong>Email: </strong> <input type="text" name="EMAIL" value="<?php echo $em; ?>" /><br/> <strong>Status: </strong> <input type="text" name="STATUS" value="<?php echo $sta; ?>" /><br/> <strong>Date of Birth: </strong> <input type="text" name="DOB" value="<?php echo $dofb; ?>" /><br/> <p>Required Field</p> <input type="submit" name="submit" value="Submit"> </div> </form> <p><a href="view.php">Update & delete students</a></p> </body> </html> <?php } include('dbconnect.php'); if (isset($_POST['submit'])) { $firstname = mysql_real_escape_string(htmlspecialchars($_POST['FNAME'])); $lastname = mysql_real_escape_string(htmlspecialchars($_POST['LNAME'])); $street = mysql_real_escape_string(htmlspecialchars($_POST['STREET'])); $suburb = mysql_real_escape_string(htmlspecialchars($_POST['SUBURB'])); $postcode = mysql_real_escape_string(htmlspecialchars($_POST['POSTCODE'])); $email = mysql_real_escape_string(htmlspecialchars($_POST['EMAIL'])); $status = mysql_real_escape_string(htmlspecialchars($_POST['STATUS'])); $dob = mysql_real_escape_string(htmlspecialchars($_POST['DOB'])); if ($firstname == '' || $lastname == '' || $street == '' || $suburb == '' || $postcode == '' || $email == '' || $status == '' || $dob == '') { $error = 'Your field is empty'; renderForm($firstname, $lastname, $street, $suburb, $postcode, $email, $status, $dob, $error); } else { mysql_query("INSERT student SET FNAME='$firstname', LNAME='$lastname', STREET='$street', SUBURB='$suburb', POSTCODE='$postcode', EMAIL='$email', STATUS='$status', DOB='$dob' ") or die(mysql_error()); header("Location: view.php"); } } else { renderForm('','','','','','','','',''); } ?> For easy view, click the link below.http://codepaste.net/3usepx Your help would be greatly appreciated!Thank you. Edited October 6, 2014 by aub Quote Link to comment https://forums.phpfreaks.com/topic/291463-validation/ Share on other sites More sharing options...
cyberRobot Posted October 6, 2014 Share Posted October 6, 2014 What kind of errors are you seeing? Note that it will help to see the exact messages. Side note: In case you're not aware, PHP has a built-in function for validating email addresses here: http://php.net/manual/en/filter.examples.validation.php Quote Link to comment https://forums.phpfreaks.com/topic/291463-validation/#findComment-1492872 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.