Hi,

I'm currently working on a site in which a user basically clicks a button repeatedly, gaining 1 extra point each time he clicks it.

Anyway, I'm trying to do a MySQL update so that when the button is clicked, it updates his score in one of the MySQL tables:

<?php
if(isset($_POST['update'])) {
mysql_connect("localhost","XXX","XXX") or die(mysql_error()); 
mysql_select_db("refrigerator") or die(mysql_error()); 

$username = $_COOKIE["user"];
mysql_query("UPDATE count SET count='5' WHERE username = '$username'");
}
?>

When the button is clicked, nothing happens - no error, no nothing.

This did work once, when I asked it to change count to '55555', but then I changed something and it stopped working. I did undo everything I did to try and get it working again, but couldn't manage it.

Any help would be appreciated :)

Recommended Answers

All 6 Replies

Modify your update statement to the one below

mysql_query("UPDATE count SET count=(count + 5) WHERE username = '$username'");

count is a keyword, so you MUST wrap it in backticks. As a matter of fact, it is better if you always put backticks around your field and table names, that way you will always avoid this problem:
NOTE: The general syntax is: UPDATE `TableName` SET `fieldName`='value' Based on what you posted, the sql should be: mysql_query("UPDATE `count` SET `count`='5' WHERE `username` = '$username'"); Are you sure that BOTH your tableName AND your fieldName are named count? If not, fix your statement accordingly.

Yeah, both the table name and field name are called 'count'. And I don't want it to set count as 'count+1' just yet, so that won't help I'm afraid.

With regards to the backticks, I'll give it a go but I haven't used them anywhere else in the site and it's seemed to work fine without them.

Thanks

mysql_query("UPDATE <Table Name> SET count=(count + 5) WHERE username = '$username'");

shouldn't you close the mysql connection in the end ?

I believe your query is wrong. If you want to increment the count when a user clicks you would need to add count plus 1

$query = mysql_query("UPDATE count SET count = (count + 1) WHERE username = '$username'");
//Check if query has ran.
if(!$query){
    die('Error with query').mysql_error();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.