我想创建一个记录对数据库所做更改的.txt日志文件。基本上每当有人添加记录时,我希望日志能够记录类似“在2013-10-03 12:21:43史密斯,乔添加了以下约会:...一些约会细节......”
我有一个具有以下功能的functions.php文件(哦,暂时跟我一起讨论mysql vs mysqli - 这个更新正在进行中,但还没有发生):
function getCalendarChange($id) {
require("db.php"); //database connection
$sql = "SELECT calendar_event.consultant_id, calendar_event.client_id, calendar_event.event_id, calendar_event.billing_id, calendar_event.dates_id, consultant.f_name, consultant.l_name, client.client_name, event_type.event_type, billing_status.billing_type, dates.date FROM calendar_event
INNER JOIN consultant ON calendar_event.consultant_id = consultant.consultant_id
INNER JOIN client ON calendar_event.client_id = client.client_id
INNER JOIN event_type ON calendar_event.event_id = event_type.event_id
INNER JOIN billing_status ON calendar_event.billing_id = billing_status.billing_id
INNER JOIN dates ON calendar_event.dates_id = dates.date_id
WHERE calendar_event_id = '$id'";
$result = mysql_query($sql,$connection) or die (mysql_error());
$results = mysql_fetch_array($result);
$consultant = $results['l_name'] . ", " . $results['f_name'];
$client = $results['client_name'];
$event = $results['event_type'];
$billing = $results['billing_type'];
$eventdate = $results['date'];
echo $consultant . " " . $client . " " . $event . " " . $billing . " " . $eventdate;
}
在处理表单以插入新记录的页面中,我有以下内容:
include("functions.php");
$add_appt_query = "INSERT INTO calendar_event (calendar_event_id, consultant_id, client_id, event_id, billing_id, dates_id) VALUES (NULL, '$addee', '$addclient', '$addevent', '$addbilling', '$dt')";
$add_appt_result = mysql_query($add_appt_query,$connection) or die(mysql_error());
$chngid = mysql_insert_id(); //gets the last auto increment id - yes this is set to be an auto increment column
$new_appointment = "<p>Appointments added successfully.</p>"; //everything works up to here - the appointment is added to the db as expected
// start log new appointment
$log_data = "On " . $log_datetime . " " . $ee_log_name . " added the following appointment: "; //$log_datetime and $ee_log_name are defined elsewhere, but work correctly
$log_data .= getCalendarChange($chngid) . "\n";
$log_data .= file_get_contents('mod.txt');
file_put_contents('mod.txt', $log_data); //done this way so the newest change is at the beginning of the file not the end
// end log new appointment
最终在mod.txt中的内容是“在2013-10-03 12:21:43史密斯,乔添加了以下约会:”除了来自getCalendarChange()
的数据外,它还添加了我想要的一切。
我试图删除getCalendarChange,只是回显$chngid
,它会产生你想象中的id#。
我在functions.php文件中调用了带有静态编号getCalendarChange(100)
的getCalendarChange函数,该文件显示了该特定事件的正确信息。
我已将$chngid
中的getCalendarChange($chngid)
替换为getCalendarChange(100)
,但仍然无效。 (是的,有一个id为100的记录)。
为什么getCalendarChange($chngid)
不在functions.php文件之外工作?