How To Get My Code Working?

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
Hiver75
Forum Newbie
Posts: 6
Joined: Mon Oct 20, 2014 12:31 pm

How To Get My Code Working?

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How To Get My Code Working?

Post 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.
Hiver75
Forum Newbie
Posts: 6
Joined: Mon Oct 20, 2014 12:31 pm

Re: How To Get My Code Working?

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How To Get My Code Working?

Post by requinix »

Yes, there are others. There were three.
Hiver75
Forum Newbie
Posts: 6
Joined: Mon Oct 20, 2014 12:31 pm

Re: How To Get My Code Working?

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How To Get My Code Working?

Post 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.
Hiver75
Forum Newbie
Posts: 6
Joined: Mon Oct 20, 2014 12:31 pm

Re: How To Get My Code Working?

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How To Get My Code Working?

Post 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 =
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How To Get My Code Working?

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