I need coding help

In the below code, I’m trying to access the value “balance”
from the ‘numbersdb’ database, use it in the calcs, and use
the accumulated “balance” to update the database. I could
use some help. Thanks

<?php mysql_connect("localhost","root",""); mysql_select_db('numbersdb') or die( "Unable to select database"); $query=" SELECT balance FROM numbdata where id='id'"; $result=mysql_query($query); $num=mysql_num_rows($result); $balance = $_POST['balance']; mysql_connect("localhost","root",""); mysql_select_db('ckdb') or die( "Unable to select database"); $query=" SELECT * FROM ckcust ORDER BY dateord ASC"; $result=mysql_query($query); $num=mysql_num_rows($result); error_reporting(0); echo date("m/d/Y") . "
"; echo ""; while($row = mysql_fetch_array($result)) { $owed = $row['charges'] - $row['paidamt']; $nubalance = $nubalance - $row['paidamt']; $totpaid = $totpaid + $row['paidamt']; $totcharges = $totcharges + $row['charges']; $totowed = $totowed + $owed; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo "
Inv# description date charges paid owed bank balance
" . sprintf("%.2f",$balance) . "
=======================================================================
" . $row['invnum'] . "" . $row['bname'] . "" . $row['dateord'] . "" . $row['charges'] . "" . $row['paidamt'] . "" . sprintf("%.2f",$owed) . "" . sprintf("%.2f",$nubalance) . "
=======================================================================
Gtotals" . sprintf("%.2f",$totcharges) . "" . sprintf("%.2f",$totpaid) . "" . sprintf("%.2f",$totowed) . "
"; mysql_query("UPDATE numbdata SET balance='$nubalance'"); mysql_close(); ?>

I’ll be honest, I only glanced over your code…there’s an awful lot of it, after all, and you don’t really specify exactly what your problem is, but I’m going to take a guess:

You use this line of code to get back what I presume to be order data or something:

[php]
while($row = mysql_fetch_array($result))
[/php]

However, further down in your code, you reference the $row variable as an associative array, when it is in fact a scalar, (numeric keys), one:

[php]
echo “

”;
echo “” . $row[‘invnum’] . “”;
echo “” . $row[‘bname’] . “”;
echo “” . $row[‘dateord’] . “”;
[/php]

To get the results you’re expecting, you need to fetch an associative array out of the database. Change this:

[php]
while($row = mysql_fetch_array($result))
[/php]

To this:

[php]
while($row = mysql_fetch_assoc($result))
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service