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

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

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 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'])) {
    
    点赞 评论 复制链接分享
  • dsdsds12222 dsdsds12222 2016-07-13 07:00

    Use this its work 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>
    
    点赞 评论 复制链接分享
  • doudu3961 doudu3961 2016-07-13 07:00

    try this for connect database

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

    may be thats why you got a error

    点赞 评论 复制链接分享
  • du9757 du9757 2016-07-13 07:01

    Simply include while loop into if statement in order to be executed only when you submit the form:

    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>";
        }
    }
    
    点赞 评论 复制链接分享
  • dongzhong6675 dongzhong6675 2016-07-13 07:04

    You are passing null value to the mysqli_fetch_array() function.because the value of $data variable can not be identified at the function.make

    $data="" ;
    

    at the beginning of the php code.

    try this code

    <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>
    
    点赞 评论 复制链接分享
  • douke1942 douke1942 2016-07-13 07:39

    After you showed us the "submit" button, the below code should work on 'Run Report' button is pressed:

    This is the complete solution to your question:

    <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>
    
    点赞 评论 复制链接分享
  • doumei8126 doumei8126 2016-07-13 07:58
    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>
    
    点赞 评论 复制链接分享

相关推荐