2011-07-29 93 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選擇是問題。它不是正確發佈.. –