Farhad paikan Farhad paikan - 4 months ago 13
MySQL Question

Select data from database when i click the submit button

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

Answer

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