It cannot display the table from database. For each program there is a table, in which some scores are saved. Plus, there is a program table in which program id
and program name
are saved. When I selected a program and a year I got the following errors:
No data found
Notice: Undefined variable: result in C:\\XAMPP\\htdocs\\statistics\\ac_directorPrograms.php on line 61
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in C:\\XAMPP\\htdocs\\statistics\\ac_directorPrograms.php on line 61
Something goes wrong with the result
from the action code and I guess with the prog_name
and program
form the form.
The form:
<?php
include 'connect.php';
?>
<form name="myform" action="ac_directorPrograms.php" method="POST">
<b>Programs:<b/>
<select name="program">
<option value="Choose">Please select..</option>
<?php
$sql = mysql_query("SELECT prog_name FROM program");
while ($row = mysql_fetch_array($sql)) {
echo "<option value='" . $row['prog_name'] . "'>" . $row['prog_name'] ."</option>";
}
?>
</select><br/><br/>
<b>Year:<b/>
<select name="year">
<option value="Choose">Please select..</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option></select><br/><br/>
<br/>
<input type="submit" value="submit" name="Submit">
<input type="reset" name="reset" value="Clear">
</form>
</div>
the action code
<?php
include 'connect.php';
$year = $_POST['year'];
$program = $_POST['program'];
$years = array(
2005,
2006,
2007
);
$programs = array(
'bsc computer science',
'bsc psychology',
'ba finance',
'ba marketing',
'ba management'
);
if (in_array($program, $programs) && in_array($year, $years)) {
$sql = "SELECT a1,a2,a3,l1,l2,l3,l4,l5,l6,l7,lavg,r1,r2,u1,u2,u3 FROM $program WHERE year=$year";
$result = mysql_query($sql);
}
else {
echo "No data found";
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../statistics/style.css">
</head>
<body>
<div id="container">
<table id="table" width="900" border="1" cellspacing="1">
<tbody>
<tr>
<td>A1 </td>
<td>A2 </td>
<td>A3 </td>
<td>L1 </td>
<td>L2 </td>
<td>L3 </td>
<td>L4 </td>
<td>L5 </td>
<td>L6 </td>
<td>L7 </td>
<td>LAVG </td>
<td>R1 </td>
<td>R2 </td>
<td>U1 </td>
<td>U2 </td>
<td>U3 </td>
</tr>
</tbody>
<?php
while($program=mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$program['a1']."</td>";
echo "<td>".$program['a2']."</td>";
echo "<td>".$pprogram['a3']."</td>";
echo "<td>".$pprogram['l1']."</td>";
echo "<td>".$program['l2']."</td>";
echo "<td>".$program['l3']."</td>";
echo "<td>".$program['l4']."</td>";
echo "<td>".$program['l5']."</td>";
echo "<td>".$program['l6']."</td>";
echo "<td>".$program['l7']."</td>";
echo "<td>".$program['lavg']."</td>";
echo "<td>".$program['r1']."</td>";
echo "<td>".$program['r2']."</td>";
echo "<td>".$program['u1']."</td>";
echo "<td>".$program['u2']."</td>";
echo "<td>".$program['u3']."</td>";
echo "</tr>";
}
?>
</table>
</div>
</body>
</html>
In the action code and after the include connect.php
, add this... $con=mysqli_connect("localhost","my_user","my_password","my_db");
and dont forget to replace my_user
, my_password
, my_db
with yours user, password and database name.
Also replace this code $result = mysql_query($sql);
with this one $result=mysqli_query($con,$sql);
This is the issue of scope of the variable. You have defined $result inside if condition and using that out side of if. You need to define $result out side of if condition.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.