Forum Moderators: coopster

Message Too Old, No Replies

not getting value from table

         

ckdoublenecks

11:51 pm on Apr 27, 2011 (gmt 0)

10+ Year Member



I'm trying to read the taxrate from the database table and use it with values from another table. the database and table are correct as is the field (taxrate) from the table, value is 0.06. Then I'm trying to multiply that value by the value of a field from another table (charges) then update the table . since it doesn't update, I tried echoing the two values but 0 is displayed for both. Will someone advise me?
<?php
mysql_connect("localhost","root","");
mysql_select_db(numbersdb) or die( "Unable to select database");
$query = "SELECT taxrate FROM numbdata ";
mysql_fetch_assoc(mysql_query($query));
$result=mysql_query($query);
// include("getpercent.php");
$taxrate = $_POST['taxrate'];
echo "taxrate ".$data['taxrate'];

mysql_connect(localhost,root,"");
mysql_select_db(oodb) or die( "Unable to select database");
$query = "SELECT id, tax,charges,datediff(curdate(),duedate) AS dayslate FROM oocust WHERE pd = ' '";
mysql_fetch_assoc(mysql_query($query));
$result=mysql_query($query);
while($row=mysql_fetch_array($result))
{
$id=$row['id'];
$amtdue = $row['amtdue'];
$shipamt = $row['shipamt'];
$charges = $row['charges'];
$tax = $charges * $taxrate;
$amtdue = $charges + $tax + $shipamt;
echo "tax is $tax <br /><br />";
$days_late = ($row['dayslate'] > 0)?$row['dayslate'] : 0;
$sql = "UPDATE oocust SET tax = " . $tax . ", amtdue = " . $amtdue . ", dayslate = " . $days_late . " WHERE
id='$id'";
mysql_query($sql) ;
$err=mysql_error();
if($err!="")
{
echo "Error in $sql: $err\n";
}
}
echo "Invoice Prep completed";
?>

coopster

2:59 pm on Apr 28, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm uncertain as to which part you are referring but you may need to start right off the top here. I don't know what you are doing here, but this is what it appears you may be wanting to do...
<?php 
mysql_connect("localhost","root","");
mysql_select_db('numbersdb') or die( "Unable to select database");
$query = "SELECT taxrate FROM numbdata";
$data = mysql_fetch_assoc(mysql_query($query));
$taxrate = $data['taxrate'];
echo "taxrate ".$data['taxrate'];

ckdoublenecks

4:41 pm on Apr 28, 2011 (gmt 0)

10+ Year Member



that part is working now with the below code but I get this message:
taxrate 0.06Warning: mysql_fetch_array() expects parameter 1 to be resource, array given in C:\xampp\htdocs\invoice\invcalc.php on line 10
Invoice Prep completed

<?php 
mysql_connect("localhost", "root", "");
mysql_select_db(numbersdb) or die("Unable to select database");
$query = "SELECT taxrate FROM numbdata ";
$result = mysql_fetch_assoc(mysql_query($query));
$taxrate = $result['taxrate'];
[b]echo "taxrate " . $taxrate; [/b]
$query = "SELECT id, tax,charges,datediff(curdate(),duedate) AS dayslate FROM oocust WHERE pd = ' '";
$stat = @mysql_fetch_assoc(mysql_query($query));
while ($row = mysql_fetch_array($result))
{
$id = $row['id'];
$amtdue = $row['amtdue'];
$shipamt = $row['shipamt'];
$charges = $row['charges'];
$tax = $charges * $taxrate;
$amtdue = $charges + $tax + $shipamt;
[b]echo "tax is $tax <br /><br />"; [/b]$days_late = ($row['dayslate'] > 0) ? $row['dayslate'] : 0;
$sql = "UPDATE oocust SET tax = " . $tax . ", amtdue = " . $amtdue . ", dayslate = " . $days_late . " WHERE id='$id'";
mysql_query($sql);
$err = mysql_error(); if ($err != "")
{
echo "Error in $sql: $err\n";
}
}
echo "Invoice Prep completed";
?>

coopster

1:59 pm on May 2, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You need to read more about how to loop through results sets. You are trying to use the resource from the first query again, not your second query. Additionally, I think you may want to start off breaking your functions into separate lines until you have a good grip on what each is doing. So, build your query statement, execute the query and then loop through the fetched results. You will likely want to bookmark the PHP MySQL Functions [php.net] page for awhile.