简体   繁体   中英

Php undefine variable issue

I've a html form which is insert data to mysql database and then get those data with following php code (From supplier_jv table)

<?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>";                       
}           
?>  

Well, After insert data then it's show:

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

But if i insert data in second time then it's OK!!.
Any idea or solution.
Thanks
shibbir.

You need to use isset to see if it is set, not null and also avoid the Notice: Undefined variable error:

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

Where you have:

$b = $t_d - $t_c;   

Make sure that there is some value coming up for $t_d and $t_c

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM