gammaman Posted April 22, 2008 Share Posted April 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted April 22, 2008 Share Posted April 22, 2008 you dont need to have these two lines in both scripts: $_SESSION['student']['user']; $_SESSION['student']['pass']; if you're not doing anything with the variables, theres not reason to have them, and im not too sure, but having that might be removing their values. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Share Posted April 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
gammaman Posted April 22, 2008 Author Share Posted April 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Share Posted April 22, 2008 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. 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. Quote Link to comment Share on other sites More sharing options...
gammaman Posted April 22, 2008 Author Share Posted April 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Share Posted April 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
gammaman Posted April 22, 2008 Author Share Posted April 22, 2008 Is that what your code bit fixes, if not how do I fix this. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Share Posted April 22, 2008 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']; } Quote Link to comment Share on other sites More sharing options...
gammaman Posted April 22, 2008 Author Share Posted April 22, 2008 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 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Share Posted April 22, 2008 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 No problem. PM me if you ever need help. =D I'll check for some of your posts tomorrow though. Quote Link to comment Share on other sites More sharing options...
gammaman Posted April 22, 2008 Author Share Posted April 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
gammaman Posted April 22, 2008 Author Share Posted April 22, 2008 Got this working now, want to give a special thanks to Darkwater. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Share Posted April 22, 2008 No problem. =) Quote Link to comment Share on other sites More sharing options...
gammaman Posted April 22, 2008 Author Share Posted April 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Share Posted April 22, 2008 Wait, what? You lose rows from the HTML table or the database table? Quote Link to comment Share on other sites More sharing options...
gammaman Posted April 22, 2008 Author Share Posted April 22, 2008 I loose the rows from the html table Quote Link to comment Share on other sites More sharing options...
gammaman Posted April 22, 2008 Author Share Posted April 22, 2008 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>"; } } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.