简体   繁体   English

从php表导出一行

[英]Export a row from php table

I try to export a single row as CSV but instead, my code fetches all the rows. 我尝试将一行导出为CSV,但是我的代码获取了所有行。

Here is my screenshot: 这是我的截图: 出口 Here is my code: index.php 这是我的代码: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 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);
}


?>

I tried to fetch the row by id but still got an error. 我试图按ID提取行,但仍然出现错误。

Updated Answer 更新的答案

The main problem is that your are adding a new form for each iteration of your while loop. 主要问题是您要为while循环的每次迭代添加新表单。 This will cause the type of problems you are having. 这将导致您遇到的问题类型。 I have removed the <form> tags from the inside of the loop to the outside of the loop. 我已经将<form>标记从循环内部移至循环外部。

Your second problem is that your ID is not correctly being passed to your export page(s). 第二个问题是您的ID没有正确地传递到导出页面。

I changed the names of your two submit buttons so that they will be represented by an array in your post data. 我更改了两个提交按钮的名称,以便它们将在您的帖子数据中以数组表示。 The key of the first element in the array will be the 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>';

Notice that you only have one form now. 请注意,您现在只有一种形式。

For your export_sql.php page : 对于您的export_sql.php page

We will grab the first key of the $_POST[export] array or the $_POST[export_pdf] and that will be you ID. 我们将获取$_POST[export]数组或$_POST[export_pdf]数组的第一个键,它就是您的ID。 Now we have an ID to pass to the correct query. 现在,我们有一个ID可以传递给正确的查询。

Note that you can now handle both the Excel and the PDF download from this page. 请注意,您现在可以从此页面处理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

}

?>

Hope it helps. 希望能帮助到你。

I think you shall need to learn PHP Form Handling 我认为您需要学习PHP表单处理

You need to pass the id on both pages. 您需要在两个页面上都传递ID。

You need pass id with hidden input <input type="hidden" name="id" value="<?php echo $row['id']; ?>"> on index.php 您需要在隐藏的输入<input type="hidden" name="id" value="<?php echo $row['id']; ?>">上通过pass id在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="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 } ?>

and you need proccess the id sent by index.php using $_POST like $id = $_POST["id"]; 并且您需要使用$ _POST处理index.php发送的id,例如$id = $_POST["id"]; on export_sql.php 在export_sql.php上

<?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);
}


?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM