Hi guys after a few adjustments with my query form
I got this problem:
Notice: Undefined variable: row... in line 178
What I want to do is enter the student number then click the Find button, then display the record in the from.
Here is my code:
Code:
if(isset($_POST['find'])) { // find the athlete
// Connect the user in the database.
require_once ('../mysql_connect.php'); // Connect to the db.
// Create a function for escaping the data.
function escape_data ($data) {
global $dbc; // Need the connection.
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
return mysql_real_escape_string($data, $dbc);
} // End of function.
$message = NULL; // Create an empty new variable.
// check for student number
if (empty($_POST['student_number'])) { // if student number is not entered
$sn = FALSE;
// Check for a last name.
if (empty($_POST['last_name'])) { // check the surname of the athlete.
$ln = FALSE;
$message .= '<p>You forgot to enter the student number or the the Surname of the athlete to search!</p>';
} else {
$ln = escape_data($_POST['last_name']);
}
} else {
$sn = escape_data($_POST['student_number']);
}
if ($sn) { // If student number is entered.
$query = "SELECT studno, lname, fname, mname, nname, course, presentadd, telno, cellno, email FROM basic_info WHERE studno='$sn'";
} else { // last name is entered
$query = "SELECT studno, lname, fname, mname, nname, course, presentadd, telno, cellno, email FROM basic_info WHERE lname='$ln'";
}
$result = @mysql_query($query); // Run the query.
if ( mysql_errno() > 0 ) {
echo mysql_error();
exit;
}
if ( mysql_num_rows( $result ) == 0 ) {
echo "no results";
}
else {
// display the data in the form
$student_number =$row['StudNo']; // this is line 178
$last_name = $row['lname'];
}
} // End of Find
// Print the error message if there is one.
if (isset($message)) {
echo '<font color="red">', $message, '</font>';
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset><legend>Enter your information in the form below:</legend>
<p><b>Student Number:</b> <input type="text" name="student_number" size="15" maxlength="8" value="<?php if (isset($_POST['first_name'])) echo $_POST['student_number']; ?>" /></p>
<p><b>Last Name:</b> <input type="text" name="last_name" size="20" maxlength="20" value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>
<p><b>First Name:</b> <input type="text" name="first_name" size="40" maxlength="40" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>
<p><b>Middle Name:</b> <input type="text" name="middle_name" size="20" maxlength="20" value="<?php if (isset($_POST['middle_name'])) echo $_POST['middle_name']; ?>" /></p>
<p><b>Nick Name:</b> <input type="text" name="nick_name" size="15" maxlength="15" value="<?php if (isset($_POST['nick_name'])) echo $_POST['nick_name']; ?>" /></p>
<p><b>Course:</b> <input type="text" name="course" size="15" maxlength="15" value="<?php if (isset($_POST['course'])) echo $_POST['course']; ?>" /></p>
<p><b>Present Address:</b> <input type="text" name="present_address" size="40" maxlength="40" value="<?php if (isset($_POST['present_address'])) echo $_POST['present_address']; ?>" /> </p>
<p><b>Email Address:</b> <input type="text" name="email" size="40" maxlength="40" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /> </p>
<p><b>Phone Number:</b> <input type="text" name="phone_number" size="10" maxlength="50" value="<?php if (isset($_POST['phone_number'])) echo $_POST['phone_number']; ?>" /></p>
<p><b>Cell Number:</b> <input type="text" name="cell_number" size="20" maxlength="50" value="<?php if (isset($_POST['cell_number'])) echo $_POST['cell_number']; ?>" /></p>
</fieldset>
<div align="center">
<input type="submit" name="find" value="Find Athlete" />
</div>
</form><!-- End of Form -->
if ( mysql_num_rows( $result ) == 0 ) {
echo "no results";
}
else {
// display the data in the form
$student_number =$row['StudNo']; // this is line 178
$last_name = $row['lname'];
}
Why does it say undefined variable: row?
Simply because you haven't defined the $row variable yet
Seriously, after you have executed your $query, you have a resource *pointer* to the returned results in $results - it doesn't contain any database rows or anything useful yet. What you want to do is to get all the database rows - pointed by this resource pointer - with functions mysql_fetch_row() or mysql_fetch_assoc() - you can do it with a loop that could look something like this:
PHP Code:
while ($row = mysql_fetch_assoc($result)) {
// handle your database $row here
print_r($row); // echoes the whole $row to screen
}
See, the $row gets defined in the beginning of the loop, and when there is no more rows to handle, it gets the value false and the loop exits. I hope that helps.
You can probably get some results by changing your code a bit:
PHP Code:
else {
while ($row = mysql_fetch_assoc($result)) {
// display the data in the form
$student_number =$row['StudNo']; // this is line 178
$last_name = $row['lname'];
echo "$last_name: $student_number<br>"
}
}