Jump to content

[SOLVED] PHP Subtract Problem


Stalingrad

Recommended Posts

Okay. I've made a simple shops script for my website. There is one problem with it, though. When you go to buy an item and you don't have enopugh points to buy that item, it lets you buy the item, and gives you negative points. I don't want people to have negative points. I want to make it so that when they go to buy an item, if they don't have enough points, they get an error that they don't have enough points to buy the item. I'll post the code below. Any help is greatly appreciated; thank you in advance!

 

<?php
session_start();
include("config.php");
if($_SESSION['username'] ==  "" || $_SESSION['password'] == "") {
notloggedin();
}
if($_SESSION['username'] != "" && $_SESSION['password'] != "") {
start();
$action = $_GET['action'];
$shopinfo = mysql_query("SELECT * FROM shops WHERE shopid='$_GET[shopid]'");
while($shops = mysql_fetch_array($shopinfo)) {
$shopname = $shops['name'];
$shopid = $shops['shopid'];
$shopkeeper = $shops['shopkeeper'];
$greeting = $shops['greeting'];
} 
if($action != "buy") { 
echo "<font size=6>$shopname</font><br><br><table border=\"0\" bordercolor=\"white\" cellpadding=\"5\" cellspacing=\"0\">";
$i = 0;
$iteminfo = mysql_query("SELECT * FROM items WHERE shopid='$_GET[shopid]' AND amount != '0'");
while($items = mysql_fetch_array($iteminfo)) {
$itemname = $items['name'];
$itemid = $items['itemid'];
$itemimage = $items['image'];
$amount = $items['amount'];
$price = $items['price'];
if ($i % 4 == 0)
echo "<tr>";
echo "<td>$itemname<br><a href=shops.php?action=buy&itemid=$itemid><img src=images/items/$itemimage></a><br>Price: $price EDC<br>Amount: $amount</td>";
$i++;
}
echo "</table>";
}
$item_query = mysql_query("SELECT * FROM items WHERE itemid='$_GET[itemid]'");
while($row = mysql_fetch_array($item_query)) {
$checkprice = "$credits - $row[price]"; // here is the actual code for the checkprice variable
if($action == "buy") {
if($checkprice < 0) { // Here it is used to say they don't have enough credits
echo "Error!";
}
if($checkprice >= "0") { // Here it is used to say that they have enough vredits
mysql_query("UPDATE users SET credits=$credits - '$row[price]' WHERE username='$username'");
mysql_query("UPDATE items SET amount = $row[amount] - '1' WHERE itemid='$_GET[itemid]'");
mysql_query("INSERT INTO useritems (id, username, item, location) VALUES ('$_GET[itemid]', '$username', '$row[name]', 'Inventory')");
echo "<font color=green>Success! You have bought that Item.</font><br><br><img src=images/items/$row[image]><br><br><a href=shops.php><< Back to Shops</a> | <a href=inventory.php>Inventory >></a><br>";
}
}
}
stop();
}
?>

 

Thanks Again! =]

Link to comment
https://forums.phpfreaks.com/topic/131344-solved-php-subtract-problem/
Share on other sites

If you format your code properly you'll find it a lot easier to read...

<?php
  session_start();
  include("config.php");
  if ($_SESSION['username'] ==  "" || $_SESSION['password'] == "") {
    notloggedin();
  }
  if ($_SESSION['username'] != "" && $_SESSION['password'] != "") {
    start();
    $action = $_GET['action'];
    $shopinfo = mysql_query("SELECT * FROM shops WHERE shopid='".$_GET['shopid']."'");
    while($shops = mysql_fetch_array($shopinfo)) {
      $shopname = $shops['name'];
      $shopid = $shops['shopid'];
      $shopkeeper = $shops['shopkeeper'];
      $greeting = $shops['greeting'];
    } 
    if ($action != "buy") { 
      echo "<font size=6>$shopname</font><br><br><table border=\"0\" bordercolor=\"white\" cellpadding=\"5\" cellspacing=\"0\">";
      $i = 0;
      $iteminfo = mysql_query("SELECT * FROM items WHERE shopid='".$_GET['shopid']."' AND amount != '0'");
      while ($items = mysql_fetch_array($iteminfo)) {
        $itemname = $items['name'];
        $itemid = $items['itemid'];
        $itemimage = $items['image'];
        $amount = $items['amount'];
        $price = $items['price'];
        if ($i % 4 == 0) {echo "<tr>";}
        echo '<td>'.$itemname.'<br><a href=shops.php?action=buy&itemid='.$itemid.'><img src=images/items/'.$itemimage.'></a><br>Price: '.$price.' EDC<br>Amount: '.$amount.'</td>';
        $i++;
      }
      echo "</table>";
    }
    $item_query = mysql_query("SELECT * FROM items WHERE itemid='".$_GET['itemid']."'");
    while($row = mysql_fetch_array($item_query)) {
      $checkprice = $credits - $row['price']; // here is the actual code for the checkprice variable
      if ($action == "buy") {
        if($checkprice < 0) { // Here it is used to say they don't have enough credits
          echo "Error!";
        }
        if($checkprice >= "0") { // Here it is used to say that they have enough vredits
          mysql_query("UPDATE users SET credits='".$credits."' - '".$row['price']."' WHERE username='".$username."'");
          mysql_query("UPDATE items SET amount ='".$row['amount']."' - '1' WHERE itemid='".$_GET['itemid']."'");
          mysql_query("INSERT INTO useritems (id, username, item, location) VALUES ('".$_GET['itemid']."', '".$username."', '".$row['name']."', 'Inventory')");
          echo "<font color=green>Success! You have bought that Item.</font><br><br><img src=images/items/".$row['image']."><br><br><a href=shops.php><< Back to Shops</a> | <a href=inventory.php>Inventory >></a><br>";
        }
      }
    }
    stop();
  }
?>

Also, explaining a couple of the variables would help.

Actually try this. I've edited the line that calculates checkprice (removed quotes) and added a debug line to tell you exactly what data the routine is playing with using an ECHO function - here's a part of the code:

    while ($row = mysql_fetch_array($item_query)) {
      $checkprice = $credits - $row['price']; // here is the actual code for the checkprice variable
      if ($action == "buy") {
        echo 'DEBUG: Price='.$row['price'].', credits='.$credits.', checkprice='.$checkprice;
        if ($checkprice < 0) { // Here it is used to say they don't have enough credits
          echo "Error!";
        }
        if ($checkprice >= "0") { // Here it is used to say that they have enough vredits
          mysql_query("UPDATE users SET credits='".$credits."' - '".$row['price']."' WHERE username='".$username."'");
          mysql_query("UPDATE items SET amount ='".$row['amount']."' - '1' WHERE itemid='".$_GET['itemid']."'");
          mysql_query("INSERT INTO useritems (id, username, item, location) VALUES ('".$_GET['itemid']."', '".$username."', '".$row['name']."', 'Inventory')");
          echo "<font color=green>Success! You have bought that Item.</font><br><br><img src=images/items/".$row['image']."><br><br><a href=shops.php><< Back to Shops</a> | <a href=inventory.php>Inventory >></a><br>";
        }
      }
    }

Give that a go and see if you're getting the data you expected.

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.