PHP Pass user id to mysql trigger - php

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.

Related

PHP/ACCESS/MYSQL Get the auto increment value and adding it to another column with a text behind it

I don't know how to explain it therefore I don't know how to search for it. I have this insert to in php which looks like this:
$sqlfatura="INSERT INTO faturas_online (cod_fatura,email,totalapagar,data) VALUES ('FO+num_fatura','$login_cookie','$total','$data')";
I have an auto-increment field in my database which is receipt_number and I want it to be on receipt_code with FO before it so I can differentiate it from store receipts, but I don't know how to do it.
How can I make this work?
May be this is what you want $sqlfatura="INSERT INTO faturas_online (cod_fatura,email,totalapagar,data) VALUES ('FO.$num_fatura','$login_cookie','$total','$data')";
You'll need to tell us what method you're using for mysql; the answer is different with a pdo vs msqli. You have to read the insert_id after a successful insert. With a pdo you'd do
$pdo = new PDO(mysql...)
if ($pdo->query("INSERT ..."))
$id = $pdo->lastInsertId();
Where $id is the auto_increment key. You can of course not use an autoincrement variable and create the receipt number yourself.

Is it possible to add a add a number after select a value?

I trying to add +1 in a column after select but its not working, what I want is, when I make a search, the scripts adds +1 in a column to track how much searches I did.
Heres how it is now
$QUERY = "SELECT company FROM test WHERE number = '$number[0]' LIMIT 1";
And I want to add this
UPDATE users SET consultas=consultas+1 WHERE username = '$username'
If I add another $QUERY line the script breaks, any ideas ?
By nature, SELECT queries are for returning information from the database, not updating the database. To this end, triggers aren't even available for SELECT queries to react to the action. As such, if you want to increment a value, this must be done in a separate query, as an UPDATE query or possibly an INSERT ... ON DUPLICATE KEY UPDATE query if that better suits your needs.
You should execute those as two separate queries. Also, be very careful to ensure your data is properly escaped because it looks like you've forgotten to do that.
Be sure to check the result code of each as an error may occur at any time. If you use PDO there's a fairly robust error handling pattern you can follow.

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.

How can i get the last affected table in php

I want to get the last affected table name (after insert).
I tried with mysql_insert_id() but i got only id.
I want table name also.
Can anyone give me the idea for my problem
It's a strech... But if :
you are running mysql 5.6.3 +
you still have access to the insert query (let's say it's $query)
you are sure it's an insert query (because, hey, you know you want last_insert_*, don't you?)
You can try:
$row = mysql_fetch_assoc(mysql_query("explain $query"));
$table = $row['table'];
From mysql 5.6.3+ you can combine explain with a insert into query.
This should return only 1 row, I think.
I dont have mysql 5.6.3+ myself to test it.
You can override mysql_query function as defined in this post, save you last call table name in global var or session and pray for last called insert finish last.
// THIS JUST SAMPLE... use code from link below ^
function custom_mysql_query($query){
if(strstr('INSERT',$query)){
/*GET YOUR TABLE NAME WITH REGEX*/
}
basic_mysql_query($query);
}
This is very very very bad solution for me. But in theory will work.
You mention in your comment that you're using a common function to apply the changes in, preventing you from 'knowing' what the last table is you inserted in.
If that's the case: your logic is flawed.
MySQL is not gonna tell you what the last table is it did an INSERT in. You will have to write a hook in your function where you last know what table you're gonna update. No matter what function you use, you must specifiy a table name at some point, before executing the insert. You store the table name at that point.
You can do that in many ways, depending on your needs:
store it in a mysql table (last_updated_table with only 1 column,
for example)
store it in a variable (if you only need it in the
same request)
store it in a session (I wouldn't opt for this)
You can use the information_schema database if you have one :
SELECT TABLE_SCHEMA, TABLE_NAME, UPDATE_TIME
FROM TABLES
ORDER BY UPDATE_TIME DESC
LIMIT 0,1
Use this after your query, and you'll get the affected table.

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.

Categories