Jump to content

Recommended Posts

is there something wrong with the code it keeps coming up blank on my webpage.

 

<?php
$sql = mysql_query("SELECT * FROM users WHERE username = '$username'");
$row = mysql_fetch_array($sql);
if ($row['gold'] >= 100) {
  $sql = "UPDATE users SET gold=gold-100, potions=potions+1 WHERE username = '$username'") or die(mysql_error());
echo '<center>You have bought one health potion</center>';
}else{
echo '<center>You don't have enough gold. Come back when you get more.</center>';
}

?>

i checked that and it still shows up white on the web browers

<?php
$sql = mysql_query("SELECT * FROM users WHERE username = '$username'");
$row = mysql_fetch_array($sql);
if ($row['gold'] >= 100) {
  $sql = "UPDATE users SET gold=gold-100, potions=potions+1 WHERE username = '$username'") or die(mysql_error();
echo "<center>You have bought one health potion.</center>";
}else{
echo "<center>You don't have enough gold. Come back when you getmore.</center>";
}
?>

this is the entire code so far i still didnt find any errors

 

<?php

mysql_connect("database", "username", "password") or die(mysql_error());
mysql_select_db("members") or die(mysql_error());

$sql = mysql_query("SELECT * FROM users WHERE username = '$username'");
$row = mysql_fetch_array($sql);
if ($row['gold'] >= 100) {
  $sql = "UPDATE users SET gold = gold-100, potions = potions+1 WHERE username = '$username'") or die(mysql_error());
echo "<center>You have bought one health potion.</center>";
}else{
echo "<center>You don't have enough gold. Come back when you getmore.</center>";
}

?>

turn on error reporting in your PHP.ini so you can catch syntax errors.

 

you also have an extra parenthesis on your $sql line.  plus, you cannot run "or die()" when you are only assigning a text string.  that is used when you actually run the query

<?php
if ($row['gold'] >= 100) {
  $sql = "UPDATE users SET gold = gold-100, potions = potions+1 WHERE username = '$username'";
  $result = mysql_query($sql) or die(mysql_error());
  echo "<center>You have bought one health potion.</center>";
}else{
echo "<center>You don't have enough gold. Come back when you getmore.</center>";
}

 

again, turn on error reporting to catch these yourself

i bellieve this should do it

<?php

mysql_connect("database", "username", "password") or die(mysql_error());
mysql_select_db("members") or die(mysql_error());

$sql = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'");
$row = mysql_fetch_array($sql);
if ($row['gold'] >= 100) {
////Had error in this line
////  $sql = ("UPDATE users SET gold = gold-100, potions = potions+1 WHERE username = '$username'") or die(mysql_error());
$sql = ("UPDATE users SET `gold` = `gold`-100, `potions` = `potions`+1  WHERE `username` = '$username'") or die(mysql_error());
echo "<center>You have bought one health potion.</center>";
}else{
echo "<center>You don't have enough gold. Come back when you getmore.</center>";
}

?>

 

 

Let us know

Do they have enough gold? I would print out the $row['gold'] in the else:

 

$currentGold = $row['gold'] - 100;
echo "<center>You have bought one health potion. You now have {$currentGold}.</center>";
}else{
echo "<center>You don't have enough gold, you currently have {$row['gold']}. Come back when you getmore.</center>";
}

 

And see what is displayed.

Hmm. Not sure why it would have returned that...maybe it is an older version of PHP I do not know. This this instead:

 

$currentGold = $row['gold'] - 100;
echo "<center>You have bought one health potion. You now have " . $currentGold . ".</center>";
}else{
echo "<center>You don't have enough gold, you currently have " . $row['gold'] . ". Come back when you getmore.</center>";
}

 

See if that produces a number. I have a hunch that it showed the $row['gold'] portion maybe because it is not defined...no clue....it seems there maybe an issue on your server ???

<?php

mysql_connect("database", "username", "password") or die(mysql_error());
mysql_select_db("members") or die(mysql_error());

$sql = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'");
if (mysql_num_rows($sql) > 0) {
    $row = mysql_fetch_array($sql);
    if ($row['gold'] >= 100) {
     ////Had error in this line
     ////  $sql = ("UPDATE users SET gold = gold-100, potions = potions+1 WHERE username = '$username'") or die(mysql_error());
       $sql = ("UPDATE users SET `gold` = `gold`-100, `potions` = `potions`+1  WHERE `username` = '$username'") or die(mysql_error());
        echo "<center>You have bought one health potion.</center>";
    }else{
        echo "<center>You don't have enough gold. Come back when you getmore.</center>";
    }
}else {
    echo "No rows were returned by the SQL. Username entered was " . $username;
}
?>

 

Where does $username come from? Try the above, my bet is that $username will not display. You need to assign $username a value some how whether from $_POST/$_GET/$_SESSION, it seems like you are assuming register_globals is turned on, which it is not and this is causing the problem.

ok it now displays "You have bought one health potion."

but its not updating the database

$sql = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'");
$row = mysql_fetch_array($sql);
if ($row['gold'] >= 100) {
$sql = ("UPDATE users SET `gold` = `gold`-100, `potions` = `potions`+ 1  WHERE `username` = '$username'") or die(mysql_error());
echo "<center>You have bought one health potion.</center>";
}else{
echo "<center>You don't have enough gold. Come back when you getmore.</center>";
}

Do they have enough gold? I would print out the $row['gold'] in the else:

 

$currentGold = $row['gold'] - 100;
echo "<center>You have bought one health potion. You now have {$currentGold}.</center>";
}else{
echo "<center>You don't have enough gold, you currently have {$row['gold']}. Come back when you getmore.</center>";
}

 

And see what is displayed.

correction on

 

echo "<center>You don't have enough gold, you currently have ". $row['gold'].". Come back when you get more.</center>";

 

goahead and see if it shows or what it shows

 

 

ok it now displays "You have bought one health potion."

but its not updating the database

Code: [select]

$sql = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'");

$row = mysql_fetch_array($sql);

if ($row['gold'] >= 100) {

$sql = ("UPDATE users SET `gold` = `gold`-100, `potions` = `potions`+ 1  WHERE `username` = '$username'") or die(mysql_error());

echo "<center>You have bought one health potion.</center>";

}else{

echo "<center>You don't have enough gold. Come back when you getmore.</center>";

}

 

wow i totally forgot

 

change

$sql = ("UPDATE users SET `gold` = `gold`-100, `potions` = `potions`+ 1  WHERE `username` = '$username'") or die(mysql_error());

 

to

change

$sql = ("UPDATE users SET `gold` = (`gold`-100), `potions` = (`potions`+ 1)  WHERE `username` = '$username'") or die(mysql_error());

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.