Potatis Posted January 25, 2008 Share Posted January 25, 2008 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? Quote Link to comment Share on other sites More sharing options...
Zane Posted January 25, 2008 Share Posted January 25, 2008 Change this `points` = '($points + $last)' WHERE to this `points` = (points + {$last}) WHERE Quote Link to comment Share on other sites More sharing options...
Potatis Posted January 25, 2008 Author Share Posted January 25, 2008 Thanks for your reply zanus, I tried it, but it isn't working. It did make a change though, it changed the value of the total points to zero, but the last round points stayed on 1. Quote Link to comment Share on other sites More sharing options...
dg Posted January 25, 2008 Share Posted January 25, 2008 where is $points ? .... if it isn't there than initialize it to zero Quote Link to comment Share on other sites More sharing options...
Potatis Posted January 25, 2008 Author Share Posted January 25, 2008 $points is a variable I set up near the top of the page. It had a value of 1. I set it to zero, but it had no effect. Should I place the variable elsewhere in the script? Quote Link to comment Share on other sites More sharing options...
dg Posted January 25, 2008 Share Posted January 25, 2008 are u storing the data what is ther in the database in the last in $last ?? or try this `points` = `points` + '$points' (one is from the database) Quote Link to comment Share on other sites More sharing options...
Potatis Posted January 25, 2008 Author Share Posted January 25, 2008 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. Quote Link to comment Share on other sites More sharing options...
Potatis Posted January 25, 2008 Author Share Posted January 25, 2008 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. Quote Link to comment Share on other sites More sharing options...
Potatis Posted January 25, 2008 Author Share Posted January 25, 2008 Change this `points` = '($points + $last)' WHERE to this `points` = (points + {$last}) WHERE Thanks so much, this worked after I sorted out my db problem! Quote Link to comment Share on other sites More sharing options...
Zane Posted January 26, 2008 Share Posted January 26, 2008 glad that it worked out for you Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.