如何在php中通过不同的变量进行foreach循环

时间:2017-07-27 07:38:07

标签: php sql

我有一个动态表单,它有动态输入的文本框和日期,浮点数和时间的变量。我想将这些输入动态插入到我的sql表中。 所以这是我的代码的一部分。我只能循环遍历一个变量,当我想循环时间并浮动时,它面临浮动和时间的转换问题。(如果需要任何图片只是让我知道)

$ct = 0;
if ($_POST['Dates'] && $_POST['slct']) {
    foreach ($_POST['Dates'] as $value) {
        $values = $value;
        $dates = $_POST['Dates']; // for leave date
        $datess = $_POST['datess']; // for created date


        $Area = $_COOKIE['cooAreaCode'];
        $days = $_POST['slct'];

        $time = date("h:i a", strtotime($frtime));
        $times = date("h:i a", strtotime($totime));

        $frtime = $_POST['frtime'];
        $totime = $_POST['totime'];


        $link_mssql = odbc_connect(DB_HSATTEND, DB_USER, DB_PASS);
        $sql = "SELECT MAX(RefNo) as val from tblLeaveHeader";
        $res = odbc_exec($link_mssql, $sql);
        while (odbc_fetch_row($res)) {
            $count = odbc_result($res, "val");
        }
        odbc_free_result($res);

        $temp = sprintf('%08d', $count + 1);

        if (empty($_POST['frtime']) && empty($_POST['totime'])) {
            $sql3 = "INSERT INTO tblLeaveDetails (RefNo, LeaveType, Leavedd, Leaveday, LeaveFrmTime, LeaveToTime)  
               VALUES('$temp','$Leave','$values', '$days[$ct]', NULL, NULL)";
            $res = odbc_exec($link_mssql, $sql3);
            $ct++;
        } else {
            $sql2 = "INSERT INTO tblLeaveDetails (RefNo, LeaveType, Leavedd, Leaveday, LeaveFrmTime, LeaveToTime)  
               VALUES('$temp','$Leave','$values', '$days[$ct]', '$frtime[$ct]', '$totime[$ct]')";
            $res = odbc_exec($link_mssql, $sql2);
            $ct++;
        }
    }
}

sql表中的结果示例

| ID   | Refno | Leavetype     | Leaveday | Leavdd     |frtime|totime
| 1    | 00001 | Annual Leave  | 1        | 2017-07-17 |3.00  | 7.00
| 2    | 00001 | Annual Leave | 1        | 2017-07-18 |4.00  | 6.00

0 个答案:

没有答案