2013-10-08 40 views
-2

我要加量+ servicecharges + replacecharges + blockcharges + othercharges如何在MySQL的PHP​​添加多行

,但它不能正常工作,請幫我解決這個問題 感謝

$sql="SELECT CONCAT(monthdate,' ','(',drivernamefuel,')') as drivernamefuel,sum(amount) + 'servicecharges' + 'replacecharges' + 'blockcharges' + 'othercharges' AS amount,drivernamefuel FROM $tablename where carno='$c' AND monthdate BETWEEN '$a' AND '$b' GROUP BY monthdate"; 

見ScreenShort 文件#1:http://www.koolfree.com/ImageUpload/uploads/1381252318.jpg

文件#2:http://www.koolfree.com/ImageUpload/uploads/1381228480.jpg

它正顯示出這些錯誤 警告:被零除在indexssds.php上線51

警告:mysql_fetch_array():提供的參數不是在indexssds.php一個有效的MySQL結果資源上線73

這裏是完整代碼請檢查我的更新 感謝...........................

<?php 
include 'config.php'; 
$link=mysql_connect("$hostname","$username","$password")or 
die('Could not connect: ' . mysql_error()); 
mysql_select_db("$dbname",$link); 
function formatMoney($number, $fractional=false) { 
    if ($fractional) { 
     $number = sprintf('%.2f', $number); 
    } 
    while (true) { 
     $replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number); 
     if ($replaced != $number) { 
      $number = $replaced; 
     } else { 
      break; 
     } 
    } 
    return $number; 
}  
$a=$_POST['from']; 
$b=$_POST['to']; 
$c=$_POST['to2']; 
echo " <p align='center' class='style1'>Vehicle No: $c </p>"; 
echo "<span align='center' class='style1'>Report For The Period Of $a to $b </span>"; 

include 'create.php'; 
$sql="SELECT CONCAT(monthdate,' ','(',drivernamefuel,')') as drivernamefuel, 
SUM(ifnull(amount,0) + 
ifnull(servicecharges,0) + 
ifnull(replacecharges,0) + 
ifnull(blockcharges,0) + 
ifnull(othercharges,0)) AS amount, 
drivernamefuel 
FROM $tablename where carno='$c' 
AND monthdate BETWEEN '$a' AND '$b' 
GROUP BY monthdate"; 

$result = mysql_query($sql); 
$graphtitle="For The month of $a to $b";//Graph Title 
$xname="BarChart Report By Vehicle No : $c"; 
$yname='VALUE';//y-axis name 
$img_width=900;//image height 
$img_height=700;//image width 
$margins=70; 
$ymargin=6; 
$count=mysql_affected_rows(); 
$graph_width=$img_width - $margins * 2; 
$graph_height=$img_height - $margins * 2; 
$bar_width=25; 
$total_bars=$count; 
$gap= ($graph_width- $total_bars * $bar_width)/($total_bars +1); 
$img=imagecreate($img_width,$img_height); 
include 'barcolor.php'; 
imagefilledrectangle($img,0,0,0,0,$bag_color); 
imageline($img,$margins,$img_height-45,$img_width-20,$img_height-45,$xyline_color); 
imageline($img,$margins,$ymargin,$margins,$img_height-45,$xyline_color); 
$maxvalue="select max(monthdate) as amount from $tablename";//Give your field name for Y axis 
$max=mysql_query($maxvalue); 
while($inf1= mysql_fetch_array($max)) 
    { 
    $ratio=$graph_height/$inf1[0]; 
    } 
$horizontal_lines=8; 
$horizontal_gap=($img_height+20)/$horizontal_lines; 
for($j=1;$j<=$horizontal_lines;$j++) 
{ 
     $y=($img_height-48) - $horizontal_gap * $j ; 
     //imageline($img,$margins+1,$y,$img_width-20,$y,$hline_color); 
     $v=intval($horizontal_gap * $j /$ratio); 
     imagestring($img,2,$margins-30,$y-5,$v,$values_color); 
} 
$i=0; 
while($inf = mysql_fetch_array($result)) 
    { 
     $x1=($margins+10) + ($gap+5) + $i * ($gap+$bar_width) ; 
     $x2=$x1+$bar_width; 
     $y1=($img_height-46)- ceil($inf[1] * $ratio) ; 
     $y2=($img_height-46); 
     imagestring($img,2,$x1+1,$y1-15,$inf[1],$values_color); 
     imagestring($img,2,$x2-23,$img_height-43,$inf[0],$values_color); 
     imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color); // Draw bar 
    $i++; 
    } 
imagestring($img,8, ($img_width-$margin)/2, 0, $graphtitle, $txt_color); 
imagestring($img,5, ($img_width-$margin)/2, $img_height-($ymargin+10), $xname, $txt_color); 
imagestringup($img,5,10,($img_height-$ymargin)/2, $yname, $txt_color); 
//header('Content-type: image/png'); 
imagepng($img, 'barchart.jpg'); 
echo "<div style='border:1px solid #d8d8d8;width:$img_width'><img src='barchart.jpg'></div>"; 
?> 

回答

1

你是不是傳遞參數進行查詢,所以像下面那樣傳遞它

並且您還沒有添加
量+ servicecharges + replacecharges + blockcharges + othercharges內選定的查詢的SUM(..),所以一定要更換

修改您的查詢作爲

$sql="SELECT CONCAT(monthdate,' ','(',drivernamefuel,')') as drivernamefuel, 
    sum(amount + servicecharges + replacecharges + blockingcharges + othercharges) AS amount, 
    drivernamefuel 
    FROM ".$tablename." 
    WHERE carno='".$c."' AND monthdate BETWEEN '".$a."' AND '".$b."' 
    GROUP BY monthdate"; 
+0

不靈它顯示錯誤警告:被零除在警告:mysql_fetch_array():提供的參數不是在 – user2468472

+0

參數傳遞喜歡一個有效的MySQL結果資源$ C,修正我的回答 –

+0

是使用PHP – user2468472

0

我希望這會幫助你。在網頁中刪除qotes(')和變化SUM(您想總結領域)

$sql="SELECT CONCAT(monthdate,' ','(',drivernamefuel,')') as drivernamefuel,sum(amount + servicecharges + replacecharges + blockcharges + othercharges) AS total_amount,drivernamefuel FROM ".$tablename." where carno='".$c."' AND monthdate BETWEEN '".$a."' AND '".$b."' GROUP BY monthdate"; 

echo $sql; 

回聲查詢並嘗試直接在phpMyAdmin執行查詢。然後檢查你的查詢有什麼問題。問題可能是值要傳遞到查詢

+0

不工作它顯示錯誤警告:除零警告:mysql_fetch_array():提供的參數不是有效的MySQL結果資源 – user2468472

+0

@ user2468472我有編輯我的答案 – Shafeeque

+0

可以打印sql語句來驗證參數$ a,$ b,$ c是有效的。 – sel

0

試試這個:

$sql="SELECT CONCAT(monthdate,' ','(',drivernamefuel,')') as drivernamefuel, 
SUM(amount + 
servicecharges + 
replacecharges + 
blockcharges + 
othercharges) AS amount, 
drivernamefuel 
FROM ".$tablename." where carno='".$c."' 
AND monthdate BETWEEN '".$a."' AND '".$b."' 
GROUP BY monthdate"; 

*注意MySQL擴展is now deprecated and will be removed sometime in the future。那是因爲它是古老的,充滿了壞習慣並且缺乏一些現代特徵。不要用它來編寫新的代碼。改爲使用PDOmysqli_*。您的查詢往往是SQL Injection

+0

不工作它顯示錯誤警告:除以零警告:mysql_fetch_array():提供的參數不是有效的MySQL結果資源 – user2468472