Page 1 of 1
Code subtracts two instead of one
Posted: Wed Dec 20, 2006 11:57 am
by Citizen
Code: Select all
$player_recruits = $player_recruits - 1;
$sql1 = "UPDATE `player` SET `player_recruits` = '$player_recruits' WHERE `player_id` = '$player_id' LIMIT 1";
$result1 = mysql_query($sql1) or die(mysql_error());
It subtracts two from the value of player_recruits instead of one. I'm not sure how this is possible, its really blowing my mind.
Posted: Wed Dec 20, 2006 12:02 pm
by John Cartwright
It has to be something else in your code.. but you can try a pure sql approach
Code: Select all
$sql1 = "UPDATE `player` SET `player_recruits` = `player_recruits`-1 WHERE `player_id` = '$player_id' LIMIT 1";
Posted: Wed Dec 20, 2006 12:11 pm
by Citizen
Jcart wrote:It has to be something else in your code.. but you can try a pure sql approach
Code: Select all
$sql1 = "UPDATE `player` SET `player_recruits` = `player_recruits`-1 WHERE `player_id` = '$player_id' LIMIT 1";
Same results with that code.
Here's my full code:
Code: Select all
<?php include('includes/topheader.php'); ?>
<title>Recruit - <?php echo"$htitle"; ?></title>
<meta name="description" content="" />
<?php
include('includes/dbloader.php');
include('includes/endheader.php');
include('includes/top.php');
?>
<h1>Recruit</h1>
<p>
<?php include('includes/menu.php'); ?>
<?php
if ($_SESSION['loggedin']) {
echo"
<p>You can recruit up to 50 times per day.
<br />Your percent chance to recruit increases with your level.
<br />If you get lucky, you might even recruit an already trained worker, attacker, defender, or spy.
";
$username = $_SESSION['username'];
$sql="SELECT * FROM `player` WHERE `player_name` = '$username' LIMIT 1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
$player_id = $row[player_id];
$player_level = $row[player_level];
$player_peasant = $row[player_peasant];
$player_worker = $row[player_worker];
$player_attacker = $row[player_attacker];
$player_defender = $row[player_defender];
$player_spy = $row[player_spy];
$player_recruits = $row[player_recruits];
echo"<p>You have $player_recruits recruits left today.";
if($player_recruits > 0){
$sql1 = "UPDATE `player` SET `player_recruits` = `player_recruits`-1 WHERE `player_id` = '$player_id' LIMIT 1";
$result1 = mysql_query($sql1) or die(mysql_error());
Re: Code subtracts two instead of one
Posted: Wed Dec 20, 2006 12:41 pm
by RobertGonzalez
Do this...
Code: Select all
<?php
var_dump($player_recruits);
$player_recruits--;
var_dump($player_recruits);
?>
What does that tell you?
Posted: Wed Dec 20, 2006 1:04 pm
by Citizen
Returns:
string(2) "36" int(35)
I ran the script a few times and checked the results that show what the rest of the code on the page does.
I think that somehow, the script is being run twice when I run it once.
One time, I ran the page (it randomly gives +1 to a different value or does nothing). It said it did nothing on the page, which usually matches up with the results, but this time it said nothing and the change of +1 happened in the db anyway.
Any ideas?
Posted: Wed Dec 20, 2006 1:49 pm
by RobertGonzalez
Can you post the whole code?
Posted: Wed Dec 20, 2006 1:51 pm
by Citizen
Code redacted
Posted: Wed Dec 20, 2006 2:55 pm
by RobertGonzalez
See what this does...
Code: Select all
<?php include 'includes/topheader.php'; ?>
<title>Recruit - <?php echo $htitle; ?></title>
<meta name="description" content="" />
<?php
include 'includes/dbloader.php';
include 'includes/endheader.php';
include 'includes/top.php';
?>
<h1>Recruit</h1>
<p>
<?php
include 'includes/menu.php';
if ($_SESSION['loggedin']) {
echo '
<p>You can recruit up to 50 times per day.
<br />Your percent chance to recruit increases with your level.
<br />If you get lucky, you might even recruit an already trained worker, attacker, defender, or spy.';
$username = $_SESSION['username'];
$sql = "SELECT *
FROM `player`
HERE `player_name` = '$username'
LIMIT 1";
$result=mysql_query($sql) or die('there was an error in the query: ' . mysql_error());
$row = mysql_fetch_array($result);
$player_id = $row['player_id'];
$player_level = $row['player_level'];
$player_peasant = $row['player_peasant'];
$player_worker = $row['player_worker'];
$player_attacker = $row['player_attacker'];
$player_defender = $row['player_defender'];
$player_spy = $row['player_spy'];
$player_recruits = intval($row['player_recruits']); // This is a number, so integerize it
echo "<p>You have $player_recruits recruits left today.";
if($player_recruits > 0){
// this is for the output messages later
$msg = '';
$msg_append = ' Recruit again...';
// This is for the screen
$player_recruits--;
// We want to lessen the player_recruits value by one in the database
$sql = "UPDATE `player`
SET `player_recruits` = `player_recruits` - 1
WHERE `player_id` = '$player_id'
LIMIT 1";
$result = mysql_query($sql) or die(mysql_error());
echo "<p>Attempting to recruit new peasants...";
$number = rand(1,100);
$chance = 35 + $player_level;
$player_peasant++;
$player_spy++;
$player_defender++;
$player_attacker++;
$player_worker++;
echo "<p>Chance: $chance";
echo "<p>Number: $number";
if ($number == 1) {
$sql = "UPDATE `player`
SET `player_worker` = '$player_worker'
WHERE `player_id` = '$player_id'
LIMIT 1";
$msg = '<p>You recruited a trained worker!';
} elseif ($number == 2) {
$sql = "UPDATE `player`
SET `player_attacker` = '$player_attacker'
WHERE `player_id` = '$player_id'
LIMIT 1";
$msg = '<p>You recruited a trained attacker!';
} elseif ($number == 3) {
$sql = "UPDATE `player`
SET `player_defender` = '$player_worker'
WHERE `player_id` = '$player_id'
LIMIT 1";
$msg = '<p>You recruited a trained defender!';
} elseif ($number == 4) {
$sql = "UPDATE `player`
SET `player_spy` = '$player_worker'
WHERE `player_id` = '$player_id'
LIMIT 1";
$msg = '<p>You recruited a trained spy!';
} elseif ($chance >= $number) {
$sql = "UPDATE `player`
SET `player_peasant` = '$player_peasant'
WHERE `player_id` = '$player_id'
LIMIT 1";
$msg = '<p>You recruited a peasant.';
} else {
$msg = '<p>No dice!';
$msg_append = ' Better luck next time...';
}
$result = mysql_query($sql) or die(mysql_error());
echo $msg . $msg_append . ' <p><a href="recruit.php">Recruit</a>';
} else {
echo '<p>Sorry, you must be logged in to continue. Visit the <a href="index.php">index</a> to login or <a href="register.php">register</a>.';
}
include 'includes/bottom.php';
include 'includes/footer.php';
?>