Page 1 of 1

How To Get My Code Working?

Posted: Mon Oct 20, 2014 12:35 pm
by Hiver75

Code: Select all

<?php
if(isset($_POST['send'])) {
    
if($newmoney > -1);  {
    $error .= "You don't have enough money.";
    echo "You don't have enough money.";
    } if ($pcboughton = 1); {
        $error .="You have already brought a refinery today.";
        echo "You have already brought a refinery today.";
    }
    
if($error == null) {
mysql_query("UPDATE players SET commercialprocessing='$newcomplants', pcbought='$pcboughton', money='$newmoney' WHERE id='$id'");
echo "You have constructed a Commercial Refinery. You now have ".number_format($newcomplants)." Commercial Refineries and $".number_format($newmoney)." in cash.<br><center><a href='default.php?id=7'>View Nation</a></center>";
 }   if($error != null);
echo $error;
}
?>
This code basically should allow you to click on a button, the game then searches to see if you have enough money and if you have brought one of these items and then proceeds to give you it, if you meet the conditions, if not, it should fire an error message. That error message doesn't seem to be working, how would I get that and thing working exactly?

Re: How To Get My Code Working?

Posted: Mon Oct 20, 2014 1:55 pm
by requinix
If you put a semicolon after an if() then you end the if immediately. The {} code will always execute.

Get rid of those semicolons.

Re: How To Get My Code Working?

Posted: Mon Oct 20, 2014 2:22 pm
by Hiver75
I've changed this part to this

Code: Select all

  if($error != null)
, by removing the semi colon. Is there any others I have to remove, as it still isn't working?

Re: How To Get My Code Working?

Posted: Mon Oct 20, 2014 2:28 pm
by requinix
Yes, there are others. There were three.

Re: How To Get My Code Working?

Posted: Mon Oct 20, 2014 2:50 pm
by Hiver75

Code: Select all

<?php
if(isset($_POST['send'])) {
    
if($newmoney > -1)  {
    $error .= "You don't have enough money.";
    } if ($pcboughton = 1) {
        $error .="You have already brought a refinery today.";
    }
    
if($error == null) {
mysql_query("UPDATE players SET commercialprocessing='$newcomplants', pcbought='$pcboughton', money='$newmoney' WHERE id='$id'");
echo "You have constructed a Commercial Refinery. You now have ".number_format($newcomplants)." Commercial Refineries and $".number_format($newmoney)." in cash.<br><center><a href='default.php?id=7'>View Nation</a></center>";
 }   if($error != null)
echo $error;
}
?>
I've updated the code to the following and for some reason it still won't echo the $error bit.

Re: How To Get My Code Working?

Posted: Mon Oct 20, 2014 2:59 pm
by requinix
I assumed you only posted part of the code...

Where are $newmoney and $pcboughton and $newcomplaints and $id coming from? If those aren't set then $newmoney > -1 is false, $error will be null, the query won't affect anything and you'll get the "You have constructed..." message with 0 refineries and $0 in cash.

Re: How To Get My Code Working?

Posted: Mon Oct 20, 2014 3:59 pm
by Hiver75
Yes, they're all at the top of the page, not in the part I've posted. No, it doesn't even echo the message at all, when I can't buy a refinery here.

Re: How To Get My Code Working?

Posted: Mon Oct 20, 2014 5:44 pm
by requinix
With that code, as long as $_POST[send] is set, the code must necessarily output something. When you say it won't "echo the $error bit", are you saying it does show the success message and the problem is it that happens every time? Posting the rest of the code would be nice too.

Side note, just noticed:

Code: Select all

if ($pcboughton = 1) {
Missing a =

Re: How To Get My Code Working?

Posted: Wed Oct 22, 2014 8:25 pm
by requinix

Code: Select all

if(isset($_POST['send'])) {
You're looking for a button named "send" but according to the code you sent me the button is actually named "submit".