从php表导出一行

0dxa2lsx  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(265)

我尝试将一行导出为csv,但是我的代码获取所有行。
这是我的截图:

下面是我的代码: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 } ?>

导出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获取行,但仍然出错。

j2cgzkjk

j2cgzkjk1#

更新的答案
主要问题是您的while循环的每次迭代都要添加一个新的表单。这将导致您遇到的问题类型。我已经移除了 <form> 从循环内部到循环外部的标记。
第二个问题是您的id没有正确地传递到导出页。
我更改了您的两个提交按钮的名称,以便它们将由post数据中的数组表示。数组中第一个元素的键是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

}

?>

希望有帮助。

zz2j4svz

zz2j4svz2#

我认为你需要学习php表单处理
你需要在两页上都传递id。
您需要带有隐藏输入的pass id <input type="hidden" name="id" value="<?php echo $row['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 } ?>

您需要使用$\u post like处理index.php发送的id $id = $_POST["id"]; 关于export\u 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);
}

?>

相关问题