incrementing a field in a table when insertion in another table occurs - php

Let's say I have two tables as shown:
user
id plan course_limit username
10 0 ahmad
note: plan is enum field containing '','a','b','c'.
course
id user_id username
1 10 ahmad
Now I want when a user insert into course as shown I want the course_limit to increment by 1 for that user so that I can apply a limit.

You can create a trigger with the following code.
CREATE
DEFINER = 'root'#'localhost'
TRIGGER databasename.AI_course_each
AFTER INSERT
ON databasename.course
FOR EACH ROW
BEGIN
UPDATE user SET user.course_limit = user.course_limit + 1
WHERE user.user_id = new.user_id;
END;
Some explanation
You create a trigger that fires once for every row FOR EACH ROW that is inserted AFTER INSERT into table course.
When you insert into the table you can trigger BEFORE and AFTER the insert is done.
If you want to be able to prevent the insert you fire before, if you just want to do useful work, you fire after.
The inserted fields can be accessed via a dummy table 'new'.
So here after each insert the UPDATE statement gets executed.
More trigger options
You can also use triggers BEFORE UPDATE, AFTER UPDATE,BEFORE DELETE and AFTER DELETE.
In the update and delete cases you get an extra dummy table old that you can use to refer to the data in the table before the update or delete happened.
Lots more is possible, but I'll keep it simple for now.
See http://forge.mysql.com/wiki/Triggers for more info.

Using a trigger should solve your problem : with that, you'll be able to register SQL code, on your MySQL server, that runs when certain events occur (like insertion).

Related

PHP pdo, how to store every query that executes to database as log to a table in same database

working with php5.5 and mysql5.5, have developed an application with the PDO connection.
now as log table I want to create a table(db_log):
id--------auto
query ----query
create----curent time
user------session-user
AND store every insert, update and delete action on database to a mention table, for example
there table named( tbl_temp) with some coumns and there user come and run query like
(DELETE * FROM tbl_temp where id = 1) from the user (user1)
here when this query runs like from the page of (delete.php)..
it should save the query on the table of db_log
id = 1
query = DELETE * FROM tbl_temp where id = 1
create = datetime
user = user1
so that how i will be able to record every action of user on database and control the user activity,
1 - here do i need to pass the query to db_log in every, page, or i can build a class
2- is there any good solution or example on web to learn.
You can create insert/update/delete triggers on all tables that you want to log. In that triggers you copy the data into your log table.
For instance:
delimiter |
CREATE TRIGGER log_insert_tbl_temp BEFORE INSERT ON tbl_temp
FOR EACH ROW BEGIN
insert into db_log (table_name, action, id)
select 'tbl_temp', 'insert', NEW.id;
END
|
delimiter ;

Modifying a record when a new entry is created

I am currently creating a database for a sort of game/reviewer based application. Whenever a user submits a review of a restaurant or such it adds points to his score. A summary of the SQL script would be:
CREATE TABLE user
userid CHAR(30)
user_name....
userpoints largeint
etc.
The table for the reviews is here:
Restaurantid largeint (auto incrementing)
restaurantname CHAR(30)
etc.
How do I program the app to give the points whenever a review is posted?
Use a trigger that gets fired automatically on every insert in the reviews table:
delimiter |
CREATE TRIGGER review_trigger AFTER INSERT ON `reviews`
FOR EACH ROW BEGIN
update user
set userpoints = userpoints + 1
where userid = NEW.reviewer;
END
|
delimiter ;
I assumed you have a column in your reviews table that relates to the user - I called it reviewer.
You can either create a Trigger on the Review table that will insert into user, or create a Procedure to handle both inserts which then gets called by your application.
You are going to want to do a bit of research into triggers. Triggers allow you to run SQL statements when records are selected, updated, inserted etc.
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
You could create one of these triggers to run on insert into the review restaurant table, and have it update the users table to add 1 to the userpoints row for that user.
you can do it using triggers if you want the database to handle the problem or you would program it in php by using an insert query or update query.

Mysql trigger variable - find updating user

I have table with fields:
id
data1
data2
next I want to set trigger that after update would write to log table changes:
CREATE TRIGGER `update_data` AFTER UPDATE on `data_table`
FOR EACH ROW
BEGIN
IF (NEW.data1 != OLD.data1) THEN
INSERT INTO data_tracking set old_value = OLD.data1, new_value = NEW.data1, field = "data1";
END IF;
-- similar for data2
END$$
I also want to record in data_tracking table user that made change, however this user is not part of original UPDATE that trigger the trigger. I it a way to let trigger know what user need to be recorded ?
This is PHP based web service with multiple registered users, that can make changes to record via website - those user i would like to add to trigger.
As you want to use the user name that only is known to PHP, well, MySql cannot know which user triggered the change if this information is only available in PHP. This means you would at least have to pass the user in every update statement, either adding this as a column to all tables that need this kind of trigger, or do all the updates via stored procedures that get the user name as an additional parameter. Then you could get rid of all the triggers, as you would use stored procedures anyway that can do the logging as well as the updates.
Use CURRENT_USER, see the MySQL manual: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_current-user

Alternative to block a table

I have a table called 'messages' (INNODB) where the user can insert their own posts.
I want to put a limitation. In my php script when the table gets to 10 records, you can not add more. The logic of the program is more or less as follows.
Step 1. I run a query to count the lines that are in the table.
Step 2. Recovered that value, I decide whether to insert a new post.
My difficulty is in properly managing the possibility of two user who do the same thing simultaneously. If the user A is in step 1 while user B has just finished entering the tenth post, user A will include the eleventh.
How to avoid it?
You can create CHAR(1) NOT NULL field and cover it with UNIQUE INDEX. This will prevent of inserting more than 10 rows.
Other solution that could work would be to create BEFORE INSERT trigger that checks number of rows and raises error if there are more than 10 (look here for sample) (but in this case you can fail with condition races).
In order to allow you to change your threshold value for the table, you can use a trigger. Because MySQL triggers don't have a "prevent INSERT" option, you need a value in your table set to NOT NULL. The trigger can then set the inserted value for that column to NULL which will prevent the INSERT if your condition check fails.
A trigger like this:
CREATE TRIGGER block_insert
BEFORE INSERT ON table_name
FOR EACH ROW
BEGIN
DECLARE count INT;
SELECT COUNT(*)
FROM table_name INTO count;
IF count >= 10
THEN
SET NEW.non_nullable_value = NULL;
END IF;
END;
would fail if you inserted an 11th row, like this:
ERROR 1048 (23000): Column 'non_nullable_value' cannot be null
You may wish to set the non-nullable column's name to something that represents its use. You could improve this by having the trigger pull the limit value from a configuration table.
Update
To avoid having to use the non-nullable columns, you could alternatively create an error procedure, and CALL it from your trigger - similar to the example in the "Emulating Check Constraints" section of this page - they're referencing Oracle databases, where a check constraint achieves what you want, but MySQL doesn't support them.
The "error procedure" in the example performs an INSERT of a duplicate row into an error table, causing a unique key error and stops the parent transaction also.
Update 2
As pointed out in the comment below, multiple simultaneous transactions may get round the checks - you'll have to use LOCK TABLES <name> WRITE in order to ensure that they can't.
2/ You can also lock the MySQL table.
execute : LOCK TABLES my_table
Then do your business rules.
execute : UNLOCK TABLES
This also ensure that each action is sequentially executed. (but you have to deal with performance overhead)
Hope this could be useful.
Updated since the comments below : transaction don't work in this case
1/ You are using InnoDB, you can also use database transaction
Open transaction
Then do your business rules.
Commit or rollback your transaction
This will ensure that each action are executed one after another.

Monitoring MySQL database using PHP

I have a mysql database with 12,000 entries, what i want setup is the ability to monitor a column in the database and if/when the column is altered for any entry it sends me an email with the details.
EDIT: I have access to mysql db, but not the script which works with it. So it should monitor it for changes...
You could create some triggers on the table, if your version of MySQL has them. A trigger can then invoke any function you care to create. A trigger has the advantage that any insertion or deletion or any update of the column will cause it to fire; you wouldn't have to change any other code to make it happen. See here for more... http://dev.mysql.com/doc/refman/5.0/en/triggers.html
Create a trigger on update
Create another table (lets call it cron_table), where the trigger will insert information of the updated row (may be old value, new value etc)
Setup a cron, which will call a script which will check the cron_table and send email if any entry is found. Cron interval can be setup according to need.
--- If you could send email from trigger, there would be no need for a separate table and cron ---
try something similar to this , you can edit the function to send you and email if the query has insert and TABLE_NAME or COLUMN_NAME in it
set up one column to be a datetimestamp.
This will update on every change of the row. There you can run a sql query either via a cron job or after every few php queries to return you the list of changed rows since the last check.
Select * from tbl_myentries where EntryUpdated > '$TimeSinceLastCheck'
you need to understand Data Manipulation Language (DML) triggers
in my sql: use
CREATE TRIGGER salary_trigger
BEFORE UPDATE ON table_name
REFERENCING NEW ROW AS n, OLD ROW AS o
FOR EACH ROW
IF n.columnName <> o.columnname THEN
END IF;
;
Create a trigger on a change on your column, then insert it to another table as log table.
Run cron job on your table that will send you an email.

Categories