I have a php script running which is checking that information in a form has been entered and that the form hasn't been by passed however when submit is clicked and the script is run I get the last error message saying 'You must complete the form' even though the form is completed, can anyone spot an error anywhere?

<?PHP
if (isset($_POST['title']) && isset($_POST['director']) && isset($_POST['certificate']) && isset($_POST['category']) && isset($_POST['format']) && isset($_POST['condition']) && isset($_POST['description']) && isset($_POST['image']))//returns true if the variable exsists, to see if the user has bypassed the form
{
	$title = trim($_POST['title']);
	$director = trim($_POST['director']);
	$cert_id = trim($_POST['certificate']);
	$format_id = trim($_POST['format']);
	$condtion_id = trim($_POST['condition']);
	$description = trim($_POST['description']);
	$iamge = trim($_POST['image']);

	if ($title != "" && $director != "" && $cert_id != "" && $format_id != "" && $condition_id != "" && $description != "" && $image != "")//checking if the fields have been filled in by the user
	{
		$sql_addProduct = "INSERT INTO products (title, director, cert_id, cat_id, format_id, condition_id, user_id, description, image, date_added) VALUES ('$title', '$director', '$cert_id', '$cat_id', '$format_id', '$condition_id', `user_id`, '$description', '$image', NOW()) WHERE `user_id` = '".$_SESSION['user_id']."'";
		$queryresult_add = mysql_query($sql_addProduct);

		echo"<h1>Record Added</h1>";
	}
	else
	{
		echo "<p>You have not entered all of the required fields </p>\n";}//If a field has not been entered then this message will be displayed
	}
	else
	{
		echo "<p>You must complete the form</p>\n";
	}//If the user has bypassed the form then this message will be displayed
?>

Recommended Answers

All 8 Replies

Consider the code below. I usually like to store all errors relating to form processing in an array and then display all to the user at once.

<?php
if(isset($_POST['submit']))
{
	//The submit button has been clicked, validate form
	$my_errors = array();//Array to store all errors
	if(empty($_POST['title']))
	{
		$my_errors[] = "Title field is empty.";
	}
	if(empty($_POST['director']))
	{
		$my_errors[] = "Director field is empty.";
	}
	if(empty($_POST['certificate']))
	{
		$my_errors[] = "Certificate field is empty.";
	}
	if(empty($_POST['category']))
	{
		$my_errors[] = "Category field is empty.";
	}
	if(empty($_POST['format']))
	{
		$my_errors[] = "Format field is empty.";
	}
	if(empty($_POST['condition']))
	{
		$my_errors[] = "Condition field is empty.";
	}
	if(empty($_POST['descriptin']))
	{
		$my_errors[] = "Description field is empty.";
	}
	if(empty($_POST['image']))
	{
		$my_errors[] = "Image field is empty.";
	}
	
	if(sizeof($my_errors) > 0)
	{
		//At least one error occurred during form validation, print errors in array
		echo "Operation failed because of the following error(s):<br>";
		echo "<ul>";
			for($counter = 0; $counter < sizeof($my_errors); $counter++)
			{
				echo "<li>".$my_errors[$counter]."</li>";
			}
		echo "</ul>";
		echo "<p>Please correct the above error(s) and try again";
	}
	else
	{
		//No error occurred during form validation, proceed with form processing
		
	
	}
}
else
{
	//User has not clicked on the submit button, display form
}

?>

On line TEN of your original post you misspelled $image . You have $iamge = trim($_POST['image']);

On line 8 you mispelled

$condtion_id = trim($_POST['condition']);

cossay I like your method of validation, would the if statements differ if it was validating from a drop down box?

if(empty($_POST['category']))
	{
		$my_errors[] = "Category field is empty.";
	}

for example the category is selected from a drop down box

cossay I like your method of validation, would the if statements differ if it was validating from a drop down box?

if(empty($_POST['category']))
	{
		$my_errors[] = "Category field is empty.";
	}

for example the category is selected from a drop down box

After checking if empty you could use array_map() to trim, convert characters and so on... here's an example:

$_POST = array_map('strip_tags', $_POST);
$_POST = array_map('htmlentities', $_POST);
$_POST = array_map('htmlspecialchars', $_POST);
$_POST = array_map('stripslashes', $_POST);
$_POST = array_map('trim', $_POST);

...

It should be the same.

Be a part of the DaniWeb community

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