Reputation: 103
I have been searching the site and seen a number of posts that are similar to what I need , but not quite. This is what I need to do...
I am building a tacking page on my site to track the total number of wins for each player on my Darts team.
I need to be able to update the wins field in MySQL with the number of wins entered into the input form.
e.g.
If a player has 7 wins to date, and the following week he puts up another 5 wins, I need to add those 5 wins the the existing 7 and update the field to show the total of 12 wins to date.
I can add a fixed amount with the following statement in PHP:
UPDATE table SET wins = wins + 1 WHERE id = 1
In need to allow for a random value where I'm currently using "+ 1"
I'm still rather unfamiliar with more advanced PHP, so any help would be appreciated.
Thank you.
Upvotes: 1
Views: 1192
Reputation: 3286
Wrap your update button in a <form>
and make it POST to your backend (in fact PUT would be more appropriate here as you are updating existing data)
In the backend,
get your input value as $_POST['input_name']
and construct your SQL statement accordingly
$input_value = $_POST['input_name'];
$query = sprintf("UPDATE table SET wins = wins + %d WHERE id = %s;",
$input_value , $id);
This is very similar to @Makesh's answer, however also takes care of SQL injection
Upvotes: 0
Reputation: 578
when a user win then this win will enter into database corresponding this user and date. and when we want to update then just count the rows of wins of that date and then get it in a variale and update as you like.
Upvotes: 0
Reputation: 870
you can use the following:
$query = "UPDATE table SET wins = wins + $addValue WHERE id = 1";
mysql_query($query);
The variable $addValue can be any random value you want.
Upvotes: 1
Reputation: 1234
Try this :
//You have mentioned a random number in your question
$number = rand(); //rand(1,10)
//If you want use the number from input form .Try like
//$number=$_POST['input_field_name'] ;
$my_sql_query = "UPDATE table SET wins = wins + $number WHERE id = 1";
Upvotes: 1