【问题标题】:how to get single row data in table php如何在表php中获取单行数据
【发布时间】:2021-05-01 06:01:00
【问题描述】:

这是显示数据库中所有数据的主表,当我单击打印时,它应该只打印该行但它不显示我得到“未定义的变量:行”,我开始认为这与此有关这里是表格的代码:

 <div class="container" >
        <div class="well" ">
            <span class="pull-right"><a data-toggle="modal" data-target="#myModal" href="#myModal" class="btn btn-primary"><span class="glyphicon glyphicon-plus"></span> Enroll Pupil</a></span>
            <div>
</div>
                <table class="table table-striped table-bordered table-hover" >
                    <thead>
                        <th >Moble Number</th>
                        <th >First Name</th>
                        <th>Last Name</th>
                        <th>Section</th>
                        <th>Grade</th>
                        <th>Action</th>
                    </thead>

                    <tbody>
                        <?php
                        include('conn.php');

                        $query=mysqli_query($conn,"select * from `tbl_students`");
                        while($row=mysqli_fetch_array($query)){
                        ?>

                        <tr s>
                            <td><?php echo $row['LRN']; ?></td>
                            <td><?php echo $row['FirstName']; ?></td>
                            <td><?php echo $row['LastName']; ?></td>
                            <td><?php echo $row['Section']; ?></td>
                            <td><?php echo $row['GradeLevel']; ?></td>
                            
                            <td >
                                <a href="#editstudent<?php echo $row['LRN']; ?>" data-toggle="modal" class="btn btn-warning"><span class="glyphicon glyphicon-edit"></span> Edit</a> | | 
                                <a href="#delstudent<?php echo $row['LRN']; ?>" data-toggle="modal" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span> Delete</a> | |
                                <a href="#addmoto<?php echo $row['LRN']; ?>" data-toggle="modal" data-target="#addmoto" class="btn btn-info"><span class="glyphicon glyphicon-edit"></span> Add Subject</a> | |
                                <a class="btn btn-success" onclick="window.open('IndivPrintTableStudent.php').print();">Print</a>
                            <?php include('buttonstudent.php'); ?>
                            </td>
                        </tr>
                        <?php
                    }

                    ?>
                </tbody>

这是我的数据库

 <?php include"includes/config.php" ?>
<link rel="stylesheet" type="text/css" href="Print.css" media="print"></link>
     <div class="container" >

<table>
                  <thead>
                        <th style="border: 3px solid #2F323A; text-align: center; padding-bottom: 8px;">Moble Number</th>
                        <th style="border: 3px solid #2F323A; text-align: center; width: 14%;">First Name</th>
                        <th style="border: 3px solid #2F323A; text-align: center; width: 14%;">Last Name</th>
                        <th style="border: 3px solid #2F323A; text-align: center; padding-bottom: 9px;">Section</th>
                        <th style="border: 3px solid #2F323A; text-align: center; width: 10%;">Grade</th>
                        
                    </thead>

                    <tbody>
                       
                <?php
                    $query=mysqli_query($con,"SELECT * FROM tbl_students WHERE LRN='".$row['LRN']."'");
                        while($row=mysqli_fetch_array($query)){
                             ?>
                
                        <tr style="background-color: #f0f8ff; border: 3px solid #2F323A;">
                            <td style="border: 3px solid #2F323A; padding-top: 15px; color: #2F323A;"><?php echo $row['LRN']; ?></td>
                            <td style="border: 3px solid #2F323A; padding-top: 15px; color: #2F323A;"><?php echo $row['FirstName']; ?></td>
                            <td style="border: 3px solid #2F323A; padding-top: 15px; color: #2F323A;"><?php echo $row['LastName']; ?></td>
                            <td style="border: 3px solid #2F323A; padding-top: 15px; color: #2F323A;"><?php echo $row['Section']; ?></td>
                            <td style="border: 3px solid #2F323A; padding-top: 15px; color: #2F323A;"><?php echo $row['GradeLevel']; ?></td>
                            </tr>
                            <?php
                        } 
                              ?>
                        </tbody>
                        </table>
                         
                         
                    </div>
                

如何以单独的方式获取单个数据?我得到的错误是“未定义的变量:行”,您可以在查询中看到。我不知道为什么它说我只制作表格只是为了看看我是否可以从单个学生那里获得实际数据。我希望我的问题可以理解。

【问题讨论】:

  • 我可以从您的代码中看到变量 $row 已定义
  • 我可以看看你的数据库表吗?
  • 废话我忘了说这段代码是打印的输出。
  • @NazmulHossainPappu 是的,我会编辑我的帖子。
  • 第一个表中会有类似onclick="window.open('IndivPrintTableStudent.php?LRN=&lt;?PHP echo $row["LRN"]; ?&gt;').print(); 的内容。然后第二个脚本将使用$_GET["LRN"] 从 URL 中检索该值并在 sql 查询中使用它。 (但要小心 SQL 注入,学习如何使用参数和准备好的语句来保护您的数据库)

标签: php html css printing


【解决方案1】:

您的按钮链接到 IndivPrintTableStudent.php,但您没有在 URL 中传递被点击行的 ID,因此该脚本不知道您想要显示哪一行。第二个脚本中的$row['LRN'] 将不存在,因为它是不同脚本的一部分,变量不会自动从一个脚本传递到另一个脚本。您必须在 URL 中传递它们。

你会有类似的东西

onclick="window.open('IndivPrintTableStudent.php?LRN=&lt;?php echo $row["LRN"]; ?&gt;').print();

在第一个表的打印按钮中。

然后第二个脚本将使用$_GET["LRN"] 从 URL 中检索该值并在 sql 查询中使用它。

(但请注意 SQL 注入,了解如何使用参数和准备好的语句来保护您的数据库 - 示例请参见 https://phpdelusions.net/mysqli。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 2018-09-06
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多