/    Sign up×
Community /Pin to ProfileBookmark

Update a score

Hello,

I’ve created a little weekly trivia game for my website. Basically its five questions, then at the end the user can add their score to a scoreboard.

The problem is that I want the scores to carry from week to week and cumulate. So let’s say you got 4 points one week, then 5 points the next. I want the scoreboard to reflect you have 9 points.

So I created a small form with an invisible field that has the users score, a field for the username, and a field for the e-mail address. Next week, when the user takes the quiz again, I want their score to be updated if the username and e-mail match a record in the database. If no record does match, I want an entry to be created.

Here’s the script I came up with, however, it doesn’t work (which doesn’t surprise me, I’m pretty new to PHP/MySQL).

[code=php]
$name = $_POST[‘name’]; //The Username
$score = $_POST[‘submitscore’]; //The users score (0-5)
$email = $_POST[’email’];//Users email address
$date = date(“F j, Y, g:i a”);//The date and time

if($name != ”) {
$qry = “SELECT * FROM scoreboard WHERE name=’$name'”;
$result = mysql_query($qry);
if($result) {
if(mysql_num_rows($result) > 0) {
$sum = ($row[‘SUM(score)’]+$score);
“UPDATE scoreboard SET score = ‘$sum’ WHERE name = ‘$name'”;
}
else
$q = mysql_query(“INSERT INTO scoreboard (`name`, `email`, `date`, `score`) VALUES (‘$name’, ‘$email’, ‘$date’, ‘$score’);”);
@mysql_free_result($result);
}
else {
die(“Query failed”);
}
}

[/code]

My table `scoreboard` looks like this

id name email date score

1 J.Doe [email][email protected][/email] 7/27/11 4

Thanks!

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@Andrew977authorJul 28.2011 — OK, well I got this partially working with an [code=php]ON DUPLICATE KEY UPDATE[/code]However, when it looks for duplicate entries, it only takes the name column into account (I want it to also take the email column into account). Is this possible?
Copy linkTweet thisAlerts:
@GalwayJul 28.2011 — I don't really see anything in the first post that shouldn't work (at least not glaring logic mistakes). What kind of errors are you getting or how isn't it working?
×

Success!

Help @Andrew977 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...