Hi all,
I have a drop down box to choose a state in a html form. When you hit submit, it runs this code and looks up wether or not there are any records for that state and prompts you to save an excel file.
This works great if their are any records, but it prompts to save the file out if there is no records.
I've tried num-rows, ! $select, etc. and I cannot get it to display the "There are no records to display" message.
Here is my code:
<?
//make db conncetion
$conn = mysql_connect("x", "x", "x")
OR DIE (mysql_error());
@mysql_select_db ("x", $conn) OR DIE (mysql_error());
//get the data from the state drop down
$State = addslashes($_POST['State']);
//echo 'The state picked is: '.$State;
//export the data
$select = "SELECT * FROM Forms WHERE State='$State'";
//$select = "SELECT * FROM Forms";
//$num_rows = mysql_num_rows($select);
//if (mysql_num_rows < 2) {
if ( ! $select) {
echo 'There are no records for that state.';
echo ' <br><br>';
echo '<a href="pickstate.php"> Go Back and Try again</a>';
} else {
$export = mysql_query($select);
$fields = mysql_num_fields($export);
for ($i = 0; $i < $fields; $i++) {
$header .= mysql_field_name($export, $i) . "\t";
}
while($row = mysql_fetch_row($export)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) OR ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\r\n";
}
$data = str_replace("\r","",$data);
if ($data == "") {
$data = "";
}
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=report.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\r\n$data";
}
?>
This will be sweet once I get this fixed.
Also, does anyone have any suggestions on how to set the "processed" field in this table, once the above code is run?
Thanks a bunch,
Don