从URL调用显示空白记录

时间:2016-03-31 17:00:56

标签: php mysql

我有一个过滤MYSQL查询的选项菜单(selFilter)。选项菜单选项用于链接以过滤第二个PHP页面。 export.php?recordID =

<select name="selFilter" id="selFilter" onchange="formFilter.submit()">
    <option value="%">all levels</option>
    <?php
do {  
?>
    <option value="<?php echo $row_RecordsetLevel['Level']?>"<?php if   
($varFilter_Recordset3 == $row_RecordsetLevel['Level']) {echo 'selected';}   
?>>level: <?php echo $row_RecordsetLevel['Level']?></option>
    <?php
} while ($row_RecordsetLevel = mysql_fetch_assoc($RecordsetLevel));
$rows = mysql_num_rows($RecordsetLevel);
if($rows > 0) {
  mysql_data_seek($RecordsetLevel, 0);
  $row_RecordsetLevel = mysql_fetch_assoc($RecordsetLevel);
 }
 ?>
  </select>

第二个php页面使用以下命令将查询导出到.csv:

$colname = "%";
if (isset($_GET['recordID'])) {
$colname = $_GET['recordID'];
}


$query = sprintf( 'SELECT Name, ID
FROM schedule WHERE Level LIKE %s ORDER BY Name', 
GetSQLValueString("%".$colname."%", "text"));

$result = mysql_query( $query, $conn ) or die( mysql_error( $conn ) );
//
// send response headers to the browser
// following headers instruct the browser to treat the data as a csv file 
called export.csv

//
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename=export.csv' );

当在index.php文件中选择了一个选项(selFilter)并且单击export.php时,一切正常。问题是如果我没有在选项菜单中选择任何内容并单击export.php链接,则URL字符串则为export.php?recordID =,其中recordID为空。该空白不会导致任何导出,零记录。如果我选择一个选项菜单级别,然后单击所有级别,则URL为export.php?recordID =%并正确导出。我该怎么办才能解决这个问题?

我尝试了以下操作,但导出网址仍为空白:

export.php?recordID=<?php $filter=$_POST['selFilter']; if (!empty($filter)) { echo "%"; } ?>

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。将导出链接更改为:

 export.php?recordID=<?php 
$filter=isset($_POST['selFilter']) ? $_POST['selFilter'] : '%';
 echo "/export_student.php?recordID=$filter";
 ?>

stackoverflow使用的解决方案:PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"

我刚刚替换了

$filter=isset($_POST['selFilter']) ? $_POST['selFilter'] : '';

 $filter=isset($_POST['selFilter']) ? $_POST['selFilter'] : '%';

因为我的查询是根据我的选项菜单触发未定义的索引而没有设置值。