How to make an addition to a MySQL column? - php

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.

Related

Mysqli add numeric value with concat?

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!

PHP Counter for a unique reference from a button

I'm looking to use a counter to give me a unique referencing system. I want to click a button which then checks a field/file with the last number in it and then simply adds 1 to it then inserts it into a fields on screen?
not sure the best way of doing this or exactly how to do this as i'm still learning php.
any help would be appreciated.
You can use the count() query in sql and just add one.
Here's the count tutorials.
One there. assign the query into a new variable and increment it:
$q = mysql_query("SELECT COUNT(column_name) FROM table_name;");
$q++;

What is the best way to expand existing data in a database with PHP

So I've got a field in my database that will contain serial id numbers separated by commas eg. 2817,2385,4937,3298 I want to be able to add more numbers to the same field over time.
The best way I can think to do this is to get the contents, add the new numbers to it, and insert them back into the database.
What I'm wondering is if there's a more direct way. I had trouble thinking of a good way to word this that yields helpful search results so I'm asking here.
Yes there is.
UPDATE `table` SET `column` = CONCAT(`column`,',new_serial')
However this is not right, you should never store comma separated values. It's called database normalization.
Try this:
UPDATE `tableName` SET `yourColum` = CONCAT(`yourColumn`, ',nextId')
This will update your column as you requested.

PHP Pass user id to mysql trigger

I need to create a log table and as expected I need to store the user id and the old and new row values.
The thing is that I want to create a trigger to do that to me but I know that is not possible to send parameters to triggers.
Is there a way to do that?
Thanks in advance for any help.
Another hack: you can use connection based variable #var.
sample:
set #user_id = 321
update table .....
set #user_id = null;
and in update trigger you can use this variable #user_id.
Not without some sort of ugly hack - which could be a trigger to think in something else than triggers.
Now for one posiibility of the ugly hack:
Assuming you username can't contain \n, you could do something like
UPDATE <tablename> SET <fieldname>=CONCAT('$username','\n','<newvalue>') WHERE <primarykey>=<something>
and then let your trigger take off the prepended username and newline. Again: This is an ugly hack.

Auto Update query

I wanted to know whether i can direct update mysql datas to add (+) the values, without needing to fetch them.
For example:
My database is like this,
data1 = 56
so to add a 4 to it, i will first fetch the data from the database then,
$data1 = $data1 + 4;
and finally again perform a Update query.
So is there is way i can send like add +4 to the current value of data1.
I am using php lanuage.
Thank You
Like... this?
UPDATE
MyTable
SET
Data = Data + 4
WHERE
Myid = 123
A look into basic SQL syntax seems advisable. ;-)
If you are working with PHP to manipulate your data, a deeper look into the mysqli_* functions family in general and prepared/parameterized statements in particular is advisable as well.
mysql_query("UPDATE `mytable` SET `data1` = `data1` + 4");
You'll want a WHERE clause unless you want to update every row.

Categories