Solved

mySQL Delete Trigger & php

Posted on 2011-09-29
2
492 Views
Last Modified: 2012-06-27
I want to create a delete trigger that will run before a record gets deleted, however I would like to get some external data passed to this trigger and have no idea how to do this or even if it can be done.

The delete trigger will take the record about to be deleted and paste it into another table, but I also want it to take the username from php - value of $_SESSION['login_user'] which holds the username value of whoever is logged into the app and is trying to delete the record.

Since the Trigger is in the database and not in the php code, how do i get this info to it??
0
Comment
Question by:saunaG
2 Comments
 
LVL 24

Expert Comment

by:johanntagle
ID: 36818295
I don't think MySQL triggers have that capability.  It can only use data from the table involved and (I think) from the MySQL session.  Suggest you do the copying to another table in your php code.  OR, still make the trigger, then have the php code update the row in the other after it calls the delete.  This was you will always copy the row to the other table, whether the delete is called by php or not.
0
 
LVL 77

Accepted Solution

by:
arnold earned 500 total points
ID: 36864864
Which version of mysql are you using?
http://dev.mysql.com/doc/refman/5.5/en/triggers.html
This thread might be what you are looking for
http://forums.mysql.com/read.php?99,140057,140057
The last post seems to be the example you are looking for
http://forums.mysql.com/read.php?99,140057,141249#msg-141249

The last part dealing with the take value, is not possible.

You should create a log table which will record transactions performed on the site at which point you could update/record which records were affected.
i.e. instead of actually deleting records, you could tag the record as delete using a deleted column and then reference who issued the delete. You would then have a cleanup process that will go through the table looking for rows marked for deletion and perform the transaction you want i.e. get the row into another table with the reference to the user that deleted it.

I think this approach gets to the point you want to achieve.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question