-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 语句上方的行,它工作正常。

我不明白问题出在哪里。请帮我找到解决方案。

4

1 回答 1

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

}
于 2015-05-24T22:55:05.307 回答