Jump to content

[SOLVED] script is pulling more then they have....


mastercjb

Recommended Posts

I have this script for my game where they can change there race. This will cost them 2,500 points. However in our database points are called crystals.

 

If the person has 500 points, and the script takes 2,500 it will put them at -2,000 points. I need the script to tell them they dont have enough points and then not change there race because they dont have enough to do so. However im not to sure on how I would write it.

 

This is the race change script im editing, can anyone explain how to write this to do what I need:

 

function conf_race_change()
{
global $ir,$c,$userid,$h;
if($ir['race'] == "Pirate") { $r="Ninja"; } else { $r="Pirate"; }
print "Switching your race will cost 2,500 points. Are you sure you want to become a $r?<br />
<a href='preferences.php?action=racechange2'>Yes</a> | <a href='preferences.php'>No</a>";
}
function do_race_change()
{
global $db,$ir,$c,$userid,$h;
if($ir['race'] == "Pirate") { $r="Ninja"; } else { $r="Pirate"; }
$db->query("UPDATE users SET race='$r' WHERE userid=$userid");
$db->query("UPDATE users SET crystals=crystals-2500 WHERE userid=$userid");
print "Success, you are now $r!<br />
<a href='preferences.php'>Back</a>";
}

You will have to retrieve the number of crystals the user has before making the change.

 

$q = mysql_query("SELECT crystals FROM users WHERE userid=$userid");
$row = mysql_fetch_array($q);
$crystals = $row['crystals'];
if($crystals>=2500)
$db->query("UPDATE users SET race='$r' WHERE userid=$userid");

if($crystals<2500 || mysql_affected_rows()==0)
print "Your race cannot be changed";
else
print "Success, you are now $r!<br />";
print "<a href='preferences.php'>Back</a>";

Thanks I re scripted it like this, and it worked:

 

 

function conf_race_change()
{
global $ir,$c,$userid,$h;
if($ir['race'] == "Pirate") { $r="Ninja"; } else { $r="Pirate"; }
print "Switching your race will cost 1,500 points. Are you sure you want to become a $r?<br />
<a href='preferences.php?action=racechange2'>Yes</a> | <a href='preferences.php'>No</a>";
}
function do_race_change()
{
global $db,$ir,$c,$userid,$h;
if($ir['race'] == "Pirate") { $r="Ninja"; } else { $r="Pirate"; }
if($ir['crystals'] < 1500)
{
die("You don't have enough points. You need 1,500 points.");
}
$db->query("UPDATE users SET race='$r' WHERE userid=$userid");
$db->query("UPDATE users SET crystals=crystals-1500 WHERE userid=$userid");
print "Success, you are now $r!<br />
<a href='preferences.php'>Back</a>";
}

 

Thank you

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.