adding a passed in number to a MySQL cell with PHP - 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;

Related

Check which columns were modified in an UPDATE query

When we update a MySQL record with php, we can check if it has effect using:
$mysqli->affected_rows;
But how do I check which column has been modified?
Example, in my table have the columns: id / name / age
In a record we have the data: 1 / Woton / 18
If I send an: UPDATE mytable SET name = 'Woton', age = '20' WHERE id = '1'
Only the age field has changed. How can I determine this?
You cannot directly get the updated columns from the query result.
It can be get from some php query. Firstly we will have to select the row from database which we are going to update in a array variable. Than run the update query for the same row.
Lastly get the same row from database from select query in the new array variable.
Finally we get two arrays.
We can get the updated column with the array_diff_assoc php function.
See the below code for the same.
$sql = "SELECT * from mytable where id=1 limit 1";
$prev = mysqli_fetch_assoc(mysqli_query($conn, $sql));
//Get the column data in the array. Before update.
$sql = "UPDATE mytable SET name = 'Woton', age = '20' WHERE id = '1'";
$conn->query($sql);
// Update data
$sql = "SELECT * from mytable where id=1 limit 1";
$updated = mysqli_fetch_assoc(mysqli_query($conn, $sql));
// Again run the select command to get updated data.
$UpdatedColumns=array_diff_assoc($updated,$prev);
In a different note: If QueryLog has been enabled in the DB then you (or your script in PHP or Python or any) can easily see/read which part of the content has been updated and even you can monitor the DB.
The good news is, even you can target which table, which query etc.

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'";

Update column value based on PHP variable which is equal to a column name

$rating = mysqli_real_escape_string($conn,$_POST['rating']);
$id = mysqli_real_escape_string($conn,$_POST['id']);
mysqli_query($conn,"UPDATE table SET $rating=$rating+1 WHERE id='$id'");
Is there any way to update a column based on the PHP variable $rating? $rating is a column name.
Also, this may be prone to security risks etc, so I'd like to know if this is even a good way to go about it.
Yes you can use variable name as field name in the sql. However you must validate it first before putting it into sql string. Since its not a field value, you cannot "quote" it.
$rating = $_POST['rating'];
// Define list of valid "rating" db field names here
$valid_fields = Array('rating_a', 'rating_b', 'rating_c');
if (in_array($rating, $valid_fields)) {
$id = mysqli_real_escape_string($conn,$_POST['id']);
mysqli_query($conn,"UPDATE table SET $rating=$rating+1 WHERE id='$id'");
}
First and foremost your sql is not correct because I don't think a variable can be a column name in mysql. So please check in the mysql database for the exact column you want to update. So your sql will be something like
$rating = mysqli_real_escape_string($conn,$_POST['rating']) + 1;
$id = mysqli_real_escape_string($conn,$_POST['id']);
mysqli_query($conn,"UPDATE `table` SET `rating`=$rating WHERE id='$id'");
If rating is not your column name then change it to the exact column name.
Hope I helped.

Can I retreave a variable from mySQL database while I am doing UPDATE?

I'm running this query:
if($ModifiedInGoogle != $row['ModifiedInGoogle']) {
// run update query
mysql_query("UPDATE events
SET EventName = '$EventName',
StartDate = '$StartDate',
WHERE GoogleID = '$GoogleID' ");
If the EventID variable is not being changed in the UPDATE, is there a way I can get it and assign it to an $EventID variable here?
No. UPDATE queries are for putting data into the database. If you want to retrieve data, then you use a separate SELECT query.

incrementing a table column's data by one || mySql

iam having a table with columns like
id || counter
if i do something (some event) i want the counter's value(at a particular id) to increase by one , currently iam doing this :
//get current value
current_value = select counter from myTable where id='someValue'
// increase value
current_value++
//update table with current value
update myTable set counter=current_value where id='someValue';
currently iam running 2 queries for this, please suggest me some way do it in one step.
Just run the math in the database:
update myTable set counter = counter + 1 where id = 'someValue';

Categories