在哪里添加if else语句。并修复一个错误

时间:2015-04-09 06:02:40

标签: php if-statement mysqli

此代码生成特定时间的销售报告。很遗憾,它有错误。注意:未定义的变量:第175行的C:\ xampp \ htdocs \ ecommerce \ admin_area \ view_sales.php中的总数

我还想添加if else语句,如果我或该语句中没有记录,它将回显“在此时间范围内找不到任何交易!”

 <form method=post name=f1 action=''>
 <table border="0" cellspacing="0" >
 Generate Report  
 <tr><td  align=left  >  

 From:</td>
 <td  align=left  > 
 <select name="month" value="Select Month"></option>
 <option value='01'>January</option>
 <option value='02'>February</option>
 <option value='03'>March</option>
 <option value='04'>April</option>
 <option value='05'>May</option>
 <option value='06'>June</option>
 <option value='07'>July</option>
 <option value='08'>August</option>
 <option value='09'>September</option>
 <option value='10'>October</option>
 <option value='11'>November</option>
 <option value='12'>December</option>
 </select>
 </td>
 <td  align=left  >   
 Date:
 <select name="day" >
 <option value='01'>01</option>
 <option value='02'>02</option>
 <option value='03'>03</option>
 <option value='04'>04</option>
 <option value='05'>05</option>
 <option value='06'>06</option>
 <option value='07'>07</option>
 <option value='08'>08</option>
 <option value='09'>09</option>
 <option value='10'>10</option>
 <option value='11'>11</option>
 <option value='12'>12</option>
 <option value='13'>13</option>
 <option value='14'>14</option>
 <option value='15'>15</option>
 <option value='16'>16</option>
 <option value='17'>17</option>
 <option value='18'>18</option>
 <option value='19'>19</option>
 <option value='20'>20</option>
 <option value='21'>21</option>
 <option value='22'>22</option>
 <option value='23'>23</option>
 <option value='24'>24</option>
 <option value='25'>25</option>
 <option value='26'>26</option>
 <option value='27'>27</option>
 <option value='28'>28</option>
 <option value='29'>29</option>
 <option value='30'>30</option>
 <option value='31'>31</option>
 </select>
 </td>
 <td>
 Year(yyyy)<input type=text name="year" size=4 value=2015>
 </td>

 </tr>
 <tr>

 <td  align=left  > 


 To :
 </td>
 <td  align=left  > 
 <select name="month2" value=''>Select Month</option>
 <option value='01'>January</option>
 <option value='02'>February</option>
 <option value='03'>March</option>
 <option value='04'>April</option>
 <option value='05'>May</option>
 <option value='06'>June</option>
 <option value='07'>July</option>
 <option value='08'>August</option>
 <option value='09'>September</option>
 <option value='10'>October</option>
 <option value='11'>November</option>
 <option value='12'>December</option>
 </select>

 </td><td  align=left  >   
 Date :<select name="day2" >
 <option value='01'>01</option>
 <option value='02'>02</option>
 <option value='03'>03</option>
 <option value='04'>04</option>
 <option value='05'>05</option>
 <option value='06'>06</option>
 <option value='07'>07</option>
 <option value='08'>08</option>
 <option value='09'>09</option>
 <option value='10'>10</option>
 <option value='11'>11</option>
 <option value='12'>12</option>
 <option value='13'>13</option>
 <option value='14'>14</option>
 <option value='15'>15</option>
 <option value='16'>16</option>
 <option value='17'>17</option>
 <option value='18'>18</option>
 <option value='19'>19</option>
 <option value='20'>20</option>
 <option value='21'>21</option>
 <option value='22'>22</option>
 <option value='23'>23</option>
 <option value='24'>24</option>
 <option value='25'>25</option>
 <option value='26'>26</option>
 <option value='27'>27</option>
 <option value='28'>28</option>
 <option value='29'>29</option>
 <option value='30'>30</option>
 <option value='31'>31</option>
 </select>


 </td><td  align=left  >   
 Year(yyyy)<input type=text name="year2" size=4 value=2015>
 </td>
 </tr>
 <tr>
 <td colspan="2">
 <input type="submit" name="gen_report" value="GENERATE REPORT">
 </td>
 </tr>
 </table>


 </form>
 <table width="739" style="border-width:2px; border-style:dotted;">
 <?Php

 include("includes/db.php");
 $i = 0 ;
 if(isset($_POST['gen_report'])){
 $month=$_POST['month'];
 $day=$_POST['day'];
 $year=$_POST['year'];
 $month2=$_POST['month2'];
 $day2=$_POST['day2'];
 $year2=$_POST['year2'];
 $date_value="$year-$month-$day";
 $date_value2="$year2-$month2-$day2";

 $get_report = "select * from payments 
where payment_date >= '$date_value' AND payment_date < '$date_value2'";
 $run_report = mysqli_query($con, $get_report); 
 echo "
 <tr align=center>
 <td colspan='6' style='background-color:black; color:red;'>SALES REPORT           FROM:   $date_value  to  $date_value2</td>
 </tr>
 <tr>
 <th>Date</th>
 <th>Transaction ID</th>
 <th>Customer Name</th>
 <th>Product</th>
 <th>Amount</th>
 </tr>
 ";

 while ($row_report=mysqli_fetch_array($run_report)){
$report_date = $row_report['payment_date'];
$cust_id = $row_report['customer_id'];
$trx_id = $row_report['trx_id'];
$pro_id = $row_report['product_id'];
$amount = array($row_report['amount']);
$total_amount = array_sum($amount);
$total += $total_amount;

$get_cust = "select * from customers where customer_id ='$cust_id'";
$run_cust = mysqli_query($con, $get_cust);
$row_cust=mysqli_fetch_array($run_cust);
$customer = $row_cust['customer_name'];

$get_pro = "select * from products where product_id ='$pro_id'";
$run_pro = mysqli_query($con, $get_pro);
$row_pro=mysqli_fetch_array($run_pro);
$product = $row_pro['product_title'];
$i++;

?>

            <tr>
            <td><?php echo $report_date ;?></td>
            <td><?php echo $trx_id ;?></td>
            <td><?php echo $customer ;?></td>
            <td width="200"><?php echo $product ;?></td>
            <td style="color:red;"><?php echo $total_amount ;?></td>
            </tr>

<?php  } };?>


 <tr align="right"><th colspan="5">Number of Transactions: <?php echo $i;?>          </th></tr>
 <tr align="right" ><th colspan="5">Total Sales: <?php echo "Php "  . $total;?></th></tr>

 </table>

1 个答案:

答案 0 :(得分:1)

在调用$total之前或尝试回复之前,您永远不会初始化$total += $total_amount;。只需在$total = 0;循环之前添加while