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
<? //this page allows a user to search for courses being offered.
require("session.php"); //include the script that handles the session information ?>
<? $level = $_SESSION["level"];//get the user's level from the session table
$un = $_SESSION["user"];//get the user's username from the session table
/////check to make sure the user is logged in as a student. If not, re-direct them to login page.////////////////////////////
if($level != -1)
{
print "<script>";
print " self.location='https://205.153.60.235/login.php?unauth'";
print "</script>";
}
else
{
//////connect to the database///////////////////////////////////////////////////////////////////////////////////////////////
$usr = "";//database username
$pwd = "";//database password
$db = "";//database name
$host = "localhost";
mysql_connect($host,$usr,$pwd);
@mysql_select_db ($db) or die( "Unable to select database");?>
<? ///// begin html code to display information on the page///////////////////////////////////////////////////////////////// ?>
<HTML>
<HEAD>
<TITLE>Course Search</TITLE>
<? //include the created template for the layout of the links/header across left and top of page. ?>
<LINK REL=StyleSheet HREF="template.css" TYPE="text/css">
</HEAD>
<BODY>
<div id="mastHead">
<?php include("header\University-Header.html"); //include the header at the top of the page.?>
</div><? //close mastHead tag
////////////////links that are available from the current page////////////////////////////////////////////////////////////// ?>
<div id="linkIndex">
<div align="center">
<p><a href="https://205.153.60.235/login.php">Login/Logout</a></p>
<p><a href="https://205.153.60.235/ViewCurrentStudentInfo.php">View Personal Information</a></p>
<p><a href="https://205.153.60.235/CourseSearch.php">Search for Classes</a></p>
<p><a href="https://205.153.60.235/ViewClassSchedule.php">View Schedule</a></p>
<p><a href="https://205.153.60.235/ViewTuitionInfo.php">Pay Bill</a></p>
<p><a href="https://205.153.60.235/ViewStudentTranscript.php">View Transcript</a></p>
<p><a href="https://205.153.60.235/ViewExamSchedule.php">View Exam Schedule</a><br><br>
<a href="#"></a><br><br>
<a href="#"></a><br><br><br>
</p>
</div><? //close center alignment div tag ?>
</div> <? //close linkIndex div tag ?>
<div id="contentsBox"><!--Put all of your code in this div tag.-->
<div align="center">
<?
if (isset($_POST['ClassSearch'])) {
$username="";//database username
$password="";//database password
$database="";//database name
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
#set variables
$Subject= $_POST['Subject'];
$Section=$_POST['Section'];
$facid=$_POST['facid'];
$CourseTitle=$_POST['CourseTitle'];
$CourseNum=$_POST['CourseNum'];
/////statements that check the fields to make sure atleast a subject is entered, and searches according to other criteria.////////
if($Subject == " ")
{
print "<SCRIPT>window.alert('Sorry, you must select a subject.')</SCRIPT>";
print "<script>";
print " self.location='https://205.153.60.235/newCourseSearch.php'";
print "</script>";
}//end if
else if (($Subject != " ") && ($Section != " ") && ($facid != "") && ($CourseTitle != "") && ($CourseNum != ""))
{// setup SQL statement to retrieve the class search results.
$SQL = "SELECT *
FROM university.classes, university.classsections
WHERE Subject = '$Subject'
AND Section = '$Section'
AND facid LIKE '%$facid'
AND CourseTitle LIKE '%$CourseTitle'
AND CourseNum = '$CourseNum'";
}
else if (($Subject != " ") && ($Section != " ") && ($facid != "") && ($CourseTitle != "") && ($CourseNum == ""))
{// setup SQL statement to retrieve the class search results.
$SQL = "SELECT *
FROM university.classes, university.classsections
WHERE Subject = '$Subject'
AND Section = '$Section'
AND facid LIKE '%$facid'
AND CourseTitle LIKE '%$CourseTitle'";
}
else if (($Subject != " ") && ($Section != " ") && ($facid != "") && ($CourseTitle == "") && ($CourseNum != ""))
{// setup SQL statement to retrieve the class search results.
$SQL = "SELECT *
FROM university.classes, university.classsections
WHERE Subject = '$Subject'
AND Section = '$Section'
AND facid LIKE '%$facid'
AND CourseNum = '$CourseNum'";
}
else if (($Subject != " ") && ($Section != " ") && ($facid == "") && ($CourseTitle != "") && ($CourseNum != ""))
{// setup SQL statement to retrieve the class search results.
$SQL = "SELECT *
FROM university.classes, university.classsections
WHERE Subject = '$Subject'
AND Section = '$Section'
AND CourseTitle LIKE '%$CourseTitle'
AND CourseNum = '$CourseNum'";
}
else if (($Subject != " ") && ($Section == " ") && ($facid != "") && ($CourseTitle != "") && ($CourseNum != ""))
{// setup SQL statement to retrieve the class search results.
$SQL = "SELECT *
FROM university.classes, university.classsections
WHERE Subject = '$Subject'
AND facid LIKE '%$facid'
AND CourseTitle LIKE '%$CourseTitle'
AND CourseNum = '$CourseNum'";
}
else if (($Subject != " ") && ($Section != " ") && ($facid != "") && ($CourseTitle == "") && ($CourseNum == ""))
{// setup SQL statement to retrieve the class search results.
$SQL = "SELECT *
FROM university.classes, university.classsections
WHERE Subject = '$Subject'
AND Section = '$Section'
AND facid LIKE '%$facid'";
}
else if (($Subject != " ") && ($Section != " ") && ($facid == "") && ($CourseTitle != "") && ($CourseNum == ""))
{// setup SQL statement to retrieve the class search results.
$SQL = "SELECT *
FROM university.classes, university.classsections
WHERE Subject = '$Subject'
AND Section = '$Section'
AND CourseTitle LIKE '%$CourseTitle'";
}
else if (($Subject != "") && ($Section != "") && ($facid == "") && ($CourseTitle == "") && ($CourseNum != ""))
{// setup SQL statement to retrieve the class search results.
$SQL = "SELECT *
FROM university.classes, university.classsections
WHERE Subject = '$Subject'
AND Section = '$Section'
AND CourseNum = '$CourseNum'";
}
else if (($Subject != " ") && ($Section == " ") && ($facid != "") && ($CourseTitle != "") && ($CourseNum == ""))
{// setup SQL statement to retrieve the class search results.
$SQL = "SELECT *
FROM university.classes, university.classsections
WHERE Subject = '$Subject'
AND facid LIKE '%$facid'
AND CourseTitle LIKE '%$CourseTitle'";
}
else if (($Subject != " ") && ($Section == " ") && ($facid != "") && ($CourseTitle == "") && ($CourseNum != ""))
{// setup SQL statement to retrieve the class search results.
$SQL = "SELECT *
FROM university.classes, university.classsections
WHERE Subject = '$Subject'
AND facid LIKE '%$facid'
AND CourseNum = '$CourseNum'";
}
else if (($Subject != " ") && ($Section == " ") && ($facid == "") && ($CourseTitle != "") && ($CourseNum != ""))
{// setup SQL statement to retrieve the class search results.
$SQL = "SELECT *
FROM university.classes, university.classsections
WHERE Subject = '$Subject'
AND CourseTitle LIKE '%$CourseTitle'
AND CourseNum = '$CourseNum'";
}
else if (($Subject != " ") && ($Section != " ") && ($facid == "") && ($CourseTitle == "") && ($CourseNum == ""))
{// setup SQL statement to retrieve the class search results.
$SQL = "SELECT *
FROM university.classes, university.classsections
WHERE Subject = '$Subject'
AND Section = '$Section'";
}
else if (($Subject != " ") && ($Section == " ") && ($facid != "") && ($CourseTitle == "") && ($CourseNum == ""))
{// setup SQL statement to retrieve the class search results.
$SQL = "SELECT *
FROM university.classes, university.classsections
WHERE Subject = '$Subject'
AND facid LIKE '%$facid'";
}
else if (($Subject != " ") && ($Section == " ") && ($facid == "") && ($CourseTitle != "") && ($CourseNum == ""))
{// setup SQL statement to retrieve the class search results.
$SQL = "SELECT *
FROM university.classes, university.classsections
WHERE Subject = '$Subject'
AND CourseTitle LIKE '%$CourseTitle'";
}
else if (($Subject != " ") && ($Section == " ") && ($facid == "") && ($CourseTitle == "") && ($CourseNum != ""))
{// setup SQL statement to retrieve the class search results.
$SQL = "SELECT *
FROM university.classes, university.classsections
WHERE Subject = '$Subject'
AND CourseNum = '$CourseNum'";
}
else if (($Subject != " ") && ($Section == " ") && ($facid == "") && ($CourseTitle == "") && ($CourseNum == ""))
{// setup SQL statement to retrieve the class search results.
$SQL = "SELECT *
FROM university.classes, university.classsections
WHERE Subject = '$Subject'";
}
// execute mysql statement
$result=mysql_query($query);
$num=mysql_num_rows($result);
mysql_close();
//check to see if there are no results
if ($num == 0)
{
echo "<center>I am sorry, but there are no classes being offered that match what you are searching for.
<br>Please try searching for something else.</center>";
}
$i = 0;//initialize i at 0.
while ($i < $num)
{//loops through results until i is no longer less than num.
$Subject=mysql_result($result,$i,"Subject");
$Section=mysql_result($result,$i,"Section");
$facid=mysql_result($result,$i,"facid");
$CourseTitle=mysql_result($result,$i,"CourseTitle");
$CourseNum=mysql_result($result,$i,"CourseNum");
$MeetingTimes=mysql_result($result,$i,"MeetingTImes");
$Days=mysql_result($result,$i,"Days");
?>
<!---form to display the class information and allow the student to add it to their schedule if they choose to--->
<form id="form1" name="form1" method="post" action="">
<table width="336" height="206" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="124"><div align="right">Subject:</div></td>
<td width="212"><label><?php print $HTTP_GET_VARS['Subject']; ?></label> </td>
</tr>
<tr>
<td><div align="right">Section:</div></td>
<td><label><?php print $HTTP_GET_VARS['Section']; ?></label> </td>
</tr>
<tr>
<td><div align="right">Instructor:</div></td>
<td><label><?php print $HTTP_GET_VARS['facid']; ?></label> </td>
</tr>
<tr>
<td><div align="right">Course Title: </div></td>
<td><label><?php print $HTTP_GET_VARS['Title']; ?></label> </td>
</tr>
<tr>
<td><div align="right">Meeting Times: </div></td>
<td><label><?php print $HTTP_GET_VARS['MeetingTimes']; print " "; print $HTTP_GET_VARS['Days']; ?></label> </td>
</tr>
<tr>
<td><div align="right">Course Number: </div></td>
<td><label><?php print $HTTP_GET_VARS['CourseNum'];?></label> </td>
</tr>
</table>
<p>
<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";?>'" />
</p>
</form>;
<? $i++;
}
?> </table></center> <?
}
else
{?>
<!------- form to select what information you want to base your search on-------->
<form id="form" name="form" method="POST" action="<? $ECHO_SELF?>">
<p>
<label><span class="style2">Search for Courses:</span></label>
<br />
Use the selection options to search for a course you would like to add to your schedule.
You may choose any combination of fields to narrow your search, but you must select at least one Subject.
Click Search when your selection is complete.</p>
<table width="599" height="226" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="119" height="202" valign="top">
<p><strong>Subject:</strong></p>
<p><strong>Course Number:</strong></p>
<p><strong>Section:</strong></p>
<p><strong>Course Title:</strong></p>
<p><strong>Instructor: </strong></p></td>
<td width="480" valign="top"><p>
<select name="Subject" size="1" id="Subject">
<option value=" " selected="selected"></option>
<option value="ENGL">ENGL</option>
<option value="MATH">MATH</option>
<option value="CIS">CIS</option>
<option value="PHYS">PHYS</option>
<option value="CHEM">CHEM</option>
<option value="PEES">PEES</option>
<option value="PHIL">PHIL</option>
</select>
<br />
<br />
<input name="CourseNum" type="text" id="CourseNum" maxlength="3" />
</p>
<p>
<select name="Section" size="1" id="Section">
<option value=" " selected="selected"> </option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="25">25</option>
</select>
</p>
<p>
<input name="CourseTitle" type="text" id="CourseTitle" maxlength="25" />
</p>
<p>
<input name="facid" type="text" id="facid" maxlength="15" />
</p></td>
</tr>
<tr>
<td height="24" colspan="2" align="left" valign="top"><p>
<input name="ClassSearch" type="submit" id="ClassSearch" value="Search" />
<input type="reset" name="Reset" value="Reset" />
</p>
</td>
</tr>
</table>
<p>
<label></label>
</p>
</form>
<? }
}
?></div></div>
</HTML>