Jump to content

Conditional to complete mysql query


thereaper87

Recommended Posts

Hello there,

 

I have this snipped of code I have been trying to create since tuesday. So far there has been little success. What this does is, if the logged in user has 20,000,000 xp or more, it completes the mysql query which subtracts the 20,000,000. But if the user has less than 20,000,000 xp, it will echo "Do not have enough xp"....here is the code:

 

<?php
session_start();
include('../database.php'); 
$userid = $_SESSION['user']; 

$userxp = "SELECT xp FROM user WHERE username = '$userid'";
$amount = 19999999;
if ($userxp > $amount){
$query = "UPDATE `user` SET `xp`=(`xp`-20000000) WHERE `username` = '$userid' LIMIT 1";
$result = mysql_query($query) or die('Query: ' . $query . '<br />Failed with: ' . mysql_error());
echo "You have purchased this item";
}
else
{
echo "You do not have enough xp!";
}
?>

 

If you need any additional info please ask! Right now I'm on an airplane so I may not be able to respond quickly.

Link to comment
Share on other sites

You never actually run the first query against the database ...

 

<?php
session_start();
include('../database.php'); 
$userid = $_SESSION['user']; 

$userxp = mysql_fetch_array ( mysql_query ( "SELECT xp FROM user WHERE username = '$userid'" ) );
$amount = 19999999;
if ($userxp [ 'xp' ] > $amount){
$query = "UPDATE `user` SET `xp`=(`xp`-20000000) WHERE `username` = '$userid' LIMIT 1";
$result = mysql_query($query) or die('Query: ' . $query . '<br />Failed with: ' . mysql_error());
echo "You have purchased this item";
}
else
{
echo "You do not have enough xp!";
}
?>

 

Would be a hacked out version of your code so that it should work.

 

~juddster

Link to comment
Share on other sites

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.