Kryllster Posted June 21, 2012 Share Posted June 21, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/264546-why-isnt-this-working/ Share on other sites More sharing options...
KevinM1 Posted June 21, 2012 Share Posted June 21, 2012 What does "does not work" mean? Error message? Data not being saved? Quote Link to comment https://forums.phpfreaks.com/topic/264546-why-isnt-this-working/#findComment-1355712 Share on other sites More sharing options...
Kryllster Posted June 21, 2012 Author Share Posted June 21, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/264546-why-isnt-this-working/#findComment-1355714 Share on other sites More sharing options...
Kryllster Posted June 21, 2012 Author Share Posted June 21, 2012 I figured out the problem it wasnt in the PHP it was in my html I forgot to close out the previous form so it is working fine now sorry to bother you. Quote Link to comment https://forums.phpfreaks.com/topic/264546-why-isnt-this-working/#findComment-1355716 Share on other sites More sharing options...
cpd Posted June 21, 2012 Share Posted June 21, 2012 Have you made any attempt to debug it and narrow it down... Quote Link to comment https://forums.phpfreaks.com/topic/264546-why-isnt-this-working/#findComment-1355722 Share on other sites More sharing options...
Kryllster Posted June 21, 2012 Author Share Posted June 21, 2012 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 Link to comment https://forums.phpfreaks.com/topic/264546-why-isnt-this-working/#findComment-1355723 Share on other sites More sharing options...
ManiacDan Posted June 21, 2012 Share Posted June 21, 2012 Well for one, nested forms are invalid HTML, you could remove those. Quote Link to comment https://forums.phpfreaks.com/topic/264546-why-isnt-this-working/#findComment-1355728 Share on other sites More sharing options...
Kryllster Posted June 21, 2012 Author Share Posted June 21, 2012 I did get rid of the nested forms! What else if anything can I do to shorten it? Quote Link to comment https://forums.phpfreaks.com/topic/264546-why-isnt-this-working/#findComment-1355907 Share on other sites More sharing options...
PFMaBiSmAd Posted June 22, 2012 Share Posted June 22, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/264546-why-isnt-this-working/#findComment-1356021 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.