在php中导出为CSV

时间:2017-02-06 05:59:58

标签: php mysql csv

出现问题导出的文件在文件上方有错误

  

注意:未定义的索引:年份 C:\ xampp \ htdocs \ new1v2 \ admin \ exporttoexcel.php 23
  注意:未定义索引: C:\ xampp \ htdocs \ new1v2 \ admin \ exporttoexcel.php 25 < / p>

但显然显示了结果。

这是我的表格,我将选择三个选择选项之一:

<form method="post" action="exporttoexcel.php">
        <select name="year" style="padding:5px; height:40px;">
                <option disabled selected>--Year Graduated</option>
                <?php
                    $year_grad=1995;
                    while($year_grad<=2017)
                    {
                ?>
                <option>
                    <?php echo $year_grad; ?>
                </option>
                    <?php $year_grad++; } ?>
            </select>
            <select name="deprtmnt" style="padding:5px; height:40px;">
                <option disabled selected>--Department</option>
                <option>College of Computer Studies</option>
            </select>
            <select name="kurs" style="padding:5px; height:40px;">
                <option disabled selected>--Course</option>
                <option>Bachelor of Science in Information Technology</option>
                <option>Bachelor of Science in Computer Studies</option>
            </select>
            <button type="submit" name="submit" class="w3-btn w3-green"><font size="6"><i class="fa fa-file-excel-o" aria-hidden="true"></i> Export to CSV</font></button>
    </form>

这是年,库尔和施工。 这是我的exporttoexcel.php的一部分

if (isset($_POST['submit'])) {
$year = $_POST['year'];

$kurs =  $_POST['kurs'];

$dept = $_POST['deprtmnt'];

$search_str= '';
        if ($year){
            $search_str .= $search_str ? ' AND ': 'WHERE' ;
            $search_str .= "`tblalumni`.`yeargrad` LIKE '%" .$year."%' "; 
        }
        if ($dept){
            $search_str .= $search_str ? ' AND ': 'WHERE' ;
            $search_str .= "`tblalumni`.`department` LIKE '%" .$dept."%' "; 
        }
        if ($kurs){
            $search_str .= $search_str ? ' AND ': 'WHERE' ;
            $search_str .= "`tblalumni`.`course` LIKE '%" .$kurs."%' "; 
        }


$output         = "";
$table          = "tblalumni"; // Enter Your Table Name
$sql            = mysql_query("SELECT fname as 'First Name', mname as 'Middle Name', lname as 'Last Name', yeargrad as 'Year Graduated', department as 'Department'  FROM tblAlumni {$search_str}  AND alum_status = 2");

该功能是三个选中的任何一个都会显示查询。

如何删除错误。请帮帮我。

1 个答案:

答案 0 :(得分:0)

您没有在选择框的<option>标记中定义值。这就是为什么php无法获得价值的原因。

将值添加到选项标记,如下所示:

$年

<option selected value="">--Year Graduated</option>

<option value="<?php echo $year_grad;">
    <?php echo $year_grad; ?>
 </option>

<强> $部门

<select name="deprtmnt" style="padding:5px; height:40px;">
      <option selected value="">--Department</option>
       <option value="College of Computer Studies">College of Computer Studies</option>
</select>

<强> $航向对接

<select name="kurs" style="padding:5px; height:40px;">
       <option selected value="">--Course</option>
       <option value="Bachelor of Science in Information Technology">Bachelor of Science in Information Technology</option>
       <option value="Bachelor of Science in Computer Studies">Bachelor of Science in Computer Studies</option>
</select>

接下来在PHP中,修改get输入代码,如下所示:

$year = (isset($_POST['year']) ? $_POST['year'] : "";

$kurs =  (isset($_POST['kurs']) ? $_POST['kurs'] : "";

$dept= (isset($_POST['dprtmnt']) ? $_POST['dprtmnt'] : "";

在你的if else代码中进行如下检查:

if($year != ''){
    // do your code here
}
if ($dept != ''){
    // do your code here
}
if ($kurs != ''){
    // do your code here
}

请记住,如果PHP抛出“Undefined index”错误,这意味着PHP无法访问变量的值,则变量值为“undefined”。