Adding the value of a variable to a database field - php

I have a MySQL database table called submission with a field called points with a type of bigint(100).
On a PHP file, I have a variable called $commentpoints with a numerical value.
In the PHP file, how could I add $commentpoints to points?

It's a matter of adding it in your SQL update query:
$sql = 'UPDATE submissions
SET points = points + ' . (int) $commentpoints . '
WHERE id = ' . (int) $id;
That will add the value of $commentpoints to the points in the database; just make sure to use the where clause so you don't add it to every record in the submissions table.

UPDATE table SET points = '[your value]'

mysql_query("UPDATE submission SET points = '$commentpoints'");

$sql = "UPDATE submission
SET points = points + ?
WHERE commentid=?";
$q = $conn->prepare($sql);
$q->execute(array($commentpoints,$commentid));

Related

How to make a MySQL column value equal a variable in PHP

I have in my database a column called counter, i'm trying to save one of the rows of that column into a variable in PHP and make it equal the column rating. I do not want to make it from sql directly, I need to make it through php.
The problem is that when I run the query, the value for rating does not change at all. Please give me a solution
$cntrvalue = "SELECT counter FROM schools WHERE name = 'School1'";
$cntrResult = $this->conn->query($cntrvalue);
$mys1 = "UPDATE schools SET rating = $cntrResult WHERE name = 'School1'";
$this->conn->query($mys1);
Thanks
EDIT:
Sorry the code above gives an error, this is the code that changes nothing
$cntrvalue = "SELECT counter FROM schools WHERE name = 'School1'";
$this->conn->query($cntrvalue);
$mys1 = "UPDATE schools SET rating = $cntrvalue WHERE name = 'School1'";
$this->conn->query($mys1);
EDIT 2:
What i'm trying to do now is that i'm trying to get the value of a SUM query:
$mys4 = "SELECT SUM(`s1`+`s2`*2 +`s3`*3 + `s4`*4 + `s5`*5) FROM schools WHERE name = 'School1'";
$mys4Result = $this->conn->query($mys4);
$mys4Value = $mys4Result->fetch_assoc()[''];
The thing is that there is no column in the db to fetch from for this operation. What am I supposed to do? Thanks
Want you want to achieve, can be done within one mysql query, but if you just want how to fix that code:
You need to use the $cntrResult outside the string. Try string concatenation.
First you need to make sure, you have the correct value, as $cntrResult is not an string object, its an resultset:
$value = $result->fetch_assoc()['counter']
And the query:
"UPDATE schools SET rating = '" . $value . "' WHERE name = 'School1'";

adding a passed in number to a MySQL cell with PHP

I am trying to add a number to a number already in a MySQL table. I am using POST to pass in the number.
<?php
require "connection.php";
$id = $_POST["id"];
$number = $_POST["number"];
Do I have to make 2 separate queries, one to retrieve the current value and set is as a variable and one to insert the new value by adding the number to the variable or can I accomplish this with one query with something like this:
$update_query = "update Table set value = (value + $number) where id like '$id';"
Yes, you can use one query UPDATE tbl_name SET value_col = value_col + :number WHERE id = :id
Notes
ID is unique, so you need id = :id to achieve faster queries and only update one field.
Use prepared statements instead of putting values directly to query
update field to add value to existing value:
UPDATE table SET value = value + $number WHERE id = $id;
update field to contact value to existing value:
use the CONCAT function :
UPDATE table SET value= CONCAT('$number',value) WHERE id=$id;

How do I UPDATE a value in a DB and add another value to it

What I wish to do is to update the value stored in a db to the same value + another value.
Example:
$points=$_POST['points'];
$ptsupdt = "UPDATE kids SET points = points + '$points' WHERE name='$name'";
Currently when I run this statement, it adds double the value stored in $points.
Example: if $points=5 the updated value will be the original value + 10.
Just don't quote $points!
$ptsupdt = "UPDATE kids SET points = points + $points WHERE name = '$name'";
I was actually running the statement twice during testing and didn't notice until after I posted.
"UPDATE kids SET points = points + '$points' WHERE name='$name'" worked perfectly.

MySQL add data to row after existing data

I am trying to add new data right after the current data. This works well,
mysql_query("UPDATE uyeler SET shoots=shoots + 1 WHERE nick='admin'")
However, its only for numbers. I tried that with normal text value
$places = "london";
mysql_query("UPDATE uyeler SET shootplaces= shootplaces + '".$places."' WHERE nick='admin'")
But all it does is that it makes the shootplaces to 0. What is the correct way to add new value to a row without deleting the current value in it ?
You have to use Mysql CONCAT function.
$places = "london";
mysql_query("UPDATE uyeler SET shootplaces= CONCAT(shootplaces , '".$places."') WHERE nick='admin'")
Is your shootplaces has INT as datatype ?
Anyway, you can use concat() function to add new data into new cell.
UPDATE your_table
SET your_column = CONCAT(your_column, 'your new value') WHERE id=..
See this link for further help
try this.
$places = "london";
mysql_query("UPDATE uyeler SET shootplaces= concate(shootplaces , '$places') WHERE nick='admin'")

MySQL table row will not UPDATE

I am trying to update a column in a row in a MySQL table. The column is the 'votes' column and when someone submits an HTML form there is a hidden input with a value of "1" that gets submit and posted. This is the code I am using to try to update the vote count:
if(isset($_POST['image_id']) && isset($_POST['vote'])){
$image_id = $mysqli->real_escape_string($_POST['image_id']);
$vote = $mysqli->real_escape_string($_POST['vote']);
$sql_users_vote = "SELECT * FROM users WHERE id='$image_id'";
$result_users_vote = $mysqli->query($sql_users_vote);
$row_vote = mysqli_fetch_array($result_users_vote);
$votes_count = $row_vote['votes'];
$new_votes = $votes_count + $vote;
$sql_vote = "UPDATE users WHERE id='$image_id' SET votes=$new_votes";
$result_vote = $mysqli->query($sql_vote);
}
I have echo'ed out the variable up until $sql_vote and $image_id, $vote, $votes_count and $new_votes all echo out the correct values. I'm guessing that there is a problem in the UPDATE syntax. I've checked it over and over but can't seem to find anything. I know that I don't have quotes around $new_votes in the UPDATE because I believe that is correct syntax. I've tried it with quotes and it doesn't work that way either.
Can someone help me identify the problem? Thanks!
Doesn't the SET come before the WHERE?
$sql_vote = "UPDATE users SET votes = $new_votes WHERE id = '$image_id'"
Or does it not matter?
$sql_vote = "UPDATE users SET votes=$new_votes WHERE id='$image_id'";

Categories