error parsing - returning wrong error.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sh33p1985
Forum Commoner
Posts: 78
Joined: Thu Mar 11, 2004 9:22 am

error parsing - returning wrong error.

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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;   
}
?>
sh33p1985
Forum Commoner
Posts: 78
Joined: Thu Mar 11, 2004 9:22 am

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

0 will return false when passed to !empty()
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Are you type casting or intval()'ing before you send the information to the function?
sh33p1985
Forum Commoner
Posts: 78
Joined: Thu Mar 11, 2004 9:22 am

Post 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.
Post Reply