Php undefine变量问题

时间:2012-06-04 09:21:00

标签: php mysql

我有一个html表单,它是将数据插入mysql数据库,然后使用以下php代码获取这些数据(来自 supplier_jv 表)

<?php
include("include/address2.php");
include("include/menu.php");
$uname_ad = $_SESSION['uname_ad'];  
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM supplier_jv WHERE jv_id = '$id'");        
$num = mysql_num_rows($sql);

if($num == 0)
{
    echo "<p><font color=red>Accounts is emtpy</font></p>";
}
else
{
    $re_name = mysql_fetch_array($sql);
    echo "<center><h2>";
    echo "<strong>Accounts of </strong>";
    echo $re_name['jv_name'];
    echo "</h2></center>";

    echo "<center>";
    echo "<table>";
    echo "<table border='0' cellpadding='5' cellspacing='5' width='1000'>";
    echo "<tr/>";                   
    echo "<td><strong>Date</strong></td>";
    echo "<td><strong>Particular</strong></td>";
    echo "<td><strong>Folio(C)</strong></td>";
    echo "<td><strong>Folio(J)</strong></td>";
    echo "<td><strong>Debit</strong></td>";
    echo "<td><strong>Credit</strong></td>";
    echo "<td><strong>Balance</strong></td>";                   
    echo "</tr>";
    while($re= mysql_fetch_array($sql))
    {
        $day = $re['day'];
        $month $re['month'];
        $year = $re['year'];                    
        $parti = $re['particulars'];                    
        $folio = $re['folio'];
        $folio2 = $re['folio2'];
        $debit = $re['debit'];
        $credit = $re['credit'];
        $balance = $re['balance'];
        $b = $debit - $credit;

        $total_debit = mysql_query("SELECT SUM(debit) FROM supplier_jv");
        $re_t = mysql_fetch_array($total_debit);
        $t_d =  $re_t['SUM(debit)'];                

        $total_credit = mysql_query("SELECT SUM(credit) FROM supplier_jv");
        $re_t2 = mysql_fetch_array($total_credit);
        $t_c =  $re_t2['SUM(credit)'];                  

        $b = $t_d - $t_c;               
        echo "<tr>";        
        echo "<td>$day/$month/$year</td>";
        echo "<td>$parti</td>";
        echo "<td>$folio</td>";
        echo "<td>$folio2</td>";
        echo "<td>";
        echo number_format($debit);
        echo "</td>";                       
        echo "<td>";
        echo number_format($credit);
        echo "</td>";                       
        echo "<td></td>";                       
        echo "</tr>";
    }
    echo "<tr bgcolor='#f3f3f3'>";      
    echo "<td></td>";
    echo "<td></td>";
    echo "<td></td>";
    echo "<td></td>";
    echo "<td></td>";                       
    echo "<td><strong>Total Balance-</strong></td>;       
    echo "<td><strong>";                    
    echo number_format($b);
    echo "</strong></td>";                      
    echo "</tr>";
    echo "</table>";
    echo "</center>";                       
}           
?>  

好吧,插入数据后,显示:

Notice: Undefined variable: b in E:\xampp\htdocs\Accounts\admin\content
\supplier_account.php on line 108

但是,如果我第二次插入数据,那就没关系!! 任何想法或解决方案。
由于
shibbir。

1 个答案:

答案 0 :(得分:2)

您需要使用isset来查看它是否已设置,而不是null并且还可以避免Notice: Undefined variable错误:

if (isset($b)) 
{
  // your code here
}

你在哪里:

$b = $t_d - $t_c;   

确保$t_d$t_c

有一些价值
相关问题