PHP $_GET and IF statement help

Hey all

Can you give me advice on this statement i have made… it doesn’t work like i want it too. but from what i see it makes sense

if ($_GET[‘additem’] == ‘yes’) {

$productid = $_GET[‘pid’];
$pdescript = $_GET[‘pdesc’];
$tblname = $_GET[‘tblname’];
$phead = $_GET[‘phead’];
$price = $_GET[‘price’];

if ($productid == ‘’) {
$response = 1;
}

Else if ($pdescript == ‘’) {
$response = 1;
}
Else if ($tblname == ‘’) {
$response = 1;
}
Else if ($phead == ‘’) {
$response = 1;
}
Else if ($price == ‘’) {
$response = 1;
}
Else if ($img == ‘’) {
$response = 1;
}
ELSE {
$response = 0;
}
}

it’s for a loop and validation checking.

when they post. it loops onto the same page with $_SERVER[‘PHP_SELF’]

the form method is get. and it is checking if any of the fields are blank. (hence the big IF statement). if any of the fields are blank. it should activate $response = 1;

and if ($response == 1) {
echo ‘You left a Required field blank.’;
}

ELSE {

(mysql update query etc…)
}

but even when i have all the fields entered… it still makes $response = 1;
and then displays that message…
can anyone shed some light?

it is an IF statement inside an IF statement. i don’t know if that works but it made logical sense to me.

i.e. check if $_GET[‘additem’] = true then check if any of the fields have been left blank.

thanks in advance
Brendan

Something like this:


<?php
$error = 0;
$required = array('pid', 'pdesc', 'tblname', 'phead', 'price');
foreach($required as $value){
	if($_GET[$value] == ''){
		$error += 1;
	}
}
if($error > 0){
	echo 'You left a Required field blank.'; 
}
else{
	//mysql_query([*query*]) etc
}

that works perfectly thank you mudshark. very much appreciated.
any reason as to why my way didn’t work?

maybe i won’t make the same mistake in the future
cheers

Maybe because you didn’t define $img

Well I didn’t really have a long look at your code as I found it rather cumbersome. Did notice though this:


Else if ($img == '') {

$img has not defined previously, so I suppose that’s what would make $response return 1 every time.

yeah i didn’t put that into the code because the statement i used to get the $img worked well. but here it is. I’m using the same one with your script.

the reason i have this is because there is a radio selector which lets them leave the default image name. or use their own.
here it is.

if (($_GET[‘img’]) == ‘’) {
$img = $_GET[‘img1’];
}
Else {
$img = $_GET[‘img’];
}

and that works properly.
img is the value of the selector name ‘img’ img 1 is the name of the text field they enter their own image name into.

any other ideas??

Are you using GET or POST?

form method = “get”
cheers