データベースに加えられた変更を記録する .txt ログ ファイルを作成したいと考えています。基本的に、誰かがレコードを追加するたびに、ログに「2013-10-03 12:21:43 スミス、ジョーが次の予定を追加しました: ...いくつかの予定の詳細...」のようなものを記録したいと考えています。
私は次の関数を持つ functions.php ファイルを持っています (ああ、mysql と 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 の最終的な内容は、"On 2013-10-03 12:21:43 Smith, Joe added the following attachment:" のようなものですgetCalendarChange()
。
getCalendarChange を削除しようとしまし$chngid
たが、期待どおりに id# を生成する echo だけです。
getCalendarChange(100)
functions.php ファイル内から静的な番号を指定して getCalendarChange 関数を呼び出しました。これにより、その特定のイベントの正しい情報が表示されます。
に置き換え$chngid
ましたgetCalendarChange($chngid)
がgetCalendarChange(100)
、まだ機能しません。(はい、ID 100 のレコードがあります)。
getCalendarChange($chngid)
functions.php ファイルの外で動作しないのはなぜですか?