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
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();
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.