I have created a php form that connects to a MySQL database. This is working fine however I have added validation and this doesn't appear to be working correctly and still posts to the results to the database regardless of if they are validated or not. Any ideas?

Recommended Answers

All 2 Replies

// define variables and set to empty values
$nameErr = $lnameErr = $ageErr = "";
$name = $lname = $age = "";
 if (empty($_POST["name"])) {
     $nameErr = "Name is required";
      } else {
$name = test_input($_POST["name"]);
   // check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
  $nameErr = "Only letters and white space allowed";
if (empty($_POST["lname"])) {
$lnameErr = "Last Name is required";
  } else {
$lname = test_input($_POST["lname"]);
  if (empty($_POST["age"])) {
$age = "";
  } else {
$age = test_input($_POST["age"]);


So sorry for the delay in replying to this. Can you show us the code for test_input()? Is that the function you're saying is failing? What input are you entering and what error are you expecting to see?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.