2

i have this code. and i have no idea why the while loop is not working. i can't retrieve the data from course db. please help

i tried everything but i cant figure out how.

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

EDITED: hi guys re create my SEARCH QUERY ($SearchCourseQuery) and it Works Fine now.

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

thank you all for the help!

4
  • check if mysql_fetch_array($resultcoursequery) actually returns anything..maybe it returns an empty array? Commented Jan 23, 2015 at 7:04
  • i tried to echo and nothing happens Commented Jan 23, 2015 at 7:16
  • Can you make sure that the connections of your mysql are right? And you can insert the first query ($AddQuery) Commented Jan 23, 2015 at 7:18
  • yap the first $AddQuery and $AddtoSession both executed well and added the data to DB. its just the while loop that is not working. Commented Jan 23, 2015 at 7:19

3 Answers 3

2

You have a typo in your Insert statement:

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

Missing quote at $strfullname.

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

Besides that, the mysql* extensions are deprecated and you are vulnerable to SQL injections. Consider to use PDO or the mysqli* extensions and prepared statements.

Sign up to request clarification or add additional context in comments.

2 Comments

yeap sorry but i just edited the original to post here in stackoverflow. my original code has right quote. yeap i know about mysqli. i just need to finish this project before i move on.
Then try to print your $searchCourceQuery and check if you get a valid query back and use mysql_error to check for any mysql errors.
0

First off;

a foreach in php has an 'index'; which could be determined like so:

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

   }

The key is your index (can be named whatever you like) thought that would be a trick worth knowing for you... i.e you don't need to use the $n variable and increment it.

Secondly:

What is mysql_fetch_array($resultcoursequery) returning?

3 Comments

mysql_fetch_array($resultcoursequery) will return the value from Course database. which will i pass to add query into trn_session.
yes of course; but are you getting any results? did you dd your results?
no and thats my problem. i tried to echo the echo $sessionnum= $record['strsessnum']; but it return to no value. so i believe the while is not working.
0

According to the documentation here.

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

You need to add the result_type:

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

};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.