Jump to content

Problem With Subtraction Script


neex1233

Recommended Posts

This script has one problem, and I have one question. It works, like if you have 4 tokens it doesn't let you buy the item, but when you have enough to buy it, it lets you, but doesn't take the tokens out of the user's account! Can somebody help me? Now my question is how could I add what the user buys to a different table? Thank you.  ???

<?
ini_set ("display_errors", "1");
error_reporting(E_ALL);
require '/home/username/public_html/folder/config.php';
$username = mysql_real_escape_string($_SESSION['username']); 
$price = 5;
$sql="SELECT * FROM users WHERE username='$username'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
if($rows['tokens'] >= $price){  // Check if user has enough tokens
$sql = "UPDATE users SET tokens=(tokens-$price)  WHERE username='$username'";
mysql_query($sql);
echo "Thank you for buying $price!"; 
}else{
echo "Not enough Tokens";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/163237-problem-with-subtraction-script/
Share on other sites

whenever you do a mysql_query() you should check for an error...on both of your queries in your code put:

mysql_query($sql) or die(mysql_error());

and see if mysql reports any problems

 

also, if there aren't any problems, echo the value of $sql for the UPDATE code and make sure all the variables are being inserted properly

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.