Page 1 of 1

update a field in another table when inserting record

Posted: 2011-09-09 08:30
by piripacchio
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

Re: update a field in another table when inserting record

Posted: 2011-09-09 19:40
by Fred
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'");
}