Jump to content

Updating Db With Math


Greysoul
 Share

Recommended Posts

not sure if there's something wrong with the search engine on these forums but i usually get everything i didn't search for when i use it lol. i'm trying to test out updating with math and so far i'm not having any luck. i'm taking the input value and adding it to the already existing value in the db via the update string.

$player=mysql_query("SELECT * FROM Scorecard WHERE ID=2060");echo "<table>";echo "<form id='update' name='update' method='post' action=''>";while($grey = mysql_fetch_array( $player )) {$score = $grey['Score'];echo "<tr><td>Greysoul</td><td>";echo "<input class='form' type='text' name='grey' id='".$grey['Score']."' value='' size='3'>";echo "</td></tr>";}echo "</table>";echo "<td>" . "<input type='Submit' value='Update' name='Update'>" . "</td>";echo "</form>";if($_POST['Update'])$greyscore = $_POST['grey'];{$update="UPDATE Scorecard SET Score='".$score."' + ".$greyscore."' WHERE ID=2060";mysql_query($update); if (!$update)  {  die('Could not connect: ' . mysql_error());  }}

i get no error. if i do this the correct total does print. any ideas?

$x = $greyscore + $score;echo $x;

Edited by Greysoul
Link to comment
Share on other sites

not sure if there's something wrong with the search engine on these forums but i usually get everything i didn't search for when i use it lol. i'm trying to test out updating with math and so far i'm not having any luck. i'm taking the input value and adding it to the already existing value in the db via the update string.
$player=mysql_query("SELECT * FROM Scorecard WHERE ID=2060");echo "<table>";echo "<form id='update' name='update' method='post' action=''>";while($grey = mysql_fetch_array( $player )) {$score = $grey['Score'];echo "<tr><td>Greysoul</td><td>";echo "<input class='form' type='text' name='grey' id='".$grey['Score']."' value='' size='3'>";echo "</td></tr>";}echo "</table>";echo "<td>" . "<input type='Submit' value='Update' name='Update'>" . "</td>";echo "</form>";if($_POST['Update'])$greyscore = $_POST['grey'];{$update="UPDATE Scorecard SET Score='".$score."' + '".$greyscore."' WHERE ID=2060";mysql_query($update); if (!$update)  {  die('Could not connect: ' . mysql_error());  }}

i get no error. if i do this the correct total does print. any ideas?

$x = $greyscore + $score;echo $x;

nevermind, just used Score=Score+'".$greyscore."' and it worked
Link to comment
Share on other sites

doing the same thing with subtraction..i just changed all the +'s to -'s and it doesn't want to process..i'm assuming its the same concept? i was trying to keep it to a minimum to find out my problem..so i was only doing the scores here. everything submits but the update doesn't occur..like i've typed something wrong. the variable names are new, but anything after the = sign for the post is copy and pasted from an above IF POST that works.This is my actual code in the page:

if($_POST['Delete']){$index = 0;while (isset($_POST['redplayer'][$index])) {$redPlayersub=$_POST['redplayer'][$index];$redScoresub=$_POST['redscore'][$index];$redKillssub=$_POST['redkills'][$index];$redDeathssub=$_POST['reddeaths'][$index];$redCapssub=$_POST['redcaps'][$index];$redAttemptssub=$_POST['redattempts'][$index];$redMVPsub=$_POST['redmvpradio'][$index];$index++;$subtractstats="UPDATE Players SET Scores=Scores-'".$redScoresub."' WHERE Player='".$redPlayersub."'";mysql_query($subtractstats);}}

This is on another page that i'm testing with..it works. I'm guessing there's something simple i've got wrong above..

$player=mysql_query("SELECT * FROM Players WHERE ID=168");echo "<table>";echo "<form id='update' name='update' method='post' action=''>";while($grey = mysql_fetch_array( $player )) {$score = $grey['Scores'];echo "<tr><td>Greysoul</td><td>";echo "<input class='form' type='text' name='grey' id='".$grey['Scores']."' value='' size='3'>";echo "</td></tr>";}echo "</table>";echo "<td>" . "<input type='Submit' value='Update' name='Update'>" . "</td>";echo "</form>";if($_POST['Update'])$greyscore = $_POST['grey'];{$update="UPDATE Players SET Scores=Scores-'".$greyscore."' WHERE ID=168";mysql_query($update); if (!$update)  {  die('Could not connect: ' . mysql_error());  }}echo "<br>";$x = $score - $greyscore;echo $x;

Edited by Greysoul
Link to comment
Share on other sites

Print the queries that aren't working, it's hard to tell why a query is failing without seeing the query. Also, numbers typically don't go in quotes. You can also test if the query fails (mysql_query will return false) and use mysql_error to get the error message.

Link to comment
Share on other sites

nothing prints when i do this..arg..nothing prints if i echo just the update query either.

if($_POST['Delete']){$index = 0;while (isset($_POST['redplayer'][$index])) {$redPlayersub=$_POST['redplayer'][$index];$redScoresub=$_POST['redscore'][$index];$redKillssub=$_POST['redkills'][$index];$redDeathssub=$_POST['reddeaths'][$index];$redCapssub=$_POST['redcaps'][$index];$redAttemptssub=$_POST['redattempts'][$index];$redMVPsub=$_POST['redmvpradio'][$index];$index++;$subtractstats=("UPDATE Players SET Scores=Scores-'".$redScoresub."' WHERE Player='".$redPlayersub."'");mysql_query($subtractstats); if($subtractstats!=true){echo "Error: ".mysql_error();}}}

Link to comment
Share on other sites

i must be putting it in the wrong place. i also tried just print $subtractstats; and absolutely nothing prints. :x

if($_POST['Delete']){$index = 0;while (isset($_POST['redplayer'][$index])) {$redPlayersub=$_POST['redplayer'][$index];$redScoresub=$_POST['redscore'][$index];$redKillssub=$_POST['redkills'][$index];$redDeathssub=$_POST['reddeaths'][$index];$redCapssub=$_POST['redcaps'][$index];$redAttemptssub=$_POST['redattempts'][$index];$redMVPsub=$_POST['redmvpradio'][$index];$index++;$subtractstats=("UPDATE Players SET Scores=Scores-'".$redScoresub."' WHERE Player='".$redPlayersub."'");mysql_query($subtractstats); echo $subtractstats;if($subtractstats!=true){echo "Error: ".mysql_error();}}}

Edited by Greysoul
Link to comment
Share on other sites

It sounds like that code isn't even running then. It should be obvious that $subtractstats is not empty and will in fact print the query. If you're not seeing it print, then that code probably isn't even running. You can print just a static value if you want to verify that, like a message that you're about to run the query.

Link to comment
Share on other sites

This stuff doesn't happen for no reason. If the code in the if statement isn't running, then the condition in the if statement isn't true. It's not any more complicated than that.When I debug my applications, I'm not tracking down syntax errors, I'm tracking down errors that involve the data not being what I expected. The only way to solve these errors is to output absolutely all of the data you use, and step through your code to figure out why it's acting the way it is. This isn't as hard as people think it is, you told the computer exactly what to do so you just need to figure out what data your code is working with, and then you can step through the code line by line to figure out why it's working the way it is. I've written countless if statements which I've found out don't do what I expect after I get some data and go through the code. In fact, the vast majority of people's questions here are asked because the have no idea what their code is actually doing. Just print out the data your code is using and then step through the code yourself, keep track of variables on paper if you need to. If your if statement is checking a variable in $_POST, and the code in the if statement isn't being executed, then check the values in $_POST. Don't make assumptions about the data, assume the code is doing the correct thing according to the rules you've given it.

Link to comment
Share on other sites

this is how it breaks down in my match update page..you insert the stats of the game and it prints the results to the right. at this point you can delete the stats that were printed and start over if you want. the problem is not only does it have to delete the scorecard stats, but it has to subtract it from the actual players stats. right now i've got it where it subtracts only one per team at a time. i'm trying to figure out how to do the increment thing so it does them all.for example, these are the values its taking to subtract..the ones that are displayed after inserting. not real sure at what angle i should go about to make this work..i have a feeling its with the increment deal but no clue where to start :3.

$grnstats=mysql_query("SELECT * FROM Scorecard WHERE GameID='$matchid' AND Clan='".$greenclan."' ORDER BY ID");while($row=mysql_fetch_array($grnstats)){$grnplyer = $row['Player'];$grnplyerscore = $row['Score'];$grnplyerkills = $row['Kills'];$grnplyerdeaths = $row['Deaths'];$grnplyercaps = $row['Caps'];$grnplyerattempts = $row['Attempts'];echo "<tr>\n";echo "<td class='tdshow'>" . $row['Player'] . "</td>";echo "<td class='tdshow'>" . $row['Score'] . "</td>";echo "<td class='tdshow'>" . $row['Kills'] . "</td>";echo "<td class='tdshow'>" . $row['Deaths'] . "</td>";echo "<td class='tdshow'>" . $row['Caps'] . "</td>";echo "<td class='tdshow'>" . $row['Attempts'] . "</td>";echo "</tr>\n";}

and

$gamemvp=mysql_query("SELECT * FROM Scorecard WHERE GameID='$matchid' AND MVP='1'");while($mvpofgame=mysql_fetch_array($gamemvp)){$mvp=$mvpofgame['Player'];echo "MVP:" . $mvpofgame['Player'];}

this is the update

if($_POST['Delete']){$sub="UPDATE Players SET Scores=Scores-'".$grnplyerscore ."', Kills=Kills-'".$grnplyerkills."', Deaths=Deaths-'".$grnplyerdeaths."', FC=FC-'".$grnplyercaps."', FA=FA-'".$grnplyerattempts."', GP=GP-1 WHERE Player='".$grnplyer."'";mysql_query($sub);if($mvp==$grnplyer){$submvp="UPDATE Players SET MVP=MVP-1 WHERE Player = '".$grnplyer."'";mysql_query($submvp);}}

Link to comment
Share on other sites

ok forget the above, i'm just using one table instead of two for all the player information now..as i've realized now two was just a little unnecessary. my scorecard table has the stats for each game, and players names will be repeated throughout the table with different GameID identifiers. For each player there is a specific page for it, to display all their total tallied stats and other things. i'm able to do this really easily with a while loop, adding up their total stats and calculating the rest. example below: Greysoul 1 13 15 7 13 15 2.14 2 0 0% 0the problem is for the clan page, where i display ALL of the players the exact same way...one after the other, it gets a little hard. if i say where=name of clan...it adds up all the players stats in one row..instead of looping it like i thought it would. as well, it only shows one players name. how do i make it loop through all the players instead of just one? this is the code for just a single player..up until now i've never seen it not loop through all of the things in a table, so its new to me.

echo "<table border='1'>";$playersum=mysql_query("SELECT *, Count(Player) AS pgp, SUM(Score) AS ps, SUM(Kills) AS pk, SUM(Deaths) AS pd, SUM(Attempts) AS pfa, SUM(Caps) AS pfc, SUM(MVP) AS pmvpFROM Scorecard WHERE Player='Greysoul'") or die(mysql_error());while($psum=mysql_fetch_array($playersum)){echo "<tr>";echo "<td>".$psum['Player']."</td>";echo "<td>".$psum['pgp']."</td>";echo "<td>".$psum['ps']."</td>";echo "<td>".$psum['pk']."</td>";echo "<td>".$psum['pd']."</td>";$pspg=$psum['ps'] / $psum['pgp'];echo "<td>".round($pspg, 2)."</td>";$pkpg=$psum['pk'] / $psum['pgp'];echo "<td>".round($pkpg, 2)."</td>";$pkd=$psum['pk'] / $psum['pd'];echo "<td>".round($pkd, 2)."</td>";echo "<td>".$psum['pfa']."</td>";echo "<td>".$psum['pfc']."</td>";$pfp=$psum['pfc'] / $psum['pfa'] * 100;echo "<td>".round($pfp, 2). "%" ."</td>";echo "<td>".$psum['pmvp']."</td>";echo "</tr>";}echo "</table>";

should i have another while loop around this one?

Edited by Greysoul
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...