dongyata3336 2016-07-13 06:46
浏览 227
已采纳

单击“提交”按钮,从数据库中选择数据

i have a table when i want to click a button it get data from database to table i try this code but i get error.

    <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>

the Error is

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 line:

 while($row = mysqli_fetch_array($data))

UPDATE

After Change code to:

<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>

data will be loaded if the page will be loaded

  • 写回答

7条回答 默认 最新

  • dongqiulei6805 2016-07-13 06:59
    关注

    If isset($_POST['submit']) Returns false the query is never executed and $data is null. But the while Loop will be executed. So Change your code to:

               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>";
                         }
                     }
                }
    

    UPDATE

    in $_POST the Name is used. not the type, so Change:

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

    to

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

    and

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

    to

           if (isset($_POST['report'])) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?