Page 1 of 1

error parsing - returning wrong error.

Posted: Mon Oct 30, 2006 11:57 am
by sh33p1985
ive created a simple function to check whether the fields in a submitted form are empty and also that some of them are of the correct format:

Code: Select all

function checkFields($productName, $productDesc, $unitPrice, $stockLevel){
	//Check for empty fields
	if(empty($productName) || empty($productDesc) || empty($unitPrice) || empty($stockLevel)){
		header ("location: ../add_product.php?error=1");
		exit;	
	}	
	//Check formats
	if(!is_double($unitPrice)){
		header ("loation: ../add_product.php?error=2");
		exit;
	}
	if(!is_integer($stockLevel)){
		header ("loation: ../add_product.php?error=3");
		exit;
	}	
}
ive tested this by entering some test data, all valid except for inputting a string instead of an integer in the StockLevel textfield but instead of getting the format error, i get the empty field error and im having a hard time correcting it.

Posted: Mon Oct 30, 2006 12:16 pm
by John Cartwright
I'm a bit confused.. try a var_dump() on all your variables to see what they contain and post back here please.

Posted: Mon Oct 30, 2006 12:20 pm
by RobertGonzalez
Are you passing data for the rest of the fields? If any of them are empty it gets redirected to error 1...

Code: Select all

<?php
if ( empty($productName) || empty($productDesc) || empty($unitPrice) || empty($stockLevel) ) 
{
    header ("location: ../add_product.php?error=1");
    exit;   
}
?>

Posted: Mon Oct 30, 2006 12:20 pm
by sh33p1985
string(1) "1" string(1) "1" float(9.99) int(0)

heres the problem and im 95% of the solution too. bare with me 2 mins

Posted: Mon Oct 30, 2006 12:37 pm
by John Cartwright
0 will return false when passed to !empty()

Posted: Mon Oct 30, 2006 12:55 pm
by RobertGonzalez
Are you type casting or intval()'ing before you send the information to the function?

Posted: Mon Oct 30, 2006 1:04 pm
by sh33p1985
i was typecasting before the empty field check, which would result in $unitPrice and $stockLevel evaluating to false, ive seperated the methods now and im currently working on reimplementing it so it works properly.