Jump to content

Minigame wont add or subtract points.


Gregoyle

Recommended Posts

Hey, I have a minigame on my website and when you bet 1 It says you lost and you lose your point but it doesn't remove it, Same with if you win it says you gain the point but doesn't add it....

 

 

function bet ($bet){  
sleep(2);

$user1 = mysql_query("SELECT * FROM `users` WHERE `id`='" . $_SESSION['id'] . "'");
$user = mysql_fetch_object($user1);

$chance = rand (1, 2);
$bet = round ($bet);
$betPretty = number_format ($bet);

if ($bet == 0) { $echo = "You did not bet anything!"; $chance = 0; }
if ($bet < 0) { $echo = "You cannot bet a negative number."; $chance = 0; }
if ($bet > $user->minigame_points) { $echo = "This is more minigame points than you have!"; $chance = 0; }
if ($user->minigame_points == 0) { $echo = "You do not have any minigame points."; $chance = 0; }

if ($chance == 1) {
	//Win
	$money = $user->minigame_points+$bet;
	$times_bet = $user->times_bet+1;
	mysql_query ("UPDATE `users` SET `minigame_points`='" . $money . "', `times_bet`='" . $new_bet . "' WHERE `username`='" . $user->username . "'");
	$money = number_format($money);
	$echo = "You won $betPretty minigame points!<br /><b>Current Minigame Points:</b> $money";
}
if ($chance == 2) {
	//Lose
	$money = $user->minigame_points-$bet;
	$times_bet = $user->times_bet+1;
	mysql_query ("UPDATE `users` SET `minigame_points`='" . $money . "', `times_bet`='" . $new_bet . "' WHERE `username`='" . $user->username . "'");
	$money = number_format($money);
	$echo = "You lost $bet minigame points.<br /><b>Current Minigame Points:</b> $money";
}
    return $echo;  

Link to comment
Share on other sites

in both if statements you have the wrong variable name being used in the query:

		$times_bet = $user->times_bet+1; // times_bet
	mysql_query ("UPDATE `users` SET `minigame_points`='" . $money . "', `times_bet`='" . $new_bet . "' WHERE `username`='" . $user->username . "'");   // new_bet

 

either rename $times_bet to $new_bet or... $new_bet to $times_bet

 

Regards, Ace

Link to comment
Share on other sites

when you bet 1 It says you lost and you lose your point but it doesn't remove it, Same with if you win it says you gain the point but doesn't add it....

Do you mean it is not updating your database?

 

I have tested your function and can confirm it does add points when you win and takes points when you loose. If you create a new file and add this code in it

<?php
function bet ($bet, &$user){
//sleep(2);

//$user1 = mysql_query("SELECT * FROM `users` WHERE `id`='" . $_SESSION['id'] . "'");
//$user = mysql_fetch_object($user1);

$chance = rand (1, 2);
$bet = round ($bet);
$betPretty = number_format ($bet);

if ($bet == 0) { $echo = "You did not bet anything!"; $chance = 0; }
if ($bet < 0) { $echo = "You cannot bet a negative number."; $chance = 0; }
if ($bet > $user->minigame_points) { $echo = "This is more minigame points than you have!"; $chance = 0; }
if ($user->minigame_points == 0) { $echo = "You do not have any minigame points."; $chance = 0; }

if ($chance == 1) {
	//Win
	$money = $user->minigame_points+$bet;
	$times_bet = $user->times_bet++;
	//mysql_query ("UPDATE `users` SET `minigame_points`='" . $money . "', `times_bet`='" . $new_bet . "' WHERE `username`='" . $user->username . "'");
	$money = number_format($money);
	$echo = "You won $betPretty minigame points!<br /><b>Current Minigame Points:</b> $money";
}
if ($chance == 2) {
	//Lose
	$money = $user->minigame_points-$bet;
	$times_bet = $user->times_bet++;
	//mysql_query ("UPDATE `users` SET `minigame_points`='" . $money . "', `times_bet`='" . $new_bet . "' WHERE `username`='" . $user->username . "'");
	$money = number_format($money);
	$echo = "You lost $bet minigame points.<br /><b>Current Minigame Points:</b> $money";
}

    return $echo;
}

$user = new stdClass;
$user->minigame_points = 100;
$user->times_bet = 0;

echo bet(10, $user);
?>

All I did was comment your queries and hard coded in some dummy data to test the function with.

Link to comment
Share on other sites

You (always) need to test in your code if a query worked or not and in the case of an UPDATE (or INSERT) query, you should always test if the row was actually updated. Also, you should (always) form your query in a variable so that you can make use of the actual query in your error handling logic that you need to have in your code (always.)

 

Typical code an UPDATE (and INSERT) query -

 

<?php
$query = "...your_query_statement_here..."; // form your query statement in a variable (allows using the actual query statement in your error handling logic)
// execute the query and test the returned value
if(mysql_query($query)){
// A TRUE was returned, the query executed without any errors.
// test the number of row(s) affected by the query
if(mysql_affected_rows()){
	// one or more row(s) where affected by the query

	// the query was successful and affected one or more rows in the database table
               ...your_code_for_when_the_UPDATE_actually_worked...	
               echo "Row was UPDATED";

} else {
	// no row was affected
	// the UPDATE either did not match any row (the WHERE clause was false) or did not update any of the data in the row (same data values)
                echo "Row NOT updated";

}
} else {
// A FALSE value was returned, the query failed, handle the error here...
$user_message = "some user/visitor message";
$application_message = "Query failed: $query, Error: " . mysql_error(); // add other relavant information such as current file, line number, current visitor information, backtrace info,...

echo $user_massage; // cause the user message to be output or returned, ... as needed to get it to be sent to the browser
trigger_error($application_message); // Use php's error handling to display (during development) and/or log the application error message
}

 

If you use code like above, you will know what your query is doing and if you have error_reporting set to E_ALL and display_errors set to ON (you apparently don't because the problem that MasterACE14 pointed out would have resulted in some php errors) the trigger_error() statement will report and display the actual query and query error if the query is failing.

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.