用php和mysql从数据库中获取数据(创建报告)

时间:2016-07-13 19:42:03

标签: php html mysql twitter-bootstrap

我创建了一个报表,其中包含2个下拉列表和表格。我想过滤数据并使用下拉列表选项生成报告。第一个下拉列表包含数据库中的所有表名,第二个下拉列表具有" item"列,每个表都有相同的项值。就像我们从表3和项目选项1进行报​​告一样。我们应该进行报告。

这是我的HTML代码:

<div class="row">
                <div class="col-lg-12">
                    <div class="ibox float-e-margins">
                        <div class="ibox-title">
                            <h5>Report by Items</h5>
                        </div>
                        <div class="ibox-content">
                            <form method="post" class="form-horizontal">
                                <div class="form-group"><label class="col-sm-4 control-label"Stocks</label>
                               <div class="col-sm-4"><select class="form-control m-b" name="Stock">
                                        <option></option>
                                        <option>Stock</option>
                                        <option>Stock1</option>
                                        <option>Stock2</option>
                                    </select>
                                    </div>
                                </div>
                                <div class="form-group"><label class="col-sm-4 control-label">Items</label>
                               <div class="col-sm-4">
                                    <select class="form-control m-b" name="Items">
                                        <option></option>
                                        <option>Item1</option>
                                        <option>Item2</option>
                                        <option>Item3</option>
                                        <option>Item4</option>
                                    </select>
                                    </div>
                                </div>
                        </div>
                        <div class="col-lg-25">
                          <div class="ibox float-e-margins">    
                        </div>
                        </div>
                                <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" name=submit>Run Report</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>

我尝试的PHP和表代码是:

                    <div class="ibox-content">
                        <table class="table table-striped table-bordered table-hover dataTables-example" >
                            <thead>
                                <tr>
                                    <th>No</th>
                                    <th>Quantity</th>
                                    <th>Date</th>
                                    <th>Sold</th>
                                    <th>Total</th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php
$mysqli = new mysqli( 'localhost', 'user', 'pass', 'mis_db' );

if (mysqli_connect_error()) {
    echo mysqli_connect_error();
    exit();
} 
if($_POST['Stock']=='Stock1')
{
    $stck1 = 'Stock1';
}
if($_POST['Stock']=='Stock2')
{
    $stck1 = 'Stock2';
}
if($_POST['Stock']=='Stock1')
{
    $stck1 = 'Stock3';
}
if($_POST['Items']=='Item1')
{
    $itm = 'Item1';
}
if($_POST['Items']=='Item2')
{
    $itm = 'Item2';
}
if($_POST['Items']=='Item3')
{
    $itm = 'Item3';
}

           if (isset($_POST['submit'])) {
                $query = 'SELECT * FROM .$stck1 where Items=.$itm';
                $data = mysqli_query($mysqli, $query) ;

                if (!$data) {
                    echo("Error description: " . mysqli_error($mysqli));
                } else {

                    while ($row = mysqli_fetch_array($data)) {
                        echo "<tr>
                            <td>" . $row['No'] . "</td>
                            <td>" . $row['Qty'] . "</td>
                            <td>" . $row['date'] . "</td>
                            <td>" . $row['Sold'] . "</td>
                            <td>" . $row['Total'] . "</td>                                        
                          </tr>";
                     }
                 }
            }
                    ?>
                            </tbody>
                            <tfoot>
                            </tfoot>
                        </table>
                    </div>

我对MySQL查询有疑问。

1 个答案:

答案 0 :(得分:1)

你有一些语法错误,你的PHP可以简化很多。

正如您所看到的,我删除了下拉列表中每个项目的if语句,而不是必需的。你的$query也有语法错误,连接不需要.所以我删除了它们。最后,$stck1中的$query变量只更改为$stck

试试这个:

<?php
    $mysqli = new mysqli('localhost', 'user', 'pass', 'mis_db');

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

$stck = $_POST['Stock'];
$itm = $_POST['Items'];

if (isset($_POST['submit'])) {
   $query = "SELECT * FROM $stck WHERE Items = $itm";

   $data = mysqli_query($mysqli, $query) ;

   if (!$data) {
       echo("Error description: " . mysqli_error($mysqli));
   } else {

       while ($row = mysqli_fetch_array($data)) {
           echo "<tr>
                <td>" . $row['No'] . "</td>
                <td>" . $row['Qty'] . "</td>
                <td>" . $row['date'] . "</td>
                <td>" . $row['Sold'] . "</td>
                <td>" . $row['Total'] . "</td>
              </tr>";
       }
   }
}
?>