Jump to content

problem with data on page reload


gammaman

Recommended Posts

This is hard to explain but I will try my best.

 

I have the following page.

 

<body>
<b>Student Login</b>	


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

if(!$Conn){
   echo "failed";
  }
  else{
  
mysql_select_db("fierm");

  $User=$_POST['user'];   

  session_start();
  
$_SESSION['student']=$_POST;

$_SESSION['student']['user'];

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

  

  

  $result=mysql_query("select studentID, password FROM Student WHERE studentID='{$_SESSION['student']['user']}' 
                 and password='{$_SESSION['student']['pass']}'");
  $cou=mysql_num_rows($result);
  
  if($cou>0) 
  {
      echo "<table border=\"1\">";
      echo "<b>Registered Courses</b>";
      echo "<tr><th>CourseID</th><th>CourseName</th><th>Grade<th>Drop Course</th></tr>";
    
      $result=mysql_query("select CourseID,CourseName,StudentID,Grade FROM Rcourse WHERE StudentID='$User'");
      $cou=mysql_num_rows($result);
    
      while($row=mysql_fetch_array($result))
      {
       $var = "dropCourse.php?ci=$row[0]";
       $strUrl = "<a href=$var>Drop Course</a>";
      echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[3]</td><td>$strUrl</td></tr>\n";

    }
     
     echo "</table>";

     
    

     
  }
  
else{
    
      echo "Invalid Password";
      echo "<a href=\"main.php\">Login Again</a>";
     }    

echo "<b>Register For Courses:</b>";
echo "<br />";
echo "<a href=\"regCourse.php\">Register Courses</a>";

     
    
   
}
?>

 

Now lets say they click on one of the links and go to this page.

<?php

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

if (!$conn){
    echo "failed";
}else{

mysql_select_db("fierm");
  $CourseID=$_GET['ci'];
  
session_start();
   $_SESSION['student']['user'];
   $_SESSION['student']['pass'];
  
  
   

   $result=mysql_query("select CourseID,StudentID, Grade FROM Rcourse
             WHERE CourseID ='$CourseID'  AND StudentID = '{$_SESSION['student']['user']}'
          AND Grade IS NULL");
   $count=mysql_num_rows($result);
   if ($count > 0){
        echo "Grade already assigned in $CourseID, course cannot be dropped";
         echo "<a href = \"student.php\">Return to Student Page</a>";
   }
   
       
     



}


?>

Now on that page if they click the link "student.php" it takes them back to the first page I posted,

but it automatically fails out and goes to

 

    echo "Invalid Password";
      echo "<a href=\"main.php\">Login Again</a>";

 

I used $_SESSIONS but when they click the link back to the first page I posted, it does not "remember what the id and pass is so it fails out and says invalid, yet when I come in the first time it obviously knows.  Do I have to somehow send the $_SESSIONS from the second page through the link, because that seems redudant.

 

 

 

Link to comment
Share on other sites

This line is ruining you:

 

$_SESSION['student']=$_POST;

 

It basically sets $_SESSION['student'] to array(), because $_POST is blank.  Here's what you do. 

if (!empty(array_diff($_SESSION['student'], $_POST))) {

 

$_SESSION['student']=$_POST;

 

$_SESSION['student']['user'];

 

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

 

}

 

That way it only sets it if there's NEW post data.

Link to comment
Share on other sites

Thanks I need to try that, I let you know how I make out tommorow, I have been at this project since

8:00AM this morning, this course is supposed to be an introduction to PHP programming and our prof thinks we are super gods. :D

 

What you're coding doesn't seem too hard. No offense. >_>  If you want serious help, contact me with PMs and I can get you on the right track.

Link to comment
Share on other sites

still here , the page displays nothing now.  One question, what happens on the first pass through

beause I do not post the sessions at any point prior to this so won't it skip the entire posting the first time through and fail the rest of my code.

Link to comment
Share on other sites

still here , the page displays nothing now.  One question, what happens on the first pass through

beause I do not post the sessions at any point prior to this so won't it skip the entire posting the first time through and fail the rest of my code.

 

Set the post variables and stuff for the sessions, but if the sessions already exist, don't delete them.  That's basically your issue.

Link to comment
Share on other sites

Is that what your code bit fixes, if not how do I fix this.

 

Yeah, that's what my code bit fixes, but I might have overdone it.  Try just this:

 

if (!isset($_SESSION['user'])) {

$_SESSION['student']=$_POST;

 

$_SESSION['student']['user'];

 

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

 

}

Link to comment
Share on other sites

well the first page now came up, don't know about the rest yet, mabe when  I come back from another page is where I may be able to use that first code bit, Ok no more testing for now, I will be working on it from 8am -2pm tommorow, if you are around do me a favor and check for more posts from me, Part of the problem is that I never took an html course so I am trying to learn a combination of both languages at the same time.  Thanks for all of your help ;D

Link to comment
Share on other sites

well the first page now came up, don't know about the rest yet, mabe when  I come back from another page is where I may be able to use that first code bit, Ok no more testing for now, I will be working on it from 8am -2pm tommorow, if you are around do me a favor and check for more posts from me, Part of the problem is that I never took an html course so I am trying to learn a combination of both languages at the same time.  Thanks for all of your help ;D

 

No problem.  PM me if you ever need help.  =D  I'll check for some of your posts tomorrow though.

Link to comment
Share on other sites

This still does not work. Here are the pages again to remind you what I am doing.

 

<body>
<b>Student Login</b>	


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

if(!$Conn){
   echo "failed";
  }
  else{
  
mysql_select_db("fierm");

  $User=$_POST['user'];   

  session_start();
if (!isset($_SESSION['user']))  {


$_SESSION['student']=$_POST;

$_SESSION['student']['user'];

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

} 

  
$result=mysql_query("select studentID, password FROM Student WHERE studentID='{$_SESSION['student']['user']}' 
                 and password='{$_SESSION['student']['pass']}'");
  $cou=mysql_num_rows($result);
  
  if($cou>0) 
  {
      echo "<table border=\"1\">";
      echo "<b>Registered Courses</b>";
      echo "<tr><th>CourseID</th><th>CourseName</th><th>Grade</th></tr>";
    
      $result=mysql_query("select CourseID,CourseName,StudentID,Grade FROM Rcourse WHERE StudentID='$User'");
      $cou=mysql_num_rows($result);
    
      while($row=mysql_fetch_array($result))
      {
      
       echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[3]</td></tr>\n";

    }
     
     echo "</table>";

     
     echo "<b>Register For Courses:</b>";
     echo "<br />";
     echo "<a href=\"regCourse.php\">Register Courses</a>";
     echo "<b>Drop a Course:</b>";
     echo "<br/>";
     echo "<a href=\"dropCourse.php\">Drop a Course</a>";


     
  }
  
else{
    
      echo "Invalid Password";
      echo "<a href=\"main.php\">Login Again</a>";
     }    

 

<?php
   $conn=mysql_connect("localhost","fierm","13183");
    
   if(!$conn){
     echo "failed";
}else{
   

   mysql_select_db("fierm");

     $CourseID = $_GET['ci'];

     $CourseName = $_GET['cn'];  
     

     session_start();
     
    
    $_SESSION['student']['user'];

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

         


     
    $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";
        echo "<a href = \"student.php?\">Return to Student Page</a>";
     }else{
     
     
      $d = $_SESSION['student']['user'];
      echo "d = $d";
      echo "<br />";
      $e = $_SESSION['student']['pass'];
      echo "e = $e";
      echo "<br />";

      $conn=mysql_connect("localhost","fierm","13183");
       mysql_select_db(fierm);
      if (!$conn){
        echo "not conn";
      }
      else{
      echo "here";
      }
      $result=mysql_query("INSERT INTO Rcourse (CourseID,CourseName,StudentID,Password) 
                 VALUES ('$CourseID','$CourseName','$d','$e')");
       if ($result) {
		echo '$result set properly.';
} #close the if
else {
	echo "Query is messed up.";
} #close the else

        echo " abc";    
     
   } #close the if $count
}#closes the !conn
?>   

 

When I click the student link which returns you to the first page I posted, it still fails and says invalid user.  The session values never change but do they need to get passed into student? Or should it still know what the original values were as if I entered the student page for the first time. 

 

   

Link to comment
Share on other sites

Actually there is still a problem because when I come back to the student page I loose the rows from the table

 

<body>
<b>Student Login</b>	


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

if(!$Conn){
   echo "failed";
  }
  else{
  
mysql_select_db("fierm");

  

  session_start();
if (!isset($_SESSION['student']['user']))  {


$_SESSION['student']=$_POST;

$_SESSION['student']['user'];

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

} 

  
$result=mysql_query("select studentID, password FROM Student WHERE studentID='{$_SESSION['student']['user']}' 
                 and password='{$_SESSION['student']['pass']}'");
  $cou=mysql_num_rows($result);
  
  if($cou>0) 
  {
      echo "<table border=\"1\">";
      echo "<b>Registered Courses</b>";
      echo "<tr><th>CourseID</th><th>CourseName</th><th>Grade</th></tr>";
    
      $result=mysql_query("select CourseID,CourseName,StudentID,Grade FROM Rcourse WHERE StudentID='$User'");
      $cou=mysql_num_rows($result);
    
      while($row=mysql_fetch_array($result))
      {
      
       echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[3]</td></tr>\n";

    }
     
     echo "</table>";

     
     echo "<b>Register For Courses:</b>";
     echo "<br />";
     echo "<a href=\"regCourse.php\">Register Courses</a>";
     echo "<b>Drop a Course:</b>";
     echo "<br/>";
     echo "<a href=\"dropCourse.php\">Drop a Course</a>";


     
  }
  
else{
    
      echo "Invalid Password";
      echo "<a href=\"main.php\">Login Again</a>";
     }    


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

   

 

This code here:  $result=mysql_query("select CourseID,CourseName,StudentID,Grade FROM Rcourse WHERE StudentID='$User'");

is messing me up because StudentID should be set to a session but when I tried that I got a blank page.

 

Link to comment
Share on other sites

Here is the latest version of the page

<body>
<b>Student Login</b>	


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

if(!$Conn){
   echo "failed";
  }
  else{
  
mysql_select_db("fierm");

   

  session_start();
if (!isset($_SESSION['student']['user']))  {


$_SESSION['student']=$_POST;

$_SESSION['student']['user'];

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

}


  
$result=mysql_query("select studentID, password FROM Student WHERE studentID='{$_SESSION['student']['user']}' 
                 and password='{$_SESSION['student']['pass']}'");
  $cou=mysql_num_rows($result);
  
  if($cou>0) 
  {
      echo "<table border=\"1\">";
      echo "<b>Registered Courses</b>";
      echo "<tr><th>CourseID</th><th>CourseName</th><th>Grade</th></tr>";
    
      $result=mysql_query("select CourseID,CourseName,StudentID,Grade FROM Rcourse WHERE StudentID='{$_SESSION['student']['user']}'");
      $cou=mysql_num_rows($result);
    
      while($row=mysql_fetch_array($result))
      {
      
       echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[3]</td></tr>\n";

    }
     
     echo "</table>";

     
     echo "<b>Register For Courses:</b>";
     echo "<br />";
     echo "<a href=\"regCourse.php\">Register Courses</a>";
     echo "<b>Drop a Course:</b>";
     echo "<br/>";
     echo "<a href=\"dropCourse.php\">Drop a Course</a>";


     
  }
  
else{
    
      echo "Invalid Password";
      echo "<a href=\"main.php\">Login Again</a>";
     }    


     
    
  
}
?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.