Jump to content

Why isnt this working?


Kryllster

Recommended Posts

I have some code for a buying script which allows a user to buy food it is basically just like another script i have but does not work and I cant figure out why for the life of me. Here is the code.

 

case 'food':

		$unit = 0 + $_POST['food'];
		$foodcost = $unit * 2;
		$limit = 0 + 5;
		$allowed = 0 + 10;


	// check for money if they dont have it let them know else go with transaction
	if($row['decasols'] < '2'){
		header("Location: main.php?page=warnmoney");
		exit();
	}
	if($unit > $limit){
		header("Location: main.php?page=warnmax");
		exit();
	}
	if($row['food'] >= $allowed){
		header("Location: main.php?page=warnamount");
		exit();
	}

	else{
	// update database
	$sql="UPDATE $tbl_name SET
    	decasols = decasols - $foodcost, 
	food = food + $unit
	WHERE username='$username'";
	mysql_query($sql) or die (mysql_error()."<p>$sql</p>");
	header("Location: main.php?page=arrakeen");
	exit();
}
	break;

 

Any Help would be appreciated Thanks in Advance!

Link to comment
https://forums.phpfreaks.com/topic/264546-why-isnt-this-working/
Share on other sites

The data is not being saved and I keep going to the warnamount page. I have a script before this and it works fine and its the same script.

 

case 'water':

	$unit = 0 + $_POST['water'];
	$watercost = $unit * 2;
	$limit = 0 + 3;
	$allowed = 0 + 10;

	// check for money if they dont have it let them know else go with transaction
	if($row['decasols'] < '2'){
		header("Location: main.php?page=warnmoney");
		exit();
	}
	if($unit > $limit){
		header("Location: main.php?page=warnmax");
		exit();
	}
	if($row['water'] >= $allowed){
		header("Location: main.php?page=warnamount");
		exit();
	}
	else{
	// Update database
	$sql="UPDATE $tbl_name SET
    	decasols = decasols - $watercost, 
	water = water + $unit
	WHERE username='$username'";
	mysql_query($sql) or die (mysql_error()."<p>$sql</p>");
	header("Location: main.php?page=arrakeen");
	exit();
	}
	break;

 

This code which is directly before the other works fine!???

I dont understand.

Well if you mean finding where the problem is or was it was in my html form I had 6 nested forms when I closed them out the script worked fine. I would like to know how I could shorten the code. I understand how some things work I just know very little but if there is more about this script i can streamline id like to know. Thanks.

  Quote
What else if anything can I do to shorten it?

 

If you mean a bunch of switch/case statements that all repeat the same code, but just use different values, yes, you would generally use an array to hold the different sets of data values, select the proper set of data based on the input selection, then have just one set of code to process the selected values.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.