new to PHP and need help

Thread Solved

Join Date: Feb 2005
Posts: 46
Reputation: blackdove is an unknown quantity at this point 
Solved Threads: 0
blackdove blackdove is offline Offline
Light Poster

new to PHP and need help

 
0
  #1
Apr 12th, 2007
This is my first attempt at creating a website using PHP. My database is done in MySQL. It is for a made up university. I am having trouble creating the page that should allow current students enrolled at the university to search for courses and add them to their schedule. So far, I have the page load up correctly and display the form for the student to enter in the search information. However, whenever a subject is chosen, and the search button clicked, I get an error. I don't know what is causing it so I am not able to finish my page. I'm pretty sure I also have other problems also.

The error message I get is "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in <file name> on line 210"

I would appreciate it if someone would take the time to look through my code and help me find the problem, and point out any other major problems I might have. If that isn't possible, are there any good tutorials available online that could help me? I've searched google and couldn't find anything useful.

My code is below, I also attached a copy of the .php file
  1. <? //this page allows a user to search for courses being offered.
  2. require("session.php"); //include the script that handles the session information ?>
  3.  
  4. <? $level = $_SESSION["level"];//get the user's level from the session table
  5. $un = $_SESSION["user"];//get the user's username from the session table
  6.  
  7. /////check to make sure the user is logged in as a student. If not, re-direct them to login page.////////////////////////////
  8. if($level != -1)
  9. {
  10. print "<script>";
  11. print " self.location='https://205.153.60.235/login.php?unauth'";
  12. print "</script>";
  13. }
  14. else
  15. {
  16. //////connect to the database///////////////////////////////////////////////////////////////////////////////////////////////
  17. $usr = "";//database username
  18. $pwd = "";//database password
  19. $db = "";//database name
  20. $host = "localhost";
  21.  
  22. mysql_connect($host,$usr,$pwd);
  23. @mysql_select_db ($db) or die( "Unable to select database");?>
  24.  
  25. <? ///// begin html code to display information on the page///////////////////////////////////////////////////////////////// ?>
  26.  
  27. <HTML>
  28. <HEAD>
  29. <TITLE>Course Search</TITLE>
  30. <? //include the created template for the layout of the links/header across left and top of page. ?>
  31. <LINK REL=StyleSheet HREF="template.css" TYPE="text/css">
  32. </HEAD>
  33. <BODY>
  34. <div id="mastHead">
  35. <?php include("header\University-Header.html"); //include the header at the top of the page.?>
  36. </div><? //close mastHead tag
  37. ////////////////links that are available from the current page////////////////////////////////////////////////////////////// ?>
  38. <div id="linkIndex">
  39. <div align="center">
  40. <p><a href="https://205.153.60.235/login.php">Login/Logout</a></p>
  41. <p><a href="https://205.153.60.235/ViewCurrentStudentInfo.php">View Personal Information</a></p>
  42. <p><a href="https://205.153.60.235/CourseSearch.php">Search for Classes</a></p>
  43. <p><a href="https://205.153.60.235/ViewClassSchedule.php">View Schedule</a></p>
  44. <p><a href="https://205.153.60.235/ViewTuitionInfo.php">Pay Bill</a></p>
  45. <p><a href="https://205.153.60.235/ViewStudentTranscript.php">View Transcript</a></p>
  46. <p><a href="https://205.153.60.235/ViewExamSchedule.php">View Exam Schedule</a><br><br>
  47. <a href="#"></a><br><br>
  48. <a href="#"></a><br><br><br>
  49. </p>
  50. </div><? //close center alignment div tag ?>
  51. </div> <? //close linkIndex div tag ?>
  52.  
  53.  
  54. <div id="contentsBox"><!--Put all of your code in this div tag.-->
  55. <div align="center">
  56. <?
  57. if (isset($_POST['ClassSearch'])) {
  58. $username="";//database username
  59. $password="";//database password
  60. $database="";//database name
  61. mysql_connect(localhost,$username,$password);
  62. @mysql_select_db($database) or die( "Unable to select database");
  63.  
  64. #set variables
  65. $Subject= $_POST['Subject'];
  66. $Section=$_POST['Section'];
  67. $facid=$_POST['facid'];
  68. $CourseTitle=$_POST['CourseTitle'];
  69. $CourseNum=$_POST['CourseNum'];
  70.  
  71. /////statements that check the fields to make sure atleast a subject is entered, and searches according to other criteria.////////
  72. if($Subject == " ")
  73. {
  74. print "<SCRIPT>window.alert('Sorry, you must select a subject.')</SCRIPT>";
  75. print "<script>";
  76. print " self.location='https://205.153.60.235/newCourseSearch.php'";
  77. print "</script>";
  78. }//end if
  79. else if (($Subject != " ") && ($Section != " ") && ($facid != "") && ($CourseTitle != "") && ($CourseNum != ""))
  80. {// setup SQL statement to retrieve the class search results.
  81. $SQL = "SELECT *
  82. FROM university.classes, university.classsections
  83. WHERE Subject = '$Subject'
  84. AND Section = '$Section'
  85. AND facid LIKE '%$facid'
  86. AND CourseTitle LIKE '%$CourseTitle'
  87. AND CourseNum = '$CourseNum'";
  88. }
  89. else if (($Subject != " ") && ($Section != " ") && ($facid != "") && ($CourseTitle != "") && ($CourseNum == ""))
  90. {// setup SQL statement to retrieve the class search results.
  91. $SQL = "SELECT *
  92. FROM university.classes, university.classsections
  93. WHERE Subject = '$Subject'
  94. AND Section = '$Section'
  95. AND facid LIKE '%$facid'
  96. AND CourseTitle LIKE '%$CourseTitle'";
  97. }
  98. else if (($Subject != " ") && ($Section != " ") && ($facid != "") && ($CourseTitle == "") && ($CourseNum != ""))
  99. {// setup SQL statement to retrieve the class search results.
  100. $SQL = "SELECT *
  101. FROM university.classes, university.classsections
  102. WHERE Subject = '$Subject'
  103. AND Section = '$Section'
  104. AND facid LIKE '%$facid'
  105. AND CourseNum = '$CourseNum'";
  106. }
  107. else if (($Subject != " ") && ($Section != " ") && ($facid == "") && ($CourseTitle != "") && ($CourseNum != ""))
  108. {// setup SQL statement to retrieve the class search results.
  109. $SQL = "SELECT *
  110. FROM university.classes, university.classsections
  111. WHERE Subject = '$Subject'
  112. AND Section = '$Section'
  113. AND CourseTitle LIKE '%$CourseTitle'
  114. AND CourseNum = '$CourseNum'";
  115. }
  116. else if (($Subject != " ") && ($Section == " ") && ($facid != "") && ($CourseTitle != "") && ($CourseNum != ""))
  117. {// setup SQL statement to retrieve the class search results.
  118. $SQL = "SELECT *
  119. FROM university.classes, university.classsections
  120. WHERE Subject = '$Subject'
  121. AND facid LIKE '%$facid'
  122. AND CourseTitle LIKE '%$CourseTitle'
  123. AND CourseNum = '$CourseNum'";
  124. }
  125. else if (($Subject != " ") && ($Section != " ") && ($facid != "") && ($CourseTitle == "") && ($CourseNum == ""))
  126. {// setup SQL statement to retrieve the class search results.
  127. $SQL = "SELECT *
  128. FROM university.classes, university.classsections
  129. WHERE Subject = '$Subject'
  130. AND Section = '$Section'
  131. AND facid LIKE '%$facid'";
  132. }
  133. else if (($Subject != " ") && ($Section != " ") && ($facid == "") && ($CourseTitle != "") && ($CourseNum == ""))
  134. {// setup SQL statement to retrieve the class search results.
  135. $SQL = "SELECT *
  136. FROM university.classes, university.classsections
  137. WHERE Subject = '$Subject'
  138. AND Section = '$Section'
  139. AND CourseTitle LIKE '%$CourseTitle'";
  140. }
  141. else if (($Subject != "") && ($Section != "") && ($facid == "") && ($CourseTitle == "") && ($CourseNum != ""))
  142. {// setup SQL statement to retrieve the class search results.
  143. $SQL = "SELECT *
  144. FROM university.classes, university.classsections
  145. WHERE Subject = '$Subject'
  146. AND Section = '$Section'
  147. AND CourseNum = '$CourseNum'";
  148. }
  149. else if (($Subject != " ") && ($Section == " ") && ($facid != "") && ($CourseTitle != "") && ($CourseNum == ""))
  150. {// setup SQL statement to retrieve the class search results.
  151. $SQL = "SELECT *
  152. FROM university.classes, university.classsections
  153. WHERE Subject = '$Subject'
  154. AND facid LIKE '%$facid'
  155. AND CourseTitle LIKE '%$CourseTitle'";
  156. }
  157. else if (($Subject != " ") && ($Section == " ") && ($facid != "") && ($CourseTitle == "") && ($CourseNum != ""))
  158. {// setup SQL statement to retrieve the class search results.
  159. $SQL = "SELECT *
  160. FROM university.classes, university.classsections
  161. WHERE Subject = '$Subject'
  162. AND facid LIKE '%$facid'
  163. AND CourseNum = '$CourseNum'";
  164. }
  165. else if (($Subject != " ") && ($Section == " ") && ($facid == "") && ($CourseTitle != "") && ($CourseNum != ""))
  166. {// setup SQL statement to retrieve the class search results.
  167. $SQL = "SELECT *
  168. FROM university.classes, university.classsections
  169. WHERE Subject = '$Subject'
  170. AND CourseTitle LIKE '%$CourseTitle'
  171. AND CourseNum = '$CourseNum'";
  172. }
  173. else if (($Subject != " ") && ($Section != " ") && ($facid == "") && ($CourseTitle == "") && ($CourseNum == ""))
  174. {// setup SQL statement to retrieve the class search results.
  175. $SQL = "SELECT *
  176. FROM university.classes, university.classsections
  177. WHERE Subject = '$Subject'
  178. AND Section = '$Section'";
  179. }
  180. else if (($Subject != " ") && ($Section == " ") && ($facid != "") && ($CourseTitle == "") && ($CourseNum == ""))
  181. {// setup SQL statement to retrieve the class search results.
  182. $SQL = "SELECT *
  183. FROM university.classes, university.classsections
  184. WHERE Subject = '$Subject'
  185. AND facid LIKE '%$facid'";
  186. }
  187. else if (($Subject != " ") && ($Section == " ") && ($facid == "") && ($CourseTitle != "") && ($CourseNum == ""))
  188. {// setup SQL statement to retrieve the class search results.
  189. $SQL = "SELECT *
  190. FROM university.classes, university.classsections
  191. WHERE Subject = '$Subject'
  192. AND CourseTitle LIKE '%$CourseTitle'";
  193. }
  194. else if (($Subject != " ") && ($Section == " ") && ($facid == "") && ($CourseTitle == "") && ($CourseNum != ""))
  195. {// setup SQL statement to retrieve the class search results.
  196. $SQL = "SELECT *
  197. FROM university.classes, university.classsections
  198. WHERE Subject = '$Subject'
  199. AND CourseNum = '$CourseNum'";
  200. }
  201. else if (($Subject != " ") && ($Section == " ") && ($facid == "") && ($CourseTitle == "") && ($CourseNum == ""))
  202. {// setup SQL statement to retrieve the class search results.
  203. $SQL = "SELECT *
  204. FROM university.classes, university.classsections
  205. WHERE Subject = '$Subject'";
  206. }
  207.  
  208. // execute mysql statement
  209. $result=mysql_query($query);
  210. $num=mysql_num_rows($result);
  211. mysql_close();
  212.  
  213. //check to see if there are no results
  214. if ($num == 0)
  215. {
  216. echo "<center>I am sorry, but there are no classes being offered that match what you are searching for.
  217. <br>Please try searching for something else.</center>";
  218. }
  219.  
  220. $i = 0;//initialize i at 0.
  221. while ($i < $num)
  222. {//loops through results until i is no longer less than num.
  223. $Subject=mysql_result($result,$i,"Subject");
  224. $Section=mysql_result($result,$i,"Section");
  225. $facid=mysql_result($result,$i,"facid");
  226. $CourseTitle=mysql_result($result,$i,"CourseTitle");
  227. $CourseNum=mysql_result($result,$i,"CourseNum");
  228. $MeetingTimes=mysql_result($result,$i,"MeetingTImes");
  229. $Days=mysql_result($result,$i,"Days");
  230. ?>
  231. <!---form to display the class information and allow the student to add it to their schedule if they choose to--->
  232. <form id="form1" name="form1" method="post" action="">
  233. <table width="336" height="206" border="0" cellpadding="0" cellspacing="0">
  234. <tr>
  235. <td width="124"><div align="right">Subject:</div></td>
  236. <td width="212"><label><?php print $HTTP_GET_VARS['Subject']; ?></label>&nbsp;</td>
  237. </tr>
  238. <tr>
  239. <td><div align="right">Section:</div></td>
  240. <td><label><?php print $HTTP_GET_VARS['Section']; ?></label>&nbsp;</td>
  241. </tr>
  242. <tr>
  243. <td><div align="right">Instructor:</div></td>
  244. <td><label><?php print $HTTP_GET_VARS['facid']; ?></label>&nbsp;</td>
  245. </tr>
  246. <tr>
  247. <td><div align="right">Course Title: </div></td>
  248. <td><label><?php print $HTTP_GET_VARS['Title']; ?></label>&nbsp;</td>
  249. </tr>
  250. <tr>
  251. <td><div align="right">Meeting Times: </div></td>
  252. <td><label><?php print $HTTP_GET_VARS['MeetingTimes']; print " "; print $HTTP_GET_VARS['Days']; ?></label>&nbsp;</td>
  253. </tr>
  254. <tr>
  255. <td><div align="right">Course Number: </div></td>
  256. <td><label><?php print $HTTP_GET_VARS['CourseNum'];?></label>&nbsp;</td>
  257. </tr>
  258. </table>
  259. <p>
  260. <INPUT TYPE="BUTTON" VALUE="Add Class" ONCLICK="window.location.href='https://205.153.60.235/StudentAddClass.php?Subject=<?php echo "$Subject";?>&Section=<?php echo "$Section";?>&facid=<?php echo "$facid";?>&Title=<?php echo "$Title";?>&MeetingTimes=<?php echo "$MeetingTimes";?>&CourseNum=<?php echo "$CourseNum";?>'" />
  261. </p>
  262. </form>;
  263. <? $i++;
  264. }
  265. ?> </table></center> <?
  266. }
  267. else
  268. {?>
  269. <!------- form to select what information you want to base your search on-------->
  270. <form id="form" name="form" method="POST" action="<? $ECHO_SELF?>">
  271. <p>
  272. <label><span class="style2">Search for Courses:</span></label>
  273. <br />
  274. Use the selection options to search for a course you would like to add to your schedule.
  275. You may choose any combination of fields to narrow your search, but you must select at least one Subject.
  276. Click Search when your selection is complete.</p>
  277. <table width="599" height="226" border="0" cellpadding="0" cellspacing="0">
  278. <tr>
  279. <td width="119" height="202" valign="top">
  280. <p><strong>Subject:</strong></p>
  281. <p><strong>Course Number:</strong></p>
  282. <p><strong>Section:</strong></p>
  283. <p><strong>Course Title:</strong></p>
  284. <p><strong>Instructor: </strong></p></td>
  285. <td width="480" valign="top"><p>
  286. <select name="Subject" size="1" id="Subject">
  287. <option value=" " selected="selected"></option>
  288. <option value="ENGL">ENGL</option>
  289. <option value="MATH">MATH</option>
  290. <option value="CIS">CIS</option>
  291. <option value="PHYS">PHYS</option>
  292. <option value="CHEM">CHEM</option>
  293. <option value="PEES">PEES</option>
  294. <option value="PHIL">PHIL</option>
  295. </select>
  296. <br />
  297. <br />
  298. <input name="CourseNum" type="text" id="CourseNum" maxlength="3" />
  299. </p>
  300. <p>
  301. <select name="Section" size="1" id="Section">
  302. <option value=" " selected="selected"> </option>
  303. <option value="1">1</option>
  304. <option value="2">2</option>
  305. <option value="3">3</option>
  306. <option value="4">4</option>
  307. <option value="5">5</option>
  308. <option value="6">6</option>
  309. <option value="7">7</option>
  310. <option value="8">8</option>
  311. <option value="9">9</option>
  312. <option value="10">10</option>
  313. <option value="11">11</option>
  314. <option value="12">12</option>
  315. <option value="13">13</option>
  316. <option value="14">14</option>
  317. <option value="15">15</option>
  318. <option value="16">16</option>
  319. <option value="17">17</option>
  320. <option value="18">18</option>
  321. <option value="19">19</option>
  322. <option value="20">20</option>
  323. <option value="21">21</option>
  324. <option value="22">22</option>
  325. <option value="23">23</option>
  326. <option value="25">25</option>
  327. </select>
  328. </p>
  329. <p>
  330. <input name="CourseTitle" type="text" id="CourseTitle" maxlength="25" />
  331. </p>
  332. <p>
  333. <input name="facid" type="text" id="facid" maxlength="15" />
  334. </p></td>
  335. </tr>
  336. <tr>
  337. <td height="24" colspan="2" align="left" valign="top"><p>
  338. <input name="ClassSearch" type="submit" id="ClassSearch" value="Search" />
  339. <input type="reset" name="Reset" value="Reset" />
  340. </p>
  341. </td>
  342. </tr>
  343. </table>
  344. <p>
  345. <label></label>
  346. </p>
  347. </form>
  348. <? }
  349. }
  350. ?></div></div>
  351. </HTML>
Attached Files
File Type: php SearchCourses.php (16.6 KB, 4 views)
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 84
Reputation: Johnathan is an unknown quantity at this point 
Solved Threads: 2
Johnathan's Avatar
Johnathan Johnathan is offline Offline
Junior Poster in Training

Re: new to PHP and need help

 
0
  #2
Apr 13th, 2007
when i look at your example page in dreamweaver I noticed that on your first form you didnt have an action set this could cause problems I also noticed that dreamweaver doesnt recognize the fact that you have a database set in it you might want to relook your database and make sure that it is set right
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 2
Reputation: PuneetSeo is an unknown quantity at this point 
Solved Threads: 1
PuneetSeo PuneetSeo is offline Offline
Newbie Poster

Re: new to PHP and need help

 
0
  #3
Apr 13th, 2007
plz explain it in short .............
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 57
Reputation: GliderPilot is an unknown quantity at this point 
Solved Threads: 2
GliderPilot's Avatar
GliderPilot GliderPilot is offline Offline
Junior Poster in Training

Re: new to PHP and need help

 
0
  #4
Apr 13th, 2007
Your problem is not with the mysql_num_rows it is with the line before it the mysql_query line.

Your query line looks like this:
$result=mysql_query($query);

However you did not define the $query variable. Technicaly the error is at the query, but because it is not a serious error the code continues to go.

You place your querys in a variable named $SQL

You should replace either the $SQLs with $query or Replace $query with $SQL.

ie.
$result=mysql_query($SQL);

As well as a good habit you should put error catching on your queries to ensure they process properly like so:

  1. $result=mysql_query($SQL) or die("Unable to process database query. Please contact the admin: " . mysql_error());

With this in place if there is an error with the query it will stop processing code, and display the error message above along with the error message from mySql
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 46
Reputation: blackdove is an unknown quantity at this point 
Solved Threads: 0
blackdove blackdove is offline Offline
Light Poster

Re: new to PHP and need help

 
0
  #5
Apr 14th, 2007
Thanks, I will try that and see iif I can fix my code from there.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC