我的代码未按预期运行

时间:2015-07-16 11:24:42

标签: php mysql

我正在尝试从表单到数据库插入一些数据。我知道如何检索值,但事实是我需要将数据插入特定的顺序。 我有一个TIMETABLE用户插入TIME和SUBJECTS,所以我需要插入对应于TIME的SUBJECT值。

    $Times      = $_POST['Time'];
    $Subject    = $_POST['Subject'];
    $Year       = "2013-2017";
    $Dept       = "CSE";
    $Tutor      = "Vishnu";
    $con        = $this->db_con();
    $count      = count($Subject);

    foreach ($Times as $Timings) {
        for ($i=0;$i<count($Subject);$i++) {
            $ins_tmtbl  = $con->prepare("insert into time_table (Department,Year,Time,subject,Tutor_name) values(?,?,?,?,?)");
            $exe        = $ins_tmtbl->execute(array($Dept,$Year,$Timings,$Subject[$i],$Tutor));
        }
    }

这是HTML部分,

$number_of_prds = "10";

    for ($i=0;$i<=$wrkng_dys;$i++) {
        echo "<th>$days[$i]</th>";  
    }

    for ($j=1;$j<=2;$j++) {
        ?>

        <tr><th><div class="col-xs-2">
                    <input class="form-control input-lg" name="Time[]" id="inputlg" type="text" value="" style="width:120px; height:30px;"> 
                    </div></td>
        <?php   
        for ($s=1;$s<=count($days)-6;$s++) {
            ?>
            <td><div class="col-xs-2">
                    <?php  for ($i=0;$i<count($number_of_prds);$i++) { ?>
                    <input class="form-control input-lg" name="Subject[]" id="inputlg" type="text" value="" style="width:120px; height:30px;"> 
                    <?php } ?>
                    </div></td>
            <?php   
        }
        echo "</tr>";


    }

插入数据库后我得到的结果,

10-11 sub1 sub2 sub3 sub4.....
11-12 sub1 sub2 sub3 sub4.....

我期待结果如, 10-11 sub1 sub2到对应于该小时的科目,依此类推 我很坚持,任何帮助都会非常感激

2 个答案:

答案 0 :(得分:0)

问题是您有X Time个变量和X * 6 Subject变量,但无法将两者联系起来。你需要的是Subject上的二维数组:

<input class="form-control input-lg" name="Time[<?php echo $j;?>]" id="inputlg" type="text" value="" style="width:120px; height:30px;">

...

<input class="form-control input-lg" name="Subject[<?php echo $j;?>][]" id="inputlg" type="text" value="" style="width:120px; height:30px;">

然后您可以将时间和主题的关键字关联起来:

foreach ($Times as $key => $Timings) {
        for ($i=0;$i<count($Subject[$key]);$i++) {
            $ins_tmtbl  = $con->prepare("insert into time_table (Department,Year,Time,subject,Tutor_name) values(?,?,?,?,?)");
            $exe        = $ins_tmtbl->execute(array($Dept,$Year,$Timings,$Subject[$key][$i],$Tutor));
        }
}

答案 1 :(得分:0)

最后我得到了输出

    $Times      = $_POST['Time'];
    $Subject    = $_POST['Subject'];
    $Day        = $_POST['Day'];
    $Year       = "2013-2017";
    $Dept       = "CSE";
    $Tutor      = "Vishnu";
    $con        = $this->db_con();
    $count      = count($Subject);
    $number_of_days = "5";

    $limit      = "5";
    for ($k=0;$k<$number_of_days;$k++) {
        for ($m=0;$m<$number_of_days;$m++) {
            if ($k == 0 ) {
                $j  = $m;   
            } 
            if ($k == 1) {
                $j  = $m+5; 
            }
            ${'arr'.$k} = array_slice($Subject,$j,$limit);  
            if ($m == 0) {
                $j = $limit+(5*$k);
            }
            break;
        }
    }

    $time_limit     = 1;
    for ($n = 0;$n<$number_of_days;$n++) {
        ${'Time'.$n}    = array_slice($Times,$n,$time_limit);   
    }

    for ($i=0;$i<5;$i++) {
        for ($j=0;$j<=5;$j++) {
            $ins_tmtbl  = $con->prepare("insert into time_table (Department,Year,Time,subject,Tutor_name,Day) values(?,?,?,?,?,?)");
            $exe        = $ins_tmtbl->execute(array($Dept,$Year,${'Time' . $i}[0],${'arr' . $i}[$j],$Tutor,$Day[$j]));  
        }
    }

我不知道这样编码是否是一个好习惯,但这段代码给了我想要的输出