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

报告相同问题?

悬赏问题

  • ¥15 求高通平台Softsim调试经验
  • ¥15 canal如何实现将mysql多张表(月表)采集入库到目标表中(一张表)?
  • ¥15 wpf ScrollViewer实现冻结左侧宽度w范围内的视图
  • ¥15 栅极驱动低侧烧毁MOSFET
  • ¥30 写segy数据时出错3
  • ¥100 linux下qt运行QCefView demo报错
  • ¥50 F1C100S下的红外解码IR_RX驱动问题
  • ¥20 基于matlab的航迹融合 航迹关联 航迹插补
  • ¥15 用Matlab实现图中的光线追迹
  • ¥15 联想笔记本开机出现系统更新界面