-
Varibles
I'm trying to access a variable from another form but cant seem to get it to work can any one see my problem?
Main Menu
Code:
<?
// script to display all the Degrees in the Degree table
// connection information
$hostName = "localhost";
$userName = "root";
$password = "12345";
$dbName = "SOC_DBtest";
// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");
mysql_select_db($dbName) or die("Unable to select database $dbName");
// Select all the fields in all the records of the Employees table
$query = "SELECT *
FROM tbl_degree";
$result = mysql_query($query);
// Determine the number of employees
$number = mysql_numrows($result);
// Create drop-down menu of degree names
print "View students enroled on a specified degree scheme:<p>
<form action=\"StudentDegree.php\" method=\"post\">
<select name=\"DegreeId\">
<option value=\"\">Select a degree scheme</option>";
for ($i=0; $i<$number; $i++) {
$DegreeId = mysql_result($result,$i,"DegreeId");
$Degree_Name = mysql_result($result,$i,"Degree_Name");
print "<option value=\"$DegreeId\">$Degree_Name</option>";
}
print "</select><input type=\"submit\" value=\"submit\"
name=\"submit\"></form>";
// Close the database connection
mysql_close();
?>
Result Page
Code:
<?
// script to display who is on a specific degree
// connection information
$hostName = "localhost";
$userName = "root";
$password = "12345";
$dbName = "SOC_DBtest";
// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");
mysql_select_db($dbName) or die( "Unable to select database $dbName");
// Select the fields from the appropriate tables
$query =
"SELECT tbl_student.Student_Id, tbl_student.Student_FName, tbl_student.Student_SName, tbl_degree.Degree_Name
FROM tbl_degree INNER JOIN tbl_student ON tbl_degree.Degree_Id = tbl_student.Degree_Id
WHERE ((tbl_student.Degree_Id = $DegreeId))";
$result = mysql_query($query);
// Determine the number of records returned
$number = mysql_numrows($result);
// Print the relevant information
print "There are $number student(s) studying this Degree:<p>";
for($i=0; $i<$number; $i++) {
$studentId = mysql_result($result, $i, "student_Id");
$stud_FName = mysql_result($result, $i, "Student_FName");
$stud_SName = mysql_result($result,$i,"Student_SName");
$stud_Degree = mysql_result($result,$i,"Degree_Name");
print "$studentId - $stud_FName $stud_SName - $stud_Degree<br>";
}
// Close the database connection
mysql_close();
?>
it prings up an error with my SQL query, i think it's the WHERE clause, where the $DegreeID isn't being accessed from the main menu
-
Re: Varibles
Whats the error message it gives you?
-
Re: Varibles
try
PHP Code:
"SELECT tbl_student.Student_Id, tbl_student.Student_FName, tbl_student.Student_SName, tbl_degree.Degree_Name
FROM tbl_degree INNER JOIN tbl_student ON tbl_degree.Degree_Id = tbl_student.Degree_Id
WHERE ((tbl_student.Degree_Id = {$_POST['DegreeId']}))";
If you dont have Register Globals turned on, which isnt a good idea, then you need to access the form information another way.
Check out http://uk.php.net/manual/en/reserved.variables.php for more infromation.
-
Re: Varibles
Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\Assignment2\StudentDegree.php on line 27
changed the code u supplied same error as above
-
Re: Varibles
Ok try then it must be your query add in mysql_error() after you execute your query. This will give you the last error that was generated by MySQL. This will be able to tell you if you have an error in your query.
-
Re: Varibles
The query it self is fine as i have taken the variable out and replaced it with a degree ID and it worked.
Code:
<?
// script to display who has which computers
// connection information
$hostName = "localhost";
$userName = "root";
$password = "12345";
$dbName = "SOC_DBtest";
// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");
mysql_select_db($dbName) or die( "Unable to select database $dbName");
// Select the fields from the appropriate tables
$query =
"SELECT tbl_student.Student_Id, tbl_student.Student_FName, tbl_student.Student_SName, tbl_degree.Degree_Name
FROM tbl_degree INNER JOIN tbl_student ON tbl_degree.Degree_Id = tbl_student.Degree_Id
WHERE ((tbl_student.Degree_Id = 1))";
$result = mysql_query($query);
// Determine the number of records returned
$number = mysql_numrows($result);
// Print the relevant information
print "There are $number student(s) studying this Degree:<p>";
for($i=0; $i<$number; $i++) {
$studentId = mysql_result($result, $i, "student_Id");
$stud_FName = mysql_result($result, $i, "Student_FName");
$stud_SName = mysql_result($result,$i,"Student_SName");
$stud_Degree = mysql_result($result,$i,"Degree_Name");
print "$studentId - $stud_FName $stud_SName - $stud_Degree<br>";
}
// Close the database connection
mysql_close();
?>
so it's the variables...read the post u gimme what am i lookin for?
-
Re: Varibles
Hey thanks for the help, but should the code snippet u sent be a GET not a POST? Thanks
The amended code
Code:
"SELECT tbl_student.Student_Id, tbl_student.Student_FName, tbl_student.Student_SName, tbl_degree.Degree_Name
FROM tbl_degree INNER JOIN tbl_student ON tbl_degree.Degree_Id = tbl_student.Degree_Id
WHERE ((tbl_student.Degree_Id = {$_GET['DegreeId']}))";
-
Re: Varibles
Well in your code you have:
PHP Code:
<form action="StudentDegree.php" method="post">
This means you need to use $_POST, how ever if the method was get then your would use $_GET.
Are you sure that there is a value being passed to the script, so check the source and make sure that there is a value in the <option> tag.
-
Re: Varibles
Also, shouldn't mysql_numrows(): be mysql_num_rows():?