PHP Query problem
UPDATE
Maybe I am just a dummy and can't see my mistake. Basically this is function is handling the math behind everything else. It has multiple queries and updates and inserts in two different tables..
When I try to process it, it gives me:
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/content/53/73113开发者_开发技巧53/html/gs/cca/accounts/include/processAct.php on line 241
Here's my function:
function calculateBilling(){
$date = date('mdY');
$bid = mysql_real_escape_string($_POST['bid']);
$account = mysql_real_escape_string($_POST['account']);
$timein = mysql_real_escape_string($_POST['timein']);
$desc = mysql_real_escape_string($_POST['desc']);
$hrs2calc1 = mysql_real_escape_string($_POST['hrly']);
$hrs2calc2 = mysql_real_escape_string($_POST['rhrly']);
$query = 'SELECT bid, account, hrly, rhrly, bal FROM billing WHERE bid='.$bid;
$result = mysql_query($query);
HERES LINE 241 ----> while($row = mysql_fetch_row($result)){
$accounttobebilled = $row[1];
$hrly = $row[2];
$rhrly = $row[3];
$curbal = $row[4];
}
$sub1 = $hrly * $hrs2calc1;
$sub2 = $rhrly * $hrs2calc2;
$subtotal = $sub1 + $sub2;
$total = $curbal + $subtotal;
$query2 = 'UPDATE billing SET bal = '.$total.' WHERE bid ='.$bid;
$result2 = mysql_query($query2);
// Update Billing Log for this customer
mysql_query("INSERT INTO billingLog (bid, date, hrsOnsite, hrsRemote, timein, descript, total) VALUES ('$bid', '$date', '$hrs2calc1', '$hrs2calc2', '$timein', '$desc', '$subtotal')");
}
I think the problem is coming from my select (drop down) where it posts to the script:
<select class="form-dropdown validate[required]" style="width:150px" id="input_5" name="account">
<?php
while($row =
mysql_fetch_row($result)){
$bid =$row[0];
$account = $row[1];
echo '<option value="'.$bid.'">'.$account.'</option>';
}
?>
</select>
For James:
SELECT bid, account, hrly, rhrly, bal FROM billing WHERE bid=You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/content/53/7311353/html/gs/cca/accounts/include/processAct.php on line 243
UPDATE billing SET bal = 0 WHERE bid =You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1INSERT INTO billingLog (bid, date, hrsOnsite, hrsRemote, timein, descript, total) VALUES ('', '07292011', '2', '2', '2', '2', '0')
If you use this instead, what output do you get:
function calculateBilling(){
$date = date('mdY');
$bid = mysql_real_escape_string($_POST['bid']);
$account = mysql_real_escape_string($_POST['account']);
$timein = mysql_real_escape_string($_POST['timein']);
$desc = mysql_real_escape_string($_POST['desc']);
$hrs2calc1 = mysql_real_escape_string($_POST['hrly']);
$hrs2calc2 = mysql_real_escape_string($_POST['rhrly']);
$query = 'SELECT bid, account, hrly, rhrly, bal FROM billing WHERE bid='.$bid;
echo $query;
$result = mysql_query($query);
echo mysql_error();
while($row = mysql_fetch_row($result)){
$accounttobebilled = $row[1];
$hrly = $row[2];
$rhrly = $row[3];
$curbal = $row[4];
}
$sub1 = $hrly * $hrs2calc1;
$sub2 = $rhrly * $hrs2calc2;
$subtotal = $sub1 + $sub2;
$total = $curbal + $subtotal;
$query2 = 'UPDATE billing SET bal = '.$total.' WHERE bid ='.$bid;
echo $query2;
$result2 = mysql_query($query2);
echo mysql_error();
// Update Billing Log for this customer
$query3 = "INSERT INTO billingLog (bid, date, hrsOnsite, hrsRemote, timein, descript, total) VALUES ('$bid', '$date', '$hrs2calc1', '$hrs2calc2', '$timein', '$desc', '$subtotal')";
echo $query3;
mysql_query($query3);
echo mysql_error();
}
It's your concatenation.
Change
$query = 'SELECT bid, account, hrly, rhrly, bal FROM billing WHERE bid='.$bid.'';
to
$query = 'SELECT bid, account, hrly, rhrly, bal FROM billing WHERE bid='.$bid;
I'm also assuming that bid
is an integer. Otherwise you need quotes:
$query = 'SELECT bid, account, hrly, rhrly, bal FROM billing WHERE bid="'.$bid.'"';
This is wrong too
mysql_query("UPDATE billing SET bal = '$total' WHERE bid ='.$bid.'");
should be something like
mysql_query("UPDATE billing SET bal = '{$total}' WHERE bid ='{$bid}'");
-- or full concatenation
mysql_query("UPDATE billing SET bal = '" . $total . "' WHERE bid ='" . $bid . "'");
Same goes for you last query.
With the information provided, it's kinda hard to figure out what the problem is. Your best solution is outputting mysql_error() right after you run the query.
$result = mysql_query($query);
echo mysql_error();
Unless you have incorrectly specify table name or field name, the value on your SELECT
statement, should be wrapped with proper quoted.
To me, it seems helpful to check the result of generating the SQL query string from php eg. echo $query
(that should show the presumed error in the first query).
If reading the string does not spot the errors, feeding it via mysql into a test db might help a lot, especially. Mixing sql, php, single and double quotes is not always easy write nor read ...
精彩评论