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.
Related
I'm a couple months into grasping PHP and MySQL basics.
I am currently querying table A and displaying it live as an end user (ie, mysite.com/includes/querying.php). I'm echoing out all the different fields in table A to display beside two decision buttons; accept and deny. The data is being produced from form requests each as a new row. I'd like to have the control to determine if I want to accept or deny each request (row) independently.
My goal is to take this data querying from table A and select accept or deny and have the row written to another table; table B for querying additional data.
For example, the row in each table itself in it's simplest can have two fields, name and status. By default I have the status on all new rows set as PENDING. So I query Jon Doe and PENDING. Jane Doe and PENDING. Joe Doe and PENDING, and so on.
Once a new row of data is queried from table A and I select accept, the accept button forces writing this information into table B, switching the status from PENDING to Y or N and removing it from table A. Most of this is easy to complete in a couple steps.
Also, I started to make some progress by having the form submission write the data to BOTH table A and table B on submit. Then I'd only need to update the status from PENDING to Y or N in table B. However when I tried using WHERE I could only write a blanket condition that updates the status column in every row in table B which is below. I am clueless on how to make this statement specific to only the row I am selecting.
$query = "UPDATE table B SET confirmed='Y' WHERE confirmed='PENDING';
So I was wondering if there is a simple way to base the field update from PENDING to Y / N by checking an auto-increment ID, an email address, etc, basically something unique against itself?
As for resources I've utilized Learning PHP, MySQL & JavaScript from O'Reilly and completed many Google searches with failed attempts and phrasing.
You might want to rethink your database design, and google 3rd normal form. You want to avoid duplicating data. I recommend using 1 table and adding a column, or have an additional table that you join to.
But to answer your question you can use the AND keyword in your query.
UPDATE table B SET confirmed='Y' WHERE confirmed='PENDING' AND firstName = 'Jon' AND lastName = 'Doe';
Doing an update by the primary key would be best, because you could have multiple people with the same name. Ex:
UPDATE table B SET confirmed='Y' WHERE id = 1234;
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 ;
I have decided to create a new table in my database that will be used for auditing all actions performed on my database.
My table "activity" is fairly simple and contains the following columns:
activity_id - the primary key
user_id - foreign key of the user
action - type of query being performed (SELECT, UPDATE, DELETE)
table - name of affected table
row - name of affected row(s)
What I am doing at the moment is when a query is performed I then have another query after it which inserts information into my "activity" table.
CODE:
//query
$db->query("DELETE FROM foo WHERE id = '$foo->id'");
//activity record query
$db->query("INSERT INTO acitivity ( user_id, action, table, row ) VALUES ('$user->id', 'Deleted' , '$foo->id', 'foo')");
As you can see I am having to do this manually for each query and I have over 100 queries in my system and I really don't want to have to type a individual query to record what happens.
I was wondering is there any features in MySQL that can tell me what actions have been performed or is there any libraries or techniques in PHP that can help me?
Or am I doomed to write out 100 individual queries in my system?
Thanks
Create ON INSERT, ON_UPDATE and ON_DELETE triggers for your table that write details of all changes to an audit activity table
What about to create triggers?
Thia is not good idea to make auditing in PHP.
How about you make a new method to your class, say $db->ql() (from query/log) that first makes the normal query, than takes the mysql_affected_rows() and it does something like
insert into logs set when = now(), query_string = "{$query}", affected_rows = "{$affected_rows}"
That way you'll have a complete history of your queries, not only the type of query and the table it was run on.
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).
In MySQL, is it possible to have two tables linked by a foreign key and when a record is inserted into the parent table to create a corresponding record in the child table?
I have a website that handles member registrations. One table contains the member's details such as name and email address, and is linked to a settings table via member ID. What I would like is for a corresponding record to be entered into the settings table automatically when I create a new member. Is this possible? Or will that involve a stored procedure?
I think what you might need is a trigger.
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
CREATE TRIGGER ins_settings AFTER INSERT ON members
FOR EACH ROW BEGIN
INSERT INTO settings SET member_id = NEW.member_id
...
END;
Thanks to Greg, I managed to arrive at the solution, which is:
CREATE TRIGGER ins_settings AFTER INSERT ON members
FOR EACH ROW
INSERT INTO settings SET member_id = NEW.member_id;