Mysqli add numeric value with concat? - php

I need to update a the employee hours column with the current numeric value that is already existed in the database.
$mysqli->query("UPDATE `".MYDATABASE."`.`".MYTABLE."`
SET employeeHours='".$employeeHours."'
WHERE sessionID='".session_id()."'");
This like is working properly but needless to say that it's overwriting the old value.
My goal is to add the current $employeeHours to the existed value in the database.
I assume that it's done with concat but I'm not sure about the syntax.
Small tweak needed here...

I couldn't believe that it would be so ridiculously simple :
$mysqli->query("UPDATE `".MYDATABASE."`.`".MYTABLE."` SET employeeHours = employeeHours + '".$employeeHours."' WHERE sessionID='".session_id()."'");
Thank you #castis!

Related

Concatenating original value with query value in MySQL

I am trying to something in MySQL that I do in JavaScript/PHP all the time. I need to concatenate the value of a field in MySQL with a value that is passed into a query from PHP. For example, let's say I have a field called favourites with a value of 27 and I have this query:
UPDATE useraccs SET favourites = favourites + ',30' WHERE id='10'
My desired new value for favourites would be 27,30, but I'm getting 57, where clearly SQL is adding them numerically. I have set the data type for this column as TEXT and was hoping that would force SQL to treat it as a string all the time, but that doesn't seem to be the case.
In my research I read about the CONCAT() function, and I tried this:
UPDATE useraccs SET favourites = CONCAT(favourites,',30') WHERE id='10'
That results in a failed query. The logic feels right but that is obviously not how that function is meant to be used.
I acknowledge that in theory, I could just grab the original value of favourites and concatenate it with the new value in the PHP itself and then send it to MySQL, but I feel like there MUST be a way to do this in one query...if I'm wrong about that so be it, but I'm sure there must be a way.
Use the following to create '27,30':
CONVERT(favourites,char) + ',30'

Mysql Table Update operation failing for no apparent reason

I have written some PHP/mysql code for a form-based subtitling program -- but there is one field in particular that I cannot get MYSQL to UPDATE for me. I have been looking for the problem for hours and I just can't find it. Below is, I think, the most relevant piece of code for resolving the problem. The field in question (TotalSeconds) definitely exists on the 'captions' table and, as the comments show below, the $TotalSeconds string also exists (at least prior to the update attempt) -- and yet all attempts to update the TotalSeconds field with the $TotalSeconds string value are failing. The TotalSeconds field, by the way, is of the type DECIMAL(11,3) (which must be related to my problem, since I'm new to using that field type).
$TotalSeconds=$_REQUEST['TotalSeconds'];
/* NOTE: I can get the TotalSeconds string variable to echo successfully before the update attempt below */
$result=mysql_query("UPDATE captions SET TotalSeconds='$TotalSeconds' WHERE ID='ThisID'") or die(mysql_error());
$k=mysql_affected_rows();
And yet the UPDATE operation above never works. The affected rows value always = 0. And yet I have nine other fields which are set to update the same way, and none of the others give me any trouble at all.
Any ideas would be greatly appreciated.
Thanks,
Brian
OP says there was a typo.
(Now, not 'Unanswered'.)

Using PHP to increase database table value by one on form submit

I am trying to increase the integer value of a database field value each time a form is submitted.
My table is simple. Only two relevant fields are Name and xp, and relevant values are Brian and 1.
I've gotten to the point I can call the PHP function on form submit, but I can't quite figure out how to declare the variable for the specific field I'm looking to increase ("1") or how to plug it into the UPDATE query.
If want to just update it with an arbitrary number, I know I can use...
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE points SET xp=9 WHERE Name='Brian'";
mysql_query($UpdateQuery, $conn);
But how do I declare the existing ("1") from the database as a variable, and then plug it into the UPDATE query so that it's something like ....."UPDATE points SET xp=($result + 1) Where Name ='Brian'"; and where $result is the variable for the desired database value, in this case ("1").
Desired result is that the database value for xp where Name = Brian is now ("2").
I tried using the fetch/select functions to declare the database value as a variable but could not quite wrap my head around it. Guess I'm kind of new at this. Help would be really appreciated.
Assuming that the column xp is cast a numeric type, then you can use:
$UpdateQuery = "UPDATE points SET xp=xp + 1 WHERE Name='Brian'";

How to make an addition to a MySQL column?

I want to make a "like" sort of button. It should basically +1 a value on the database called Likes. I can simply retrieve the value, do the addition and insert it again but I'm wondering if there is an easier way. Thanks
UPDATE Likes SET likeCount = likeCount + 1 WHERE likeID = ?
If you need to keep track of a value, then it is necessary that you store it. In the alternative, you might want to consider using a normal text file if you don't already have a database running. Hope it helps.

adding to numerical value of column

Hi so I was wondering what the best way to add or subtract from a field in my table would be. The way I know it would work is if I query the value do the addition and then UPDATE the value.
I would rather include the addition as part of the update query like:
UPDATE users SET points = +10
Or something like that. Is it possible?
You just need to name the column on the right hand side of the = operator:
UPDATE `users` SET `points` = `points`+10
Since there is no WHERE clause this will give all users 10 more points then they currently have.
You can just do this:
UPDATE users SET points = points + 10

Categories