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

时间:2016-07-13 06:46:01

标签: 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>

错误是

  

警告:mysqli_fetch_array()期望参数1为mysqli_result,   /home/cshrnaf/public_html/MIS_CSHRN/reporttest.php中给出的null   第265行

265 line:

 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>
如果要加载页面,将加载

数据

7 个答案:

答案 0 :(得分:1)

如果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'])) {

答案 1 :(得分:0)

尝试连接数据库

$mysqli = mysqli_connect( 'localhost', 'username', 'pass', 'mis_db' );

可能就是为什么你出错了

答案 2 :(得分:0)

只需将while循环包含在if语句中,以便仅在提交表单时执行:

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

答案 3 :(得分:0)

您正在将null值传递给mysqli_fetch_array()函数。因为无法在function.make

中识别$ data变量的值
$data="" ;

在PHP代码的开头。

试试这段代码

<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
                $data="";
                $mysqli = new mysqli('localhost', 'username', 'pass', 'mis_db');

                if (mysqli_connect_error()) {
                    die('Connect Error (' . mysqli_connect_errno() . ') '
                        . mysqli_connect_error());
                }
                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>

答案 4 :(得分:0)

向您展示&#34;提交&#34;按钮,下面的代码应该用于&#39; 运行报告&#39;按下按钮:

这是您问题的完整解决方案:

<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>
                            <form action="" method="post">
                                <button class="btn btn-primary" type="submit">Run Report</button><input type="hidden" name="submit" value="true"/>
                            </form>
                        </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', 'cshrnaf_user2', '=cXlIBsdMkdr', 'cshrnaf_mis_db');


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

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

                                $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>
    </div>
</div>

答案 5 :(得分:0)

you called ($row = mysqli_fetch_array($data))
using       ($row = mysqli_fetch_assoc($data))

   this code i used to test for tou with dreamweaver ADOBE
        its work fine
    <?php require_once('Connections/test.php'); ?>
    <?php
    mysql_select_db($database_test, $test);
    $query_table = "SELECT * FROM `table`";
    $table = mysql_query($query_table, $test) or die(mysql_error());
    $row_table = mysql_fetch_assoc($table);
    $totalRows_table = mysql_num_rows($table);
    ?>
    <html>
    <head>
        <title>test</title>
    </head>
    <body>
        <table width="200" border="1">
      <tbody>
        <tr>
          <?php do { ?>
      <th scope="col"><?php echo $row_table['col1']; ?></th>
            <th scope="col"><?php echo $row_table['col2']; ?></th>
            <th scope="col"><?php echo $row_table['col3']; ?></th>
            <th scope="col"><?php echo $row_table['col4']; ?></th>
            <th scope="col"><?php echo $row_table['col5']; ?></th>
            <th scope="col"><?php echo $row_table['col6']; ?></th>
            <th scope="col"><?php echo $row_table['col7']; ?></th>
            <th scope="col"><?php echo $row_table['col8']; ?></th>
            <th scope="col"><?php echo $row_table['col9']; ?></th>
            <th scope="col"><?php echo $row_table['col10']; ?></th></tr><br>
          <?php } while ($row_table = mysql_fetch_assoc($table)); ?>
        </tr>
      </tbody>
    </table>

    </body>
    </html>
    <?php
    mysql_free_result($table);
    ?>
     //OUTPUT

    <html>
    <head>
        <title>test</title>
    </head>
    <body>
        <table width="200" border="1">
      <tbody>
        <tr>
            <th scope="col">insert1col1</th>
            <th scope="col">insert1col2</th>
            <th scope="col">insert1col3</th>
            <th scope="col">insert1col4</th>
            <th scope="col">insert1col5</th>
            <th scope="col">insert1col6</th>
            <th scope="col">insert1col7</th>
            <th scope="col">insert1col8</th>
            <th scope="col">insert1col9</th>
            <th scope="col">insert1col10</th></tr><br>
            <th scope="col">insert2col1</th>
            <th scope="col">insert2col2</th>
            <th scope="col">insert2col3</th>
            <th scope="col">insert2col4</th>
            <th scope="col">insert2col5</th>
            <th scope="col">insert2col6</th>
            <th scope="col">insert2col7</th>
            <th scope="col">insert2col8</th>
            <th scope="col">insert2col9</th>
            <th scope="col">insert2col10</th></tr><br>
              </tr>
      </tbody>
    </table>

    </body>
    </html>

答案 6 :(得分:-1)

使用此作品Test link

<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 = mysqli_connect( 'localhost', 'username', 'pass', 'mis_db');

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>