Page 1 of 1

Mysql Update

Posted: Sat Oct 17, 2009 4:17 am
by geryatric
I am developing a script which will take a number from the url divide it by the number of members in a table add it with the balance already on the table and save the total. Then update the table with the new total.
All is working fine only the table wont update, could some one advise me on the update query.

Code: Select all

if(isset($_POST['$lotto_id'])){$lotto_id = $_POST['$lotto_id'];}
if(isset($_POST['$income'])){$income = $_POST['$income'];}
 
$query = "SELECT lotto_id, balance FROM lotto_member WHERE lotto_id =$lotto_id";
 
$result = mysql_query($query) or die;
$num_res = mysql_num_rows($result);
$num_rows = mysql_num_rows($result);
for ($i=0; $i<$num_res; $i++){
$row = mysql_fetch_array($result);
 
// existing Balance in table
$balance = $row["balance"];
 
// Divide the new amount by the number of members
$split = $income / $num_rows;
 
// add the two together
$member_total = $split + $balance ;
 
// test it
echo "$member_total <br>";
}
// update the DB
$query = "UPDATE lotto_member SET balance='$member_total' WHERE lotto_id =$lotto_id";
Thank you

Re: Mysql Update

Posted: Sat Oct 17, 2009 1:08 pm
by requinix
Can't help but notice that the UPDATE is using $balance as the new value...

Code: Select all

// add the two together
$member_total = $split + $balance ;

Re: Mysql Update

Posted: Sat Oct 17, 2009 1:31 pm
by geryatric
Thanks for the reply tasairis
That was a typo and I just corrected it .
having played around with it during the the day I think the problem is in the
mysql_fetch_array($result);
any thoughts ?