|
-
Mar 3rd, 2006, 11:55 AM
#1
Thread Starter
Addicted Member
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
Last edited by JamesBowtell; Mar 3rd, 2006 at 12:03 PM.
-
Mar 3rd, 2006, 12:03 PM
#2
<?="Moderator"?>
Re: Varibles
Whats the error message it gives you?
-
Mar 3rd, 2006, 12:08 PM
#3
<?="Moderator"?>
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.
-
Mar 3rd, 2006, 12:13 PM
#4
Thread Starter
Addicted Member
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
-
Mar 3rd, 2006, 12:24 PM
#5
<?="Moderator"?>
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.
-
Mar 3rd, 2006, 12:44 PM
#6
Thread Starter
Addicted Member
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?
-
Mar 3rd, 2006, 01:12 PM
#7
Thread Starter
Addicted Member
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']}))";
-
Mar 3rd, 2006, 02:27 PM
#8
<?="Moderator"?>
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.
-
Mar 4th, 2006, 06:31 AM
#9
Re: Varibles
Also, shouldn't mysql_numrows(): be mysql_num_rows():?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|