Jump to content

auto update another table field on add


sunpat

Recommended Posts

Hello all,

 

I have a database setup as following:

 

Table1:

 

ID

Student

phone

last_class_date

remarks

 

Table2:

 

ID

class_Date

Student_ID

Time

billable_amount

remarks

 

So this is what i am trying to do from couple days now. When i add a record into Table 2, I want to automatically update Table1:Last_class_date, so that when i view tabe1, i can sort out student who have not attented class for few weeks or etc.

 

Any help is much appreciated.

 

Thanks,

Link to comment
https://forums.phpfreaks.com/topic/274078-auto-update-another-table-field-on-add/
Share on other sites

Seems pretty simple to me.

 

First you could always use a trigger. You didn't state what database you were using but I don't know of any that wouldn't give you a simple way of updating the last_class_date for the appropriate ID in table1 using an "insert or after insert" trigger.

 

You could also pretty easily do this in your php code with a 2nd query after the INSERT query.

 

$sth->prepare('INSERT INTO Table2 (Student_ID, ....) VALUES (?, ....)');
$sth->bindParam(1, $student_id, PDO::PARAM_INT);
if ($sth->execute()) {
   $sth->prepare('UPDATE Table1 SET last_class_date = CURDATE() WHERE ID = ?');
   $sth->bindParam(1, $student_id, PDO::PARAM_INT);
   $sth->execute();
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.