2011-07-29 29 views
1

更新PHP查询问题

也许我只是一个虚拟的,看不到我的错误。基本上这是函数处理一切背后的数学。它具有多种查询和更新,并在两个不同的表中插入..

当我尝试处理它,它给了我:

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 241 

这里是我的功能:

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')"); 

    } 

我觉得问题来自我的选择(下拉式),它发布到脚本中:

<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') 
+1

$查询=“SELECT投标,账户, hrly,rhrly,bal FROM billing WHERE bid ='。$ bid。''; - 在这里检查你的报价,我想你想用双引号作为外部报价:$ query =“SELECT bid,account,hrly,rhrly,bal FROM billing WHERE bid ='”。$ bid。“'”; - 这也可能是错误的原因(也许是因为没有返回?) – Quasdunk

+0

1)尝试echo'ing你的查询来检查它看起来是否正确,2)尝试echo'ing mysql_error()来查看MySQL是否在理解你的查询时遇到问题(即使它看起来是正确的)。 – Flambino

+0

是的,第一个查询看起来不正确,所以$ result是False(但我们猜测这里的出价可能是一个数字......),当试图从错误中获取一行时,会导致错误消息 – Dilettant

回答

1

如果使用此相反,你得到了什么输出:

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(); 
} 
+0

我添加到“詹姆斯”下的问题 –

+0

该出价未发布上一页。在问题中查看我的html选择代码。表单没有正确发布。 –

+0

看来您的答案是没有设置$ bid,可能是因为$ _POST ['bid']为空。 – James

0

根据所提供的信息,很难弄清楚问题所在。您最好的解决方案是在运行查询之后立即输出mysql_error()。

$result = mysql_query($query); 
echo mysql_error(); 
+0

它不会给出错误。我已经尝试过了。 –

+0

其实刚刚得到:你的SQL语法有错误;检查对应于你的MySQL服务器版本的手册,在第一行附近使用正确的语法 –

0

除非你没有正确指定表名或字段名,您SELECT语句中的值时,应适当引用的包裹。

+0

我认为我的html选择是问题。这不是正确发布.. –

1

这是你的连接。

变化

$query = 'SELECT bid, account, hrly, rhrly, bal FROM billing WHERE bid='.$bid.''; 

$query = 'SELECT bid, account, hrly, rhrly, bal FROM billing WHERE bid='.$bid; 

我也假设bid是一个整数。否则,你需要引号:

$query = 'SELECT bid, account, hrly, rhrly, bal FROM billing WHERE bid="'.$bid.'"'; 

这是错的太

mysql_query("UPDATE billing SET bal = '$total' WHERE bid ='.$bid.'"); 

应该像

mysql_query("UPDATE billing SET bal = '{$total}' WHERE bid ='{$bid}'"); 
-- or full concatenation 
mysql_query("UPDATE billing SET bal = '" . $total . "' WHERE bid ='" . $bid . "'"); 

你也一样最后一个查询。

+0

这工作,现在我的数学不起作用。给出所有的零 –

+0

我已经更新了我的答案 –

+0

太棒了!现在我的数学不适用于我正在成倍增加的地方 –

0

对我来说,检查从PHP生成SQL查询字符串的结果似乎很有帮助。 echo $query(应该在第一个查询中显示假定的错误)。 如果读取字符串不会发现错误,那么通过mysql将其送入测试数据库可能会有很大帮助,特别是。混合SQL,PHP,单引号和双引号并不总是很容易写也不会读...

+0

我认为我的html选择是问题。它不是正确发布.. –