使用循环将记录插入sql数据库

时间:2013-12-20 16:19:07

标签: php mysql sql sqlite

您好请帮助这个代码!代码从另一个页面上的文本框中获取信息,这些信息全部正确完成,然后通过函数传递,结果保存为变量。我试图将记录插入到数据库中,每天缺少周末,特定时间段的长度,$ StartDate到$ EndDate。谢谢任何帮助将不胜感激。

<?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");

?>

2 个答案:

答案 0 :(得分:1)

您需要在$counter > 5循环中使用$count > $Daysfor,而不是=

  for($count = 0; $count = $Days; $count++) {
    // insert new booking
    for($counter = 1; $counter = 5; $counter++) {

答案 1 :(得分:1)

调整user876345所说的内容,您需要在for循环中使用$counter <= 5$count <= $Days而不是=

for($count = 0; $count <= $Days; $count++) {
    // insert new booking
    for($counter = 1; $counter <= 5; $counter++) {