php while while循环里面的每一个

时间:2015-01-23 06:48:08

标签: php loops

我有这个代码。我不知道为什么while循环不起作用。我无法从课程数据库中检索数据。请帮忙

我尝试了一切,但我无法弄清楚如何。

if(isset($_POST['enroll'])){

$n = 0; 
foreach($_POST['trainings'] as $textbox){


    $strpicture = $_POST['strpicture'][$n];
    $stridnumber = $_POST['stridnumber'][$n];
    $strfullname = $_POST['strfullname'][$n];
    $strtraining =$textbox;



    //Add Query to Training Masterlist
    $AddQuery ="INSERT INTO  tms_ml 
    (strpicture,stridnumber,strfullname,strtraining)
   VALUES
   ('$strpicture','$stridnumber','$strfullname','$strtraining')";

    mysql_query($AddQuery, $con);



   $strcoursestat="OPEN";
   //query for training and status training


$SearchCourseQuery = 
        "SELECT strtraining 
        FROM course 
        WHERE strtraining like '%".$strtraining."%' AND strcoursestat like '%".$strcoursestat."%'";

    $resultcoursequery =mysql_query($SearchCourseQuery); 

   require("dbc.php");
   //retrieve data from Course DB
   while($record = mysql_fetch_array($resultcoursequery)){ //this is
    echo $sessionnum= $record['strsessnum'];               // the part
                                                            //not working
        };


    $countcourse=mysql_num_rows($resultcoursequery);

    if($countcourse==0){

        echo '</br>' . $strtraining . ' ' . '<b>No Available Schedule for this Training</b>' .'</br>';
    } else{
           echo '</br>' . $strtraining . ' ' . '<b>Enrolled</b>' .'</br>';
            //Add to Training Session

            $test="ab";
            $AddtoSession ="INSERT INTO trn_session(strsessnum)VALUES('$sessionnum')";
            mysql_query($AddtoSession, $con);

    }//else if count       

    $n++;


} // for each

编辑:嗨大家好重新创建我的SEARCH QUERY($ SearchCourseQuery),现在它工作正常。

"SELECT * FROM course where strtraining = '$strtraining' AND strcoursestat = '$strcoursestat'";

谢谢大家的帮助!

3 个答案:

答案 0 :(得分:2)

您的Insert语句中有拼写错误:

INSERT INTO  tms_ml 
(strpicture,stridnumber,strfullname,strtraining)
VALUES
('$strpicture','$stridnumber',$strfullname','$strtraining')

$strfullname错过报价。

INSERT INTO  tms_ml 
(strpicture,stridnumber,strfullname,strtraining)
VALUES
('$strpicture','$stridnumber','$strfullname','$strtraining')

除此之外,不推荐使用mysql *扩展,并且您很容易受到SQL注入攻击。请考虑使用PDOmysqli*扩展名和准备好的语句。

答案 1 :(得分:0)

首先关闭;

php中的foreach有一个&#39;索引&#39 ;;可以这样确定:

   foreach ($items as $key => $item)
   {

   }

关键是你的索引(可以任意你喜欢的名字)认为这对你来说是一个值得了解的技巧......即你不需要使用$ n变量并递增它。

其次:

什么是mysql_fetch_array($resultcoursequery)返回?

答案 2 :(得分:0)

根据这里的文件。

http://php.net/manual/en/function.mysql-fetch-array.php

您需要添加result_type:

while($record = mysql_fetch_array($resultcoursequery, MYSQL_BOTH)){
    echo $sessionnum= $record['strsessnum'];               

};