php - 单击提交按钮时从数据库中选择数据

标签 php html mysql

我有一个表,当我想单击一个按钮时,它将数据从数据库获取到表,我尝试此代码,但出现错误。

    <div class="wrapper wrapper-content animated fadeInRight">
            <div class="row">
                <div class="col-lg-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>Conference Table</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="dropdown-toggle" data-toggle="dropdown" href="table_data_tables.php#">
                                <i class="fa fa-wrench"></i>
                            </a>
                            <ul class="dropdown-menu dropdown-user">
                                <li><a href="table_data_tables.php#">Config option 1</a>
                                </li>
                                <li><a href="table_data_tables.php#">Config option 2</a>
                                </li>
                            </ul>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>

                    <div class="ibox-content">
                                                    <div class="form-group">
                                    <div class="col-sm-4 col-sm-offset-5">
                                        <button class="btn btn-white" type="submit">Cancel</button>
                                        <button class="btn btn-primary" type="submit">Run Report</button>
                                    </div>
                                </div>
                        <table class="table table-striped table-bordered table-hover dataTables-example" >
                            <thead>
                                <tr>
                                    <th>No</th>
                                    <th>Aim</th>
                                    <th>Date</th>
                                    <th>Funded</th>
                                    <th>Male</th>
                                    <th>Female</th>
                                    <th>Disabled</th>
                                    <th>Total</th>
                                    <th>Comments</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php
$mysqli = new mysqli( 'localhost', 'user2', 'password', 'database' );


if (mysqli_connect_error()) {
    echo mysqli_connect_error();
    exit();
} 

           if (isset($_POST['submit'])) {
                $query = 'SELECT * FROM conference';
                $data = mysqli_query($mysqli, $query) ;

                if (!$data) {
                    echo("Error description: " . mysqli_error($mysqli));
                } else {

                    while ($row = mysqli_fetch_array($data)) {
                        echo "<tr>
                            <td>" . $row['NOTW'] . "</td>
                            <td>" . $row['Aim'] . "</td>
                            <td>" . $row['date'] . "</td>
                            <td>" . $row['Funded'] . "</td>
                            <td>" . $row['Male'] . "</td>
                            <td>" . $row['Female'] . "</td>
                            <td>" . $row['Disabled'] . "</td>
                            <td>" . $row['Total'] . "</td>
                            <td>" . $row['Comments'] . "</td>
                            <td>   Edit   Trush  </td>                                           
                          </tr>";
                     }
                 }
            }
                    ?>
                            </tbody>
                            <tfoot>
                            </tfoot>
                        </table>
                    </div>
            </div>
            </div>

错误是

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/cshrnaf/public_html/MIS_CSHRN/reporttest.php on line 265

265行:

 while($row = mysqli_fetch_array($data))

更新

将代码更改为:

<div class="ibox-content">
                        <table class="table table-striped table-bordered table-hover dataTables-example" >
                            <thead>
                                <tr>
                                    <th>No</th>
                                    <th>Aim</th>
                                    <th>Date</th>
                                    <th>Funded</th>
                                    <th>Male</th>
                                    <th>Female</th>
                                    <th>Disabled</th>
                                    <th>Total</th>
                                    <th>Comments</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php
$mysqli = new mysqli( 'localhost', 'user2', 'password', 'database' );


if (mysqli_connect_error()) {
    echo mysqli_connect_error();
    exit();
} 
/*
if(isset($_POST['submit']))
{
    */
$query = 'SELECT * FROM conference';
$data = mysqli_query($mysqli, $query);


                    while($row = mysqli_fetch_array($data)) 
                    {
                        echo "  <tr>
                                    <td>" . $row['NOTW'] . "</td>
                            <td>" . $row['Aim'] . "</td>
                            <td>" . $row['date'] . "</td>
                            <td>" . $row['Funded'] . "</td>
                            <td>" . $row['Male'] . "</td>
                            <td>" . $row['Female'] . "</td>
                            <td>" . $row['Disabled'] . "</td>
                            <td>" . $row['Total'] . "</td>
                            <td>" . $row['Comments'] . "</td>
                            <td>   Edit   Trush  </td>  

                                </tr>";
                    }
//}
                    ?>
                            </tbody>
                            <tfoot>
                            </tfoot>
                        </table>
                    </div>
            </div>
            </div>

如果页面加载,数据也会被加载

最佳答案

如果 isset($_POST['submit']) 返回 false,则永远不会执行查询,并且 $data 为 null。但 while 循环会被执行。因此,将您的代码更改为:

           if (isset($_POST['submit'])) {
                $query = 'SELECT * FROM conference';
                $data = mysqli_query($mysqli, $query) ;

                if (!$data) {
                    echo("Error description: " . mysqli_error($mysqli));
                } else {

                    while ($row = mysqli_fetch_array($data)) {
                        echo "<tr>
                            <td>" . $row['NOTW'] . "</td>
                            <td>" . $row['Aim'] . "</td>
                            <td>" . $row['date'] . "</td>
                            <td>" . $row['Funded'] . "</td>
                            <td>" . $row['Male'] . "</td>
                            <td>" . $row['Female'] . "</td>
                            <td>" . $row['Disabled'] . "</td>
                            <td>" . $row['Total'] . "</td>
                            <td>" . $row['Comments'] . "</td>
                            <td>   Edit   Trush  </td>                                           
                          </tr>";
                     }
                 }
            }

更新

在 $_POST 中使用名称。不是类型,所以更改:

<button class="btn btn-primary" type="submit">Run Report</button>

<button class="btn btn-primary" type="submit" name="Report">Run Report</button>

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

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

关于php - 单击提交按钮时从数据库中选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38344415/

相关文章:

php - Mysql 将数据存储为数组

php - laravel 资源在获取数据时更改 id

php - Codeigniter 3 - 我可以从 3 张图片中获取图片上传的 url 吗?

mysql命令行选项

java - ucanaccess 系统重载,该怎么办?

php - 如何在 laravel 范围内查询关系

php - 如何将事件与jquery中通过ajax生成的动态值绑定(bind)

html - Google 可视化表中一般 'Formatters' 的 CSS 属性

html - CSS:如果兄弟输入没有值,则选择标签并更改颜色

javascript - Angular 中选择框的默认值 "placeholder"?