I had written and tested a PHP script that reads data from a MySQL database and presents it in free text and in a table. All was well until today when it stopped working. I've tried to debug for a couple hours and am getting nowhere. Is it possible that $_POST is keeping a value and therefore the script is not running?
I would appreciate any help anyone could give. Thank you.
<?php
//connect to database
$mysqli = mysqli_connect("localhost", "xxxxx", "xxxxx", "loc");
if (!$_POST) {
//haven't seen the selection form, so show it
$display_block = "<h1>Select an Entry</h1>";
//get parts of records
$get_list_sql = "SELECT mbr_id,
CONCAT_WS(', ', l_name, f_name) AS display_name
FROM member_name ORDER BY l_name, f_name";
$get_list_res = mysqli_query($mysqli, $get_list_sql) or die(mysqli_error($mysqli));
if (mysqli_num_rows($get_list_res) < 1) {
//no records
$display_block .= "<p><em>Sorry, no records to select!</em></p>";
} else {
//has records, so get results and print in a form
$display_block .= "
<form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">
<p><strong>Select a Record to View:</strong><br/>
<select name=\"sel_id\">
<option value=\"\">-- Select One --</option>";
while ($recs = mysqli_fetch_array($get_list_res)) {
$id = $recs['mbr_id'];
$display_name = stripslashes($recs['display_name']);
$display_block .= "<option value=\"".$id."\">".$display_name."</option>";
}
$display_block .= "
</select>
<p><input type=\"submit\" name=\"submit\" value=\"View Selected Entry\"></p>
</form>";
}
//free result
mysqli_free_result($get_list_res);
} else if ($_POST) {
//check for required fields
if ($_POST["sel_id"] == "") {
header("Location: selentry.php");
exit;
}
//get master_info
$get_master_sql = "SELECT concat_ws(' ', f_name, l_name) as display_name
FROM member_name WHERE mbr_id = '".$_POST["sel_id"]."'";
$get_master_res = mysqli_query($mysqli, $get_master_sql) or die(mysqli_error($mysqli));
while ($name_info = mysqli_fetch_array($get_master_res)) {
$display_name = stripslashes($name_info['display_name']);
}
$display_block = "<p>Record for LOC Member: ".$display_name."</p>";
//free result
mysqli_free_result($get_master_res);
//get all addresses
$get_addresses_sql = "SELECT address, city, state, zip, type
FROM address WHERE mbr_id = '".$_POST["sel_id"]."'";
$get_addresses_res = mysqli_query($mysqli, $get_addresses_sql) or die(mysqli_error($mysqli));
if (mysqli_num_rows($get_addresses_res) > 0) {
while ($add_info = mysqli_fetch_array($get_addresses_res)) {
$f_name = stripslashes($add_info['f_name']);
$l_name = stripslashes($add_info['l_name']);
$address = stripslashes($add_info['address']);
$city = stripslashes($add_info['city']);
$state = stripslashes($add_info['state']);
$zipcode = stripslashes($add_info['zip']);
$address_type = $add_info['type'];
$display_block .= "".$display_name."<br>$address<br>$city, $state $zipcode<br>";
}
}
//free result
mysqli_free_result($get_addresses_res);
//get all email
$get_email_sql = "SELECT email FROM email
WHERE mbr_id = '".$_POST["sel_id"]."'";
$get_email_res = mysqli_query($mysqli, $get_email_sql) or die(mysqli_error($mysqli));
if (mysqli_num_rows($get_email_res) > 0) {
while ($email_info = mysqli_fetch_array($get_email_res)) {
$email = stripslashes($email_info['email']);
$display_block .= "<a href='mailto:$email'>$email</a>";
}
}
//free result
mysqli_free_result($get_email_res);
//get all tel
$get_tel_sql = "SELECT h_phone, b_phone, c_phone, f_number FROM phone
WHERE mbr_id = '".$_POST["sel_id"]."'";
$get_tel_res = mysqli_query($mysqli, $get_tel_sql) or die(mysqli_error($mysqli));
if (mysqli_num_rows($get_tel_res) > 0) {
while ($tel_info = mysqli_fetch_array($get_tel_res)) {
$h_phone = stripslashes($tel_info['h_phone']);
$b_phone = stripslashes($tel_info['b_phone']);
$c_phone = stripslashes($tel_info['c_phone']);
$f_number = stripslashes($tel_info['f_number']);
$display_block .= "<p>Home Phone: $h_phone<br>Work Phone: $b_phone<br>Cell Phone: $c_phone<br>Fax: $f_number</p>";
}
}
//free result
mysqli_free_result($get_tel_res);
echo $display_block;
//get all car_desc
$get_car_sql = "SELECT car_id, year, model, body_num, engine_num, designer, builder, body_style, cond FROM car_desc
WHERE mbr_id = '".$_POST["sel_id"]."'";
$get_car_res = mysqli_query($mysqli, $get_car_sql) or die(mysqli_error($mysqli));
if (mysqli_num_rows($get_car_res) > 0) {
echo "<table width='100%' border='1' cellpadding='2' cellspacing='2' bgcolor='#ffffff'>";
echo "<tr>";
echo "<th>Car ID</th><th>Year</th><th>Model</th><th>Body Number</th><th>Engine Number</th><th>Designer</th><th>Builder</th><th>Body Style</th><th>Condition</th>";
echo "</tr>";
while ($car_info = mysqli_fetch_array($get_car_res)) {
$car_id = stripslashes($car_info['car_id']);
$year = stripslashes($car_info['year']);
$model = stripslashes($car_info['model']);
$body_num = stripslashes($car_info['body_num']);
$engine_num = stripslashes($car_info['engine_num']);
$designer = stripslashes($car_info['designer']);
$builder = stripslashes($car_info['builder']);
$body_style = stripslashes($car_info['body_style']);
$cond = stripslashes($car_info['cond']);
echo "<tr>";
echo "<td>$car_id</td><td>$year</td><td>$model</td><td>$body_num</td><td>$engine_num</td><td>$designer</td><td>
$builder</td><td>$body_style</td><td>$cond</td>";
echo "</tr>";
}
echo "</table>";
}
//free result
mysqli_free_result($get_car_res);
/* <p align=\"center\"><a href=\"".$_SERVER["PHP_SELF"]."\">select another</a></p>";*/
}
//close connection to MySQL
mysqli_close($mysqli);
?>
<html>
<head>
<title>LOC Member Record</title>
</head>
<body>
</body>
</html>