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
https://forums.phpfreaks.com/topic/208791-conditional-to-complete-mysql-query/
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

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.