Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在MySQL Php中添加多行_Php_Mysql_Sum - Fatal编程技术网

如何在MySQL Php中添加多行

如何在MySQL Php中添加多行,php,mysql,sum,Php,Mysql,Sum,我想加上金额+服务费用+替换费用+区块费用+其他费用 但它不工作,请帮助我解决这个问题 谢谢 见截图 文件#1: 文件#2: 它正在显示这些错误 警告:在第51行的indexsds.php中被零除 警告:mysql_fetch_array():提供的参数在第73行的indexsds.php中不是有效的mysql结果资源 这里是完整的代码,请检查我的更新 谢谢 <?php include 'config.php'; $link=mysql_connect("$hostname","$use

我想加上金额+服务费用+替换费用+区块费用+其他费用

但它不工作,请帮助我解决这个问题 谢谢

见截图 文件#1:

文件#2:

它正在显示这些错误 警告:在第51行的indexsds.php中被零除

警告:mysql_fetch_array():提供的参数在第73行的indexsds.php中不是有效的mysql结果资源

这里是完整的代码,请检查我的更新 谢谢

 <?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>";
?>

您没有向查询传递参数,所以请按如下方式传递它

而且您没有添加
所选查询的总和(..)中的金额+服务费用+替换费用+区块费用+其他费用,因此请确保替换它

将查询修改为

$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";

我希望这对你有帮助。删除qotes(')并更改总和(要求和的字段)

回显页面中的查询,并尝试直接在phpMyadmin中执行查询。然后检查您的查询有什么问题。问题可能是传递给查询的值

请尝试以下操作:

$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扩展。这是因为它是古老的,充满了坏习惯,缺乏一些现代特征。不要用它来编写新代码。使用或代替。您的查询容易出错。

不工作时显示错误警告:警告中被零除:mysql\u fetch\u array():提供的参数不是有效的mysql结果资源,无法打印sql语句以验证参数$a、$b、$c是否有效。不工作时显示错误警告:警告中被零除:mysql\u fetch\u array():提供的参数不是有效的MySQL结果资源,正在输入参数,例如:$c,修改了我的应答如果不需要,请删除单涂层,而($inf=MySQL\U fetch\U array($result)),在这里查询对您来说没问题,我想:)不工作,它显示错误警告:警告中被零除:MySQL\U fetch\U array():提供的参数在资源中不是有效的MySQL结果
$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;
$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";