2016-07-13 173 views
0

我创建了一个报表,其中包含2个下拉列表和上表中的表格。我想过滤数据并使用下拉列表选项生成报告。第一个下拉列表包含来自数据库的所有表名,第二个下拉列表具有“项目”列,每个表具有相同的项目值。就像我们从表3和项目选项1做出报告一样。我们应该做出报告。从php和mysql数据库中获取数据(创建报告)

这里是我的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

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

正如你所看到的,我删除了下拉菜单中每个项目的if语句,不需要。您的$query中也有语法错误,但连接时不需要.,因此我已将它们删除。最后,$query中的$stck1变量仅更改为$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>"; 
     } 
    } 
} 
?> 
+0

它给出了这样的错误:错误描述:在“where子句中” –

+0

是什么,你的数据库表的样子未知列“物品”? – pmahomme

+0

对不起,我没有?喜欢? –