Jump to content

using $_SESSION for select query


gammaman

Recommended Posts

I know this was posted before but I still need help because it is not working.  When I choose an existing record from the previoius page and send the information over to this page to be checked by the query, even  if the record already exists I still get 0 every time I echo the count from the number of matched records.

 

<?php
$conn=mysql_connect("localhost","fierm","13183");

  if(!$conn){
    echo "failed";
}else{ 
   mysql_select_db(fierm);
     session_start();
  $_SESSION['student']['user'];


  
  

  $_SESSION['student']['pass'];

   
     $CourseID=$_POST['ci'];
     $CourseName=$_POST['cn'];

         



    $course=mysql_query("select CourseID,CourseName,StudentID,Password FROM Rcourse
             WHERE CourseID ='$CourseID' AND CourseName='$CourseName' AND StudentID = '{$_SESSION['student']['user']}' 
   AND Password ='{$_SESSION['student']['pass']}'");

     
     $count=mysql_num_rows($course);
     echo "$count";     //this always shows zero even if a matching record is in Rcourse

     if ($count > 0 ){
        echo "Already took or currently taking this course";
     }

     else{
        mysql_query("Insert into Rcourse (CourseID,CourseName,StudentID,Password) 
                 Values ($CourseID,$CourseName,{$_SESSION['student']['user']},{$_SESSION['student']['pass']})");

        echo "You are now registerd";    
     
    }
}
?> 

 

Link to comment
https://forums.phpfreaks.com/topic/102113-using-_session-for-select-query/
Share on other sites

Add this line right above the mysql_num_rows line for me and tell me what it says:

if (is_resource($course)) {

  echo '$Course set properly.';

}

else {

  echo "Query is messed up.";

}

 

Use single quotes on the first if statement so it displays $Course and doesn't parse it. =P  I just want to make sure the query is working right.  Then it's gotta be another problem.

It works now, I think it was my fault because on the pervious page I was sending the course id and the course name through a link and then on this page, I tried to use them with $_POST which you cannot do, right? I changed it to $_GET and now I get 1 on my count.

Now I have a problem with the Insert Query, because if the count from the selct is 0 is should go to the insert but nothing is inserted.

 

<?php
$conn=mysql_connect("localhost","fierm","13183");

  if(!$conn){
    echo "failed";
}else{ 
   mysql_select_db(fierm);
     session_start();
  $_SESSION['student']['user'];


  
  

  $_SESSION['student']['pass'];

   
     $CourseID=$_GET['ci'];
     echo "$CourseID";
     $CourseName=$_GET['cn'];
     echo "$CourseName";

         



    $course=mysql_query("select CourseID,CourseName,StudentID,Password FROM Rcourse
             WHERE CourseID ='$CourseID' AND CourseName='$CourseName' AND StudentID = '{$_SESSION['student']['user']}' 
   AND Password ='{$_SESSION['student']['pass']}'");

    

     if (is_resource($course)) {
  echo '$Course set properly.';
}
else {
  echo "Query is messed up.";
}

     $count=mysql_num_rows($course);
     echo $count;
     

     if ($count > 0 ){
        echo "Already took or currently taking this course";
     }

     else{
        mysql_query("Insert into Rcourse (CourseID,CourseName,StudentID,Password) 
                 Values ($CourseID,$CourseName,{$_SESSION['student']['user']},{$_SESSION['student']['pass']})");

        echo "You are now registerd";    
     
    }
}
?>   

Yes that works, and I moved that code block of yours to the insert query and set a variable to it and now I run and get output like this

 

ENG130Literature$Course set properly.0Query is messed up.You are now registerd

 

Notice the zero which means it did not go into the select and when it got to the insert, it determined the query was bad.

<?php
$conn=mysql_connect("localhost","fierm","13183");

  if(!$conn){
    echo "failed";
}else{ 
   mysql_select_db(fierm);
     session_start();
  $_SESSION['student']['user'];


  
  

  $_SESSION['student']['pass'];

   
     $CourseID=$_GET['ci'];
     echo "$CourseID";
     $CourseName=$_GET['cn'];
     echo "$CourseName";

         



    $course=mysql_query("select CourseID,CourseName,StudentID,Password FROM Rcourse
             WHERE CourseID ='$CourseID' AND CourseName='$CourseName' AND StudentID = '{$_SESSION['student']['user']}' 
   AND Password ='{$_SESSION['student']['pass']}'");

    

   

     $count=mysql_num_rows($course);
     echo $count;
     

     if ($count > 0 ){
        echo "Already took or currently taking this course";
     }

     else{

      $ins=mysql_query("Insert into Rcourse (CourseID,CourseName,StudentID,Password) 
                 Values ($CourseID,$CourseName,'{$_SESSION['student']['user']}','{$_SESSION['student']['pass']}')");
       if (is_resource($ins)) {
  echo '$ins set properly.';
}
else {
  echo "Query is messed up.";
}

        echo "You are now registerd";    
     
    }
}
?>   

The way I personally do queries is:

<?php
$query = "Query here";
$ins = mysql_query($query);
?>
Let me try it with yours and you can see if it works.
[code]
     $query = "insert into Rcourse (CourseID, CourseName, StudentID, Password) VALUES ($CourseID, $CourseName, {$_SESSION['student']['user']}, '{$_SESSION['student']['pass']}')";
     $ins = mysql_query($query);

 

Try it.  And I put $_SESSION['student']['user'] as a numeric value instead of a literal.  Might have fixed it.  Try that code.[/code]

  • 1 year later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.