aeafisme23 Posted August 15, 2008 Share Posted August 15, 2008 Basically I have a form that updates and inserts new records that works perfectly fine but I need to show a updated timestamp/date of when the last update or insert was done. (Code below shows insert and update). Below my code i will show you what i thought was a correct way to go but need insight to find out how to do it. Original snippet of code of insert/update: if ($action == "add") { $sql = "insert into event_master (station_id, eventdate, eventname, eventcity, participated, notes) values('{$station_id}','{$eventdate}','{$eventname}','{$eventcity}','{$participated}','{$notes}');"; $msg = "Record successfully added"; }elseif($action=="edit"){ $sql = "update event_master set station_id = '{$station_id}', eventdate = '{$eventdate}', eventname = '{$eventname}', eventcity = '{$eventcity}', participated = '{$participated}', notes = '{$notes}' where id = {$id};"; $msg = "Record successfully updated"; } How i thought it would be done: basically just tryign to do a dual sql insert to show date/timestamp of update. form would have a hiddent date field that would allow it to do this. if ($action == "add") { $sql = "insert into event_master (station_id, eventdate, eventname, eventcity, participated, notes) values('{$station_id}','{$eventdate}','{$eventname}','{$eventcity}','{$participated}','{$notes}');" "insert into dateonupdate (dayonupdated) values ('{$dayonupdate}');" ; $msg = "Record successfully added"; }elseif($action=="edit"){ $sql = "update event_master set station_id = '{$station_id}', eventdate = '{$eventdate}', eventname = '{$eventname}', eventcity = '{$eventcity}', participated = '{$participated}', notes = '{$notes}' where id = {$id};" insert into dateonupdate (dayonupdated) values ('{$dayonupdate}');" ; $msg = "Record successfully updated"; } Any help would be great. Just a dual sql insert to two different tables in the database but unsure how to write the sql or how to structure it! Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/119883-php-sql-dual-updateinsert-show-date/ Share on other sites More sharing options...
lemmin Posted August 15, 2008 Share Posted August 15, 2008 You need to make a separate query for each table. Link to comment https://forums.phpfreaks.com/topic/119883-php-sql-dual-updateinsert-show-date/#findComment-617573 Share on other sites More sharing options...
aeafisme23 Posted August 15, 2008 Author Share Posted August 15, 2008 would it be like this? if ($action == "add") { $sql = "insert into event_master (station_id, eventdate, eventname, eventcity, participated, notes) values('{$station_id}','{$eventdate}','{$eventname}','{$eventcity}','{$participated}','{$notes}');"; $sql = "insert into dateonupdate (dayonupdated) values('{$dayonupdated}');"; $msg = "Record successfully added"; }elseif($action=="edit"){ $sql = "update event_master set station_id = '{$station_id}', eventdate = '{$eventdate}', eventname = '{$eventname}', eventcity = '{$eventcity}', participated = '{$participated}', notes = '{$notes}' where id = {$id};"; $sql = "insert into dateonupdate (dayonupdated) values('{$dayonupdated}');"; $msg = "Record successfully updated"; } or will 2 $sql back to back screw it up? Link to comment https://forums.phpfreaks.com/topic/119883-php-sql-dual-updateinsert-show-date/#findComment-617580 Share on other sites More sharing options...
Jabop Posted August 15, 2008 Share Posted August 15, 2008 Well your $sql variable is just a variable getting assigned, there is no mysql_query() related to it. So if you run that $sql, the second set of the variable will be the query to be ran. Also, you should append or die(mysql_error()); to the end of your queries, otherwise would you know if the record was successfully updated? Link to comment https://forums.phpfreaks.com/topic/119883-php-sql-dual-updateinsert-show-date/#findComment-617593 Share on other sites More sharing options...
aeafisme23 Posted August 15, 2008 Author Share Posted August 15, 2008 You were exactly right Jabop on that the second variable $sql ran that sql statement only and not both of them conjuctively... How do i make them two seperate inserts upon submit. Here's a little more of the code shwonig you i do have error catching just not sure how to make 2 sql statements run at same time since they are both $sql if ($action == "add") { $sql = "insert into event_master (station_id, eventdate, eventname, eventcity, participated, notes) values('{$station_id}','{$eventdate}','{$eventname}','{$eventcity}','{$participated}','{$notes}');"; $sql = "insert into dateonupdate (dayonupdate) values('{$dayonupdate}');"; $msg = "Record successfully added"; }elseif($action=="edit"){ $sql = "update event_master set station_id = '{$station_id}', eventdate = '{$eventdate}', eventname = '{$eventname}', eventcity = '{$eventcity}', participated = '{$participated}', notes = '{$notes}' where id = {$id};"; $sql = "insert into dateonupdate (dayonupdate) values('{$dayonupdate}');"; $msg = "Record successfully updated"; } $result = conn($sql); if (mysql_errno()==0) { confirm($msg); list_users(); }else{ $msg = "There was a problem adding the user to the database. Error is:".mysql_error(); confirm($mag); }//end if } Link to comment https://forums.phpfreaks.com/topic/119883-php-sql-dual-updateinsert-show-date/#findComment-617602 Share on other sites More sharing options...
Jabop Posted August 15, 2008 Share Posted August 15, 2008 $query1='your query'; $query2='your query'; if ($query1) { /* perform it */ or die(mysql_error()); $msg='success'; } if ($query2) { /* perform it */ or die(mysql_error()); $msg='success'; } There are many ways you can do this. I just listed one. Link to comment https://forums.phpfreaks.com/topic/119883-php-sql-dual-updateinsert-show-date/#findComment-617612 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.