2017-08-16 30 views
1

我想通过使用两个函数将datetime变量存储到不同的表中。我在CI中使用constraint,但仍然没有运气。如何将相同的`datetime`变量插入到不同的表中?

这是约束:

$date_now = date("ymdhis"); 
define('TODAY_DATE',$date_now); 

这些功能:

public function save_activity_m(){ 
    foreach($details as $rows){   

     $stock_in   = $rows['product']."_".TODAY_DATE; 
     $data['STOCK_IN'] = ($rows['product'] == "") ? NULL : $stock_in; 

     $this->MProduct->ins_product_m($data); 
    } 
    echo "<script type='text/javascript'>alert('New stock arrived');window.top.location.reload();</script>"; 
} 

public function save_notifikasi(){ 

    $lampiran = $this->input->post('lamp');  

    $data['note_date'] = $lampiran."_".TODAY_DATE;  
    $data['note']  = $this->input->post('isi'); 

    $this->MProduct->ins_notif($data); 
    echo "<script type='text/javascript'>alert('You have a notification');</script>"; 


} 

如何使日期时间为$data['STOCK_IN']$data['note_date']一样吗?

+0

您使用的是什么数据库服务器和数据类型?对于类似于MySQL 5.7或MariaDB的应用程序,您需要使用日期格式为“Y-m-d H:i:s”的DATETIME字段。 – CD001

+0

您可以回显常量变量以检查它是否保持相同的值。 – Sam

+0

我把日期时间和表格中的文字结合起来。所以我使用varchar – ashura91

回答

0

由于Web是无状态的,PHP变量中的数据将不会从一个页面(或加载)保存到另一个页面;基本上你每次都是从零开始启动应用程序。

解决此问题的唯一方法是使用某种类型的半永久性存储,如Cookie或会话变量(或持久性存储,如数据库) - 设置constant(例如, define('TODAY_DATE',$date_now);将只会使该数据不变,以执行脚本的当前

这是使用会话存储($_SESSION)一个基本的例子:

<?php 

// crank up the session 
// you may well have one running already, 
// in which case ignore this 
session_start(); 

// store the execution time for this script 
// as a session variable if it's NOT already set 
// i.e. don't overwrite it 
if(empty($_SESSION['time_now'])) $_SESSION['time_now'] = date("ymdhis"); 


public function save_activity_m() { 
    foreach($details as $rows) {   
     $stock_in = $rows['product'] . "_" . $_SESSION['time_now']; 
     $data['STOCK_IN'] = ($rows['product'] == "") ? NULL : $stock_in; 
     $this->MProduct->ins_product_m($data); 
    } 

    echo "<script type='text/javascript'>alert('New stock arrived');window.top.location.reload();</script>"; 
} 

/** 
* Assuming this is the last thing you want to 
* do with 'time_now' you should unset it here 
*/ 
public function save_notifikasi() { 
    $lampiran = $this->input->post('lamp');  
    $data['note_date'] = $lampiran . "_" . $_SESSION['time_now']; 
    $data['note'] = $this->input->post('isi'); 
    $this->MProduct->ins_notif($data); 

    // since we're done with the 'time_now' session 
    // variable we need to unset it... 
    unset($_SESSION['time_now']); 

    echo "<script type='text/javascript'>alert('You have a notification');</script>"; 
} 

// just to be on the safe side unset the 'time_now' session var 
// if it's older than 1 minute - otherwise future calls to this 
// script, by the same user, during the same session will use 
// the stored value from $_SESSION['time_now'] 
if(isset($_SESSION['time_now'])) { 
    $sessionTime = DateTime::createFromFormat('ymdhis', $_SESSION['time_now']); 
    $oneMinuteAgoTime = new DateTime('-1 minute'); 

    if($sessionTime < $oneMinuteAgoTime) { 
     unset($_SESSION['time_now']); 
    } 
} 

需要说明的是,因为你已经存储在一个会话变量的时候,除非你更新或取消它,它总是会那里(对于当前会话) - 所以如果用户再次运行脚本,它将使用会话中存储的时间。

我已经在几个unset()调用尝试和解决此问题。

0

请参阅PHP: define。这是一个常量,如果两个函数在脚本运行的同一时间执行,它应该具有相同的值。

+0

我已经使用它。并有几秒钟的差异。 – ashura91

+0

也许它是数据库的配置。该行可以具有** DEFAULT CURRENT_TIMESTAMP **或** ON UPDATE CURRENT_TIMESTAMP **。也许? –

+0

datetime的默认格式是:** YYYY-MM-DD HH:MM:SS ** –

相关问题