Jump to content

Can only increment a value, can't decrement?


dpedroia

Recommended Posts

I have tables in my MySQL database called "games", "wins" and "losses". I'm attempting to write SQL queries that will increment or decrement a value depending on what link is clicked from the main page. My SQL queries are as follows:

 

<?php require_once('config.php');
if ( !isset($_SESSION['user']) ) { include('login.php'); exit; }
$sql = 'select * from `teams` WHERE `id` = \'' . mysql_real_escape_string($_GET['team']) . '\'';
$res = mysql_query($sql);
//if ( mysql_num_rows($res) == 0 ) { include('index.php'); exit; }
$row_team = mysql_fetch_array($res);
if ( isset($_GET['addgame']) ) { 
$sql = 'UPDATE `teams` SET `games` = `games` + 1 WHERE `id` = \'' . mysql_real_escape_string($_GET['team']) . '\'';
mysql_query($sql);
}
if ( isset($_GET['subtractgame']) ) { 
$sql = 'UPDATE `teams` SET `games` = `games` - 1 WHERE `id` = \'' . mysql_real_escape_string($_GET['team']) . '\'';
mysql_query($sql);
}
if ( isset($_GET['addwin']) ) { 
$sql = 'UPDATE `teams` SET `wins` = `wins` + 1 WHERE `id` = \'' . mysql_real_escape_string($_GET['team']) . '\'';
mysql_query($sql);
}
if ( isset($_GET['subtractwin']) ) { 
$sql = 'UPDATE `teams` SET `wins` = `wins` - 1 WHERE `id` = \'' . mysql_real_escape_string($_GET['team']) . '\'';
mysql_query($sql);
}
if ( isset($_GET['addloss']) ) { 
$sql = 'UPDATE `teams` SET `losses` = `losses` + 1 WHERE `id` = \'' . mysql_real_escape_string($_GET['team']) . '\'';
mysql_query($sql);
}
if ( isset($_GET['subtractloss']) ) { 
$sql = 'UPDATE `teams` SET `losses` = `losses` - 1 WHERE `id` = \'' . mysql_real_escape_string($_GET['team']) . '\'';
mysql_query($sql);
}
$sql = 'select * from `teams` WHERE `id` = \'' . mysql_real_escape_string($_GET['team']) . '\'';
$res = mysql_query($sql);
$row_team = mysql_fetch_array($res);
?>

 

For some reason, I can increment the amount of games, wins or losses as many times as I want.. it'll be updated in the database and the changes will be reflected on the page. However, when I go to subtract a game, win or loss, it seems to leave null values as if there are no games, wins or losses at all.

 

For example, say the site outputs the Yankees have 10 wins and 2 losses. When "[Add Win]" is clicked, the win total will increase to 11 and such changes will be displayed on the site. But, when "[Deduct Win]" is clicked, it seems to cancel out every value for the number of games, wins and losses and just outputs nothing..

 

Is there a reason for this? My addition is working fine, it's the subtraction that's messed up.. but it should work based on what I see.

 

Thanks.

Link to comment
Share on other sites

Try this...

if ( isset($_GET['subtractwin']) ) { 
  $sql = 'UPDATE `teams` SET `wins` = `wins` - 1 WHERE `id` = \'' . mysql_real_escape_string($_GET['team']) . '\'';
  echo $sql.'<br>';
  mysql_query($sql);
}

 

Then try the code. Once you get the query in your browser copy and paste it into phpMyAdmin and see if it generates an error - if it does, you've found the problem.

Link to comment
Share on other sites

First, I meant to say I have fields in my database called 'games', 'wins' and 'losses'. They are all in the 'teams' table. Sorry!

 

Okay, this is odd. Yesideez, when I added the echo line and echoed the query for addwin it was showing the Team ID of 15, which is the ID of a team in my database. However, I use the same query for subtractwin but only change the '+' to a '-' and the query that's echoed then shows a Team ID of 1.. not 15. It seems that all of the 'add' features work.. but the 'subtract' ones reset the Team ID being used to '1' instead of whatever the Team ID should really be.

 

Query being echoed when I add a win:

 

UPDATE `teams` SET `wins` = `wins` + 1 WHERE `id` = '15';

 

Query being echoed when I subtract a win:

 

UPDATE `teams` SET `wins` = `wins` + 1 WHERE `id` = '1';

 

Thing is, I ran the query manually from phpMyAdmin and I can add and subtract fine.. values are changing and it's all good. It's only when I click 'Subtract Win' through the webpage that it resets the Team ID back to 1 and does not work.

 

I attached a screenshot of my `teams` table.

 

Thanks.

 

[attachment deleted by admin]

Link to comment
Share on other sites

Well folks, it looks like we've encountered an ID-10-T error. My link for subtracting wins, for example, was:

 

<a href="manageteam.php?team=<?php echo $teamRow['id']['id']; ?>&subtractwin=1">[Deduct Win]</a>

 

Looks like I had two ['id'] entries in the link that was messing up the link somehow.. deleted one of them and it's good to go.

 

On to better times!

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.