我一直在这个网站上工作,该网站打印提交用户的表格。我使用了“查看表单”按钮来通过FPDF生成表单的PDF。
 我的问题是我无法打印单击的表格。而是生成了最后提交的表单。
 这是代码:
Homepage.php:

<?php

$connection = mysqli_connect('localhost','root','','logindb1');

$output='';
if(isset($_POST['submit1'])){
// storing session
$username = $_SESSION['username'];

$query = mysqli_query($connection,"SELECT * FROM users2 WHERE username LIKE
'%$username%'") or die("Could not search!");
$count = mysqli_num_rows($query);


if($count == 0){
     echo "There was no search result!";
}
else{
    while($row=mysqli_fetch_array($query)){


 echo '<table width = "30%" cellpadding = "2" cellspacing ="2" border = "2px">
                <tr>
        <td><strong> ID</strong></td>
        <td><strong> username</strong> </td>

        <td><strong> EC. no</strong> </td>

        <td><strong> Division</strong> </td>
        <td><strong> ProjectCode</strong> </td>

        <td><strong> date of journey</strong> </td>
        <td><strong> return date</strong> </td>
        <td><strong> From </strong> </td>
        <td><strong> To</strong> </td>
        <td><button  type="submit" name="but" >view form</button></td>
                </tr>
                <tr>
        <td> <input  type="text" name="dbid" value='.$row['ID'].' size="4" readonly ></td>
        <td>'.$row['username'].'</td>

        <td>'.$row['ecno'].'</td>

        <td>'.$row['division'].'</td>
        <td>'.$row['code'].'</td>


    if (isset($_POST['but'])){




  $dbid = mysqli_real_escape_string($db, $_POST['dbid']);

  $_SESSION['dbid']= $dbid;


  header('location: invoice.php');


 }
 ?>


Invoice.php:

<?php
session_start();



$db = mysqli_connect("localhost", "root", "", "logindb1");
if (mysqli_connect_errno())
{
echo "something went wrong with the connection" . mysqli_connect_error();
}
  $dbid = $_SESSION['dbid'];
  $query = mysqli_query($db,"SELECT * FROM users2  WHERE ID ='$dbid'");
  while($row=mysqli_fetch_array($query)){

        $ID    =$row['ID'];
        $username=$row['username'];
        $email=$row['email'];
        $ecno =$row['ecno'];
        $gradepay=$row['gradepay'];


在Homepage.php中,我将输出ID作为输入文本框,并将相同的ID作为输入ID。此方法正确吗?有没有更有效的方法。请帮忙。

最佳答案

这是因为您没有session_start();在您的第一个文件中。

<?php
session_start();

$connection = mysqli_connect('localhost','root','','logindb1');


当您要设置或进行会话时,应执行此操作。

另外还有一个问题,您没有关闭字符串:

echo '<table width = "30%" cellpadding = "2" cellspacing ="2" border = "2px">
            <tr>
    <td><strong> ID</strong></td>
    <td><strong> username</strong> </td>

    <td><strong> EC. no</strong> </td>

    <td><strong> Division</strong> </td>
    <td><strong> ProjectCode</strong> </td>

    <td><strong> date of journey</strong> </td>
    <td><strong> return date</strong> </td>
    <td><strong> From </strong> </td>
    <td><strong> To</strong> </td>
    <td><button  type="submit" name="but" >view form</button></td>
            </tr>
            <tr>
    <td> <input  type="text" name="dbid" value='.$row['ID'].' size="4" readonly ></td>
    <td>'.$row['username'].'</td>

    <td>'.$row['ecno'].'</td>

    <td>'.$row['division'].'</td>
    <td>'.$row['code'].'</td> </tr></table>';

07-24 09:50