Jump to content

Problems validating form info before inserting into db!


jackie11

Recommended Posts

Hi everyone

I 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 displayed
If no errors are found it prints what info they have sucessfully added to the db

everything 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 wrong

It 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
Share on other sites

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]<?php
if ($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
Share on other sites


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 db
or
- if no errors detected insert info into db and display print ststaements of what has been inserted


any suggestions?

Jackie
Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites


Hi

I 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 105

Any suggestions???
Jackie
Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.