Jump to content

PHP SQL dual update/insert show date


aeafisme23

Recommended Posts

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

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?

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?

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 
      
} 

$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.

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.