update a field in another table when inserting record

Any tips and tricks that has to with the ADDT that doesn't fit into one of the other categories
piripacchio
Posts: 37
Joined: 2010-04-28 08:34

update a field in another table when inserting record

Post by piripacchio » 2011-09-09 08:30

Hi all,
I have two tables in a mysql db:

TABLE A (classroom)
id_cla [int(3)]
name_cla [varchar(255)]
seats_cla [int(3)]
max_seats_cla [int(3)]

TABLE B (students)
id_stu [int(3)]
name_stu [varchar(255)]
classroomid_stu [int(3)]

I have a form where the student will be able to subscribe to a classroom.
When the student will subscribe he will select the classroom from a dropdown menu.
Once he submits the form a record will be inserted in Table B, containing student name and the choosen classroom; at the same time the seats_cla in Table A should be updated/incremented (adding 1 to the actual field content).
Is there a way to do this with ADDT?

TIA for any help.

tony

User avatar
Fred
Site Admin
Posts: 491
Joined: 2010-02-15 12:10
Location: Armagh, Northern Ireland
Contact:

Re: update a field in another table when inserting record

Post by Fred » 2011-09-09 19:40

A custom trigger of type AFTER should do.

Code: Select all

function Trigger_Custom(&$tNG) {
$id_cla = $_POST['whatever class he selected'];
mysql_query("UPDATE classroom SET seats_cla = seats_cla+1 WHERE id_cla = '$id_cla'");
}

Post Reply