-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

スタック オーバーフローの構文の強調表示は、エラーを特定するのに十分なはずです。

SQL クエリの 1 つから終了引用符が抜けています。以下の修正を見つけてください。

 $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 に答える