解析错误:语法错误、意外'insert'



我是php新手。我试图连接android与phpmyadmin使用webservice .

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语句上面的

行,它工作正常。

我不明白问题出在哪里,请帮助我找出解决办法

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

相关内容

  • 没有找到相关文章

最新更新