2015-05-24 35 views
-1

我是新來的PHP。我正嘗試使用webservice連接android與phpmyadmin。解析錯誤:語法錯誤,意外'插入'

PHP代碼

<?php 
    include_once('configuration.php'); 


$UserId = $_POST['UserId']; 
$ProductId = $_POST['ProductId']; 
$DesiredQuantity = $_POST['DesiredQuantity']; 
$cartstable=mysql_query("SELECT `UserId`, `ProductId`, `DesiredQuantity` FROM `carts` WHERE UId='".$UserId. "' AND ProductId='".$ProductId. "'"); 

    $num_rows = mysql_num_rows($cartstable); 
     if($num_rows>0){ 
$updateqry=mysql_query("Update `carts` set `DesiredQuantity`= `DesiredQuantity` + $DesiredQuantity) WHERE UId='".$UserId. "' AND ProductId='".$ProductId. "'); 

} 
     else 
{ 
$insertqry=mysql_query ("Insert into `carts` (`UId`, `ProductId`, `DesiredQuantity`) VALUES ('".$UserId. "','".$ProductId. "',$DesiredQuantity)"); 

} 


     $carts_ful=mysql_query("SELECT `UserId`, `ProductId`, `DesiredQuantity` FROM `CARTS` WHERE UId='".$UserId. "'"); 

     while($carts = mysql_fetch_array($carts_ful)){ 
     extract($carts); 
     $result[] = array("UserId" => $UserId,"ProductId" => $ProductId,"DesiredQuantity" => $DesiredQuantity); 
    } 
     $json = array("Updated Cart Details" => $result); 
     @mysql_close($conn); 
     header('Content-type: application/json'); 
     // echo "Selected Product is added to the Cart !"; 
     echo json_encode($json); 


?> 

當我試圖運行,我看到下面的錯誤

<b>Parse error</b>: syntax error, unexpected 'insert' . 

如果我剪切和粘貼,

$insertqry=mysql_query ("Insert into `carts` (`UId`, `ProductId`, `DesiredQuantity`) VALUES ('".$UserId. "','".$ProductId. "',$DesiredQuantity)"); 

行if語句以上,它工作正常。

我無法理解問題在哪裏。請幫我找到解決方案。

+0

您不關閉'$ updateqry = mysql_query'行末尾的字符串。 –

+0

使用適當的編輯器/ IDE。語法突出顯示會顯示錯誤,就像SO正在做的那樣。 (我討厭想*哪位*編輯的人誰問這些問題使用..) – user2864740

+0

非常感謝:-)現在工作正常@ Jon Stirling –

回答

1

Stack Overflow的語法高亮應該足以發現錯誤。

您錯過了某個SQL查詢的結束報價。找到下面的修改。

$updateqry=mysql_query("Update `carts` set `DesiredQuantity`= `DesiredQuantity` + $DesiredQuantity) WHERE UId='".$UserId. "' AND ProductId='".$ProductId."'"); 

} 
     else 
{ 
$insertqry=mysql_query ("Insert into `carts` (`UId`, `ProductId`, `DesiredQuantity`) VALUES ('".$UserId. "','".$ProductId. "',$DesiredQuantity)"); 

} 
相關問題