Jump to content

[SOLVED] PHP sums


Potatis

Recommended Posts

I've spent many hours to get this far, and I'm stuck.

 

My code is supposed to send information to the database to find out who (ploural) picked the winning answer, and then add a point to their points in the database.

 

Here is the code:

// Game 1

//finds username of winners who picked the answer (This bit works but maybe could be done better?)

$result = mysql_query("SELECT username FROM games
WHERE game1='$winner1'");
while($row = mysql_fetch_array($result))
  {

$name1 = $row['username'];
  echo $name1;
  echo "<br />";

//Attempt to update db to add $last (last round points) = 1 to total points (not working)

mysql_query("UPDATE `afl08db`.`users` SET `last` = '$last',
`points` = '($points + $last)' WHERE `users`.`username` = '$name1' LIMIT 1 ;")
or die(mysql_error()); 
}

 

The way I am getting the info about who the winners are may be poor, I don't know, but it works. The echo does indeed list the winners in my sample database and those who submitted wrong answers are not on the list.

 

I don't know how to use the list of names in the $name1 variable to update their score in the users table.  The code does add 1 point to the winners points, but that's it, it doesn't add any more if I resubmit the form. The score in the database is always 1. What change do I need to make to get the score to increase when the user gets a correct score in the next game?

Link to comment
Share on other sites

I found a problem that the scores in the databases were put there earlier by code that worked, except it I could not add to the numbers in the database. The code I posted initially doesn't work, I emptied the database and when I opened the page again, it submitted nothing to the database.

 

However it DID echo out the correct winners, so that part works. Nothing in the replies has worked but that is probably because my code for updating the database is wrong. I'll upload the whole thing, which is not long and I'll see if anyone can spot what is wrong.

 

<?php

//Connect to server
$con = mysql_connect("host", "user", "pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  //Connect to database
  mysql_select_db("afl08db", $con);

$winner1="answer1";
$last="1";
$points="0";


// Game 1

//finds username of winners who picked the answer (This bit works but maybe could be done better?)

$result = mysql_query("SELECT username FROM games
WHERE game1='$winner1'");
while($row = mysql_fetch_array($result))
  {

$name1 = $row['username'];
  echo $name1;
  echo "<br />";

//Attempt to update db to add $last (last round points) = 1 to total points (not working)

mysql_query("UPDATE `afl08db`.`users` SET `last` = '1',
`points` = '1' WHERE `users`.`username` = '$name1' ;")
or die(mysql_error()); 
}


?>

 

dg, last is where I store how many games a person got right, because I want to have several on one form. This script I am posting is just to get the first one to work, then I'll duplicate with $name2 and $winner2 type variables. $winner1 is just the variable for the answer. The amount of games they get right goes in the 'last' field and I want to add that value to 'points' which is a grand total from previous games.

Link to comment
Share on other sites

I should add that this is done by a form that an admin submits the correct answers by a form in the admin panel by using a radio bar that is basically yes/no. The scores are then sent to the database.

 

Of course it's easy to write the scores in a text box and submit to that user's score in the database, that's how it works now, but it is tedious to do it for each person. I'd rather check a radio button for the right answer and the script works all the scores out after that.

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.