0
嗨請幫助使用此代碼!該代碼從另一個頁面上的文本框中獲取信息,這些信息都已正確完成,然後通過函數傳遞,並將結果保存爲變量。我試圖將記錄插入到每天缺少週末的數據庫中,對於特定時間段的$ StartDate到$ EndDate的長度。謝謝任何幫助,將不勝感激。使用循環將記錄插入sql數據庫
<?php
session_start();
//Extract user data
$SelectedRoom = $_POST['Room'];
$Period1 = cleanString(trim($_POST['Period1']));
$Period2 = cleanString(trim($_POST['Period2']));
$Morning_Break = cleanString(trim($_POST['Morning_Break']));
$Period3 = cleanString(trim($_POST['Period3']));
$Period4 = cleanString(trim($_POST['Period4']));
$Period5 = cleanString(trim($_POST['Period5']));
$Lunch1 = cleanString(trim($_POST['Lunch1']));
$Lunch2 = cleanString(trim($_POST['Lunch2']));
$Period6 = cleanString(trim($_POST['Period6']));
$Period7 = cleanString(trim($_POST['Period7']));
$Period8 = cleanString(trim($_POST['Period8']));
// create or open database called TimeTable.sqlite
$db = @sqlite_open("TimeTable.sqlite");
//Get Start Date from table
$sql = "SELECT fldStartDate FROM tblTermDate WHERE fldTerm_Name ='Christmas 1st Half'";
$res = sqlite_query($db, $sql);
if (sqlite_num_rows($res) > 0) {
$StartDate = sqlite_fetch_single($res);
}
//Get End Date from Table
$sql = "SELECT fldEndDate FROM tblTermDate WHERE fldTerm_Name ='Christmas 1st Half'";
$res = sqlite_query($db, $sql);
if (sqlite_num_rows($res) > 0) {
$EndDate = sqlite_fetch_single($res);
}
// Finds the differnce between the days
$Days = (strtotime($EndDate) - strtotime($StartDate))/86400;
echo $Days;
if($db) {
for($count = 0; $count = $Days; $count++) {
// insert new booking
for($counter = 1; $counter = 5; $counter++) {
@sqlite_exec($db, "INSERT INTO tblBooking(fldRoom_ID, fldPeriod_ID, fldDate, fldBooking_Name, fldStaff_ID, fldComment) VALUES('" . $SelectedRoom . "',' Period_1 ','" . $StartDate . "','" . $Period1 . "',' '', '')");
@sqlite_exec($db, "INSERT INTO tblBooking(fldRoom_ID, fldPeriod_ID, fldDate, fldBooking_Name, fldStaff_ID, fldComment) VALUES('" . $SelectedRoom . "',' Period_2 ','" . $StartDate . "','" . $Period2 . "',' '', '')");
@sqlite_exec($db, "INSERT INTO tblBooking(fldRoom_ID, fldPeriod_ID, fldDate, fldBooking_Name, fldStaff_ID, fldComment) VALUES('" . $SelectedRoom . "',' Morning_Break ','" . $StartDate . "','" . $Morning_Break . "',' '', '')");
@sqlite_exec($db, "INSERT INTO tblBooking(fldRoom_ID, fldPeriod_ID, fldDate, fldBooking_Name, fldStaff_ID, fldComment) VALUES('" . $SelectedRoom . "',' Period_3 ','" . $StartDate . "','" . $Period3 . "',' '', '')");
@sqlite_exec($db, "INSERT INTO tblBooking(fldRoom_ID, fldPeriod_ID, fldDate, fldBooking_Name, fldStaff_ID, fldComment) VALUES('" . $SelectedRoom . "',' Period_4 ','" . $StartDate . "','" . $Period4 . "',' '', '')");
@sqlite_exec($db, "INSERT INTO tblBooking(fldRoom_ID, fldPeriod_ID, fldDate, fldBooking_Name, fldStaff_ID, fldComment) VALUES('" . $SelectedRoom . "',' Period_5 ','" . $StartDate . "','" . $Period5 . "',' '', '')");
@sqlite_exec($db, "INSERT INTO tblBooking(fldRoom_ID, fldPeriod_ID, fldDate, fldBooking_Name, fldStaff_ID, fldComment) VALUES('" . $SelectedRoom . "',' Lunch_1 ','" . $StartDate . "','" . $Lunch1 . "',' '', '')");
@sqlite_exec($db, "INSERT INTO tblBooking(fldRoom_ID, fldPeriod_ID, fldDate, fldBooking_Name, fldStaff_ID, fldComment) VALUES('" . $SelectedRoom . "',' Lunch_2 ','" . $StartDate . "','" . $Lunch2 . "',' '', '')");
@sqlite_exec($db, "INSERT INTO tblBooking(fldRoom_ID, fldPeriod_ID, fldDate, fldBooking_Name, fldStaff_ID, fldComment) VALUES('" . $SelectedRoom . "',' Period_6 ','" . $StartDate . "','" . $Period6 . "',' '', '')");
@sqlite_exec($db, "INSERT INTO tblBooking(fldRoom_ID, fldPeriod_ID, fldDate, fldBooking_Name, fldStaff_ID, fldComment) VALUES('" . $SelectedRoom . "',' Period_7 ','" . $StartDate . "','" . $Period7 . "',' '', '')");
@sqlite_exec($db, "INSERT INTO tblBooking(fldRoom_ID, fldPeriod_ID, fldDate, fldBooking_Name, fldStaff_ID, fldComment) VALUES('" . $SelectedRoom . "',' Period_8 ','" . $StartDate . "','" . $Period8 . "',' '', '')");
$StartDate = strtotime($StartDate) + 86400;
}
$StartDate = strtotime($StartDate) + 172800;
$count = $count + 2;
}
$_SESSION['WeeklyMessage'] = "Monday TimeTable Changed";
}
else {
$_SESSION['WeeklyMessage'] = "Monday TimeTable Not Changed Error";
}
// close connection
@sqlite_close($db);
//return to previous webpage with message sent as session variable
header("Location: admin.php");
?>
嘗試刪除所有的錯誤抑制。 (The @) – Jessica
只是爲了您的信息,用php,如果您命名輸入字段如'Period [1] Period [2]'等,您將擁有一個易於使用的數組而不是多個不同的項目 –
只是FYI,您應該能夠通過單個SELECT查詢獲取期間開始和期末。你也應該能夠用單個查詢批量插入所有記錄,只需在循環中構建查詢的插入值並執行一次插入即可。 –