从php表导出一行

时间:2018-11-27 06:26:49

标签: php mysql csv

我尝试将一行导出为CSV,但是我的代码获取了所有行。

这是我的屏幕截图:export 这是我的代码: index.php

<?php
    db();
    global $link;

    $query  = "SELECT id, name, address, contact, age, birthday FROM info";
    $result = mysqli_query($link, $query);

    while($row = mysqli_fetch_assoc($result)){
        $id           = $row['id'];
        $name          = $row['name'];
        $address       = $row['address'];
        $contact       = $row['contact'];
        $age           = $row['age'];
        $birthday      = $row['birthday'];
        ?>

        <tr>
           <td><?php echo $row['id']; ?></td>
           <td><?php echo $row['name']; ?></td>
           <td><?php echo $row['address']; ?></td>
           <td><?php echo $row['contact']; ?></td>
           <td><?php echo $row['age']; ?></td>
           <td><?php echo $row['birthday']; ?></td>
           <td><a href="read.php?id=<?php echo $id?>">View</a></td>
           <td><a href="update.php?id=<?php echo $id?>">Update</a></td>
           <td><a href="delete.php?id=<?php echo $id?>" onClick="return confirm('Delete This account?')">Delete</a></td>
           <td>    
            <form method="post" action="export_excel.php" id="export_sql"> 
                <input type="submit" name="export" value="EXCEL"> 
            </form>
        </td>
        <td>
         <form method="post" action="export_pdf.php"  id="export_pdf" target="_blank">
            <input type="submit" name="export_pdf" value="PDF">
        </form> 
    </td>
</tr>

<?php } ?>

export_sql.php

<?php
if (isset($_POST["export"])) {
 $link = mysqli_connect("localhost", "root", "", "bondad-crud") ;
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=data.csv');
    $output = fopen("php://output", "w");
    fputcsv($output, array('id','name','address','contact','age','birthday', 'date'));
    $query = "SELECT * FROM info WHERE id='$id'";
    $result = mysqli_query($link, $query);
    while ($row = mysqli_fetch_assoc($result)) {
        fputcsv($output, $row);
    }
    fclose($output);
}


?>

我试图通过id来获取行,但是仍然出现错误。

2 个答案:

答案 0 :(得分:0)

更新后的答案

主要问题是您正在为while循环的每次迭代添加新表单。这将导致您遇到的问题类型。我已将<form>标签从循环内部移至循环外部。

您的第二个问题是您的ID没有正确地传递到您的导出页面。

我更改了您的两个提交按钮的名称,以便它们将在您的帖子数据中以数组的形式表示。数组中第一个元素的键是id。

db();
  global $link;

  $query  = "SELECT id, name, address, contact, age, birthday FROM info";
  $result = mysqli_query($link, $query);
  echo 
  '<form method="post" action="export_excel.php" id="export_sql">';

  echo 
  '<table>';

  while($row = mysqli_fetch_assoc($result)){

    $id         = $row['id'];
    $name       = $row['name'];
    $address    = $row['address'];
    $contact    = $row['contact'];
    $age        = $row['age'];
    $birthday   = $row['birthday'];

       echo
       '<tr>' .
          '<td>' . $row['id'] . '</td>' .
          '<td>' . $row['name'] . '</td>' .
          '<td>' . $row['address'] . '</td>' .
          '<td>' . $row['contact'] . '</td>' .
          '<td>' . $row['age'] . '</td>' .
          '<td>' . $row['birthday'] . '</td>' . 
          '<td><a href="read.php?id=<' . $id . '">View</a></td>' .
          '<td><a href="update.php?id=<' . $id . '">Update</a></td>' .
          '<td><a href="delete.php?id=<' . $id . '" onClick="return confirm("Delete This account?")">Delete</a></td>' .
          '<td><input type="submit" name="export[' . $id . ']" value="EXCEL">' . '</td>' .
          '<td><input type="submit" name="export_pdf[' . $id . ']" value="PDF"></td>' . 
        '</tr>';

}

  echo
  '</table>';

echo
'</form>';

请注意,您现在只有一种表格。

针对您的export_sql.php page

我们将获取$_POST[export]数组或$_POST[export_pdf]的第一个键,它就是您的ID。现在,我们有一个ID可以传递给正确的查询。

请注意,您现在可以同时从此页面处理Excel和PDF下载。

<?php
//Do code for Excels
if (isset($_POST["export"])) {
 $link = mysqli_connect("localhost", "root", "", "bondad-crud") ;
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=data.csv');
    $output = fopen("php://output", "w");
    fputcsv($output, array('id','name','address','contact','age','birthday', 'date'));

    reset($_POST["export"]); //Added this.
    $id = key($_POST["export"]); //And this.

    $query = "SELECT * FROM info WHERE id='$id'";
    $result = mysqli_query($link, $query);
    while ($row = mysqli_fetch_assoc($result)) {
        fputcsv($output, $row);
    }
    fclose($output);
}

//Handle the PDF's.
if (isset($_POST["export_pdf"])) {

  reset($_POST["export_pdf"]); //Added this.
  $id = key($_POST["export_pdf"]); //And this.

  //.....Do the rest of code for the PDF's

}

?>

希望有帮助。

答案 1 :(得分:0)

我认为您需要学习PHP Form Handling

您需要在两个页面上都传递ID。

您需要在index.php上具有隐藏输入<input type="hidden" name="id" value="<?php echo $row['id']; ?>">的传递ID

<?php
    db();
    global $link;

    $query  = "SELECT id, name, address, contact, age, birthday FROM info";
    $result = mysqli_query($link, $query);

    while($row = mysqli_fetch_assoc($result)){
        $id           = $row['id'];
        $name          = $row['name'];
        $address       = $row['address'];
        $contact       = $row['contact'];
        $age           = $row['age'];
        $birthday      = $row['birthday'];
        ?>

        <tr>
           <td><?php echo $row['id']; ?></td>
           <td><?php echo $row['name']; ?></td>
           <td><?php echo $row['address']; ?></td>
           <td><?php echo $row['contact']; ?></td>
           <td><?php echo $row['age']; ?></td>
           <td><?php echo $row['birthday']; ?></td>
           <td><a href="read.php?id=<?php echo $id?>">View</a></td>
           <td><a href="update.php?id=<?php echo $id?>">Update</a></td>
           <td><a href="delete.php?id=<?php echo $id?>" onClick="return confirm('Delete This account?')">Delete</a></td>
           <td>    
            <form method="post" action="export_excel.php" id="export_sql"> 
 <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
                <input type="submit" name="export" value="EXCEL"> 
            </form>
        </td>
        <td>
         <form method="post" action="export_pdf.php"  id="export_pdf" target="_blank">
     <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
            <input type="submit" name="export_pdf" value="PDF">
        </form> 
    </td>
        <td>
         <form method="post" action="export_sql.php"  id="export_pdf" target="_blank">
            <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
            <input type="submit" name="export_sql" value="PDF">
        </form> 
    </td>
</tr>

<?php } ?>

您需要处理index.php使用$ _POST发送的id,例如export_sql.php上的$id = $_POST["id"];

<?php
if (isset($_POST["export"])) {
 $id = $_POST["id"];
 $link = mysqli_connect("localhost", "root", "", "bondad-crud") ;
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=data.csv');
    $output = fopen("php://output", "w");
    fputcsv($output, array('id','name','address','contact','age','birthday', 'date'));
    $query = "SELECT * FROM info WHERE id='$id'";
    $result = mysqli_query($link, $query);
    while ($row = mysqli_fetch_assoc($result)) {
        fputcsv($output, $row);
    }
    fclose($output);
}


?>