gammaman Posted April 21, 2008 Share Posted April 21, 2008 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 More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 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. Link to comment https://forums.phpfreaks.com/topic/102113-using-_session-for-select-query/#findComment-522705 Share on other sites More sharing options...
gammaman Posted April 21, 2008 Author Share Posted April 21, 2008 It says Course set properly Link to comment https://forums.phpfreaks.com/topic/102113-using-_session-for-select-query/#findComment-522715 Share on other sites More sharing options...
gammaman Posted April 21, 2008 Author Share Posted April 21, 2008 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. Link to comment https://forums.phpfreaks.com/topic/102113-using-_session-for-select-query/#findComment-522722 Share on other sites More sharing options...
gammaman Posted April 21, 2008 Author Share Posted April 21, 2008 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"; } } ?> Link to comment https://forums.phpfreaks.com/topic/102113-using-_session-for-select-query/#findComment-522727 Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 You can remove the is_resource() block now. And umm, try running the query in the MySQL console with some values that could be in the script, and make sure it works. Link to comment https://forums.phpfreaks.com/topic/102113-using-_session-for-select-query/#findComment-522730 Share on other sites More sharing options...
gammaman Posted April 21, 2008 Author Share Posted April 21, 2008 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. Link to comment https://forums.phpfreaks.com/topic/102113-using-_session-for-select-query/#findComment-522734 Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 Show me the new code. Link to comment https://forums.phpfreaks.com/topic/102113-using-_session-for-select-query/#findComment-522737 Share on other sites More sharing options...
gammaman Posted April 21, 2008 Author Share Posted April 21, 2008 <?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"; } } ?> Link to comment https://forums.phpfreaks.com/topic/102113-using-_session-for-select-query/#findComment-522741 Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 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] Link to comment https://forums.phpfreaks.com/topic/102113-using-_session-for-select-query/#findComment-522744 Share on other sites More sharing options...
gammaman Posted April 21, 2008 Author Share Posted April 21, 2008 No that does not work either. Link to comment https://forums.phpfreaks.com/topic/102113-using-_session-for-select-query/#findComment-522762 Share on other sites More sharing options...
r2ks Posted April 23, 2009 Share Posted April 23, 2009 This is what i found and it works Hope it Helps You SELECT * FROM my_user WHERE username = '{$_SESSION["valid_user"]}' "; Link to comment https://forums.phpfreaks.com/topic/102113-using-_session-for-select-query/#findComment-817705 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.