【问题标题】:QUERY ERROR, can't update query in DBQUERY ERROR,无法更新数据库中的查询
【发布时间】:2014-11-03 13:31:18
【问题描述】:

今天我尝试使用 SLIM 框架为我的 REST Web 服务创建更新功能。 但我遇到了这个问题。 我一直坚持我的查询,因为它效果不佳。

这是我的类包含查询

       public function updateUserCard($card_id, $user_id, $barcode) 
       {
            $stmt = $this->conn->prepare("UPDATE user_cards uc, cards c
                                          SET uc.barcode = ?                                  
                                          WHERE uc.id = ? 
                                            AND uc.id = c.card_id 
                                            AND uc.user_id = ?");
            if($stmt == FALSE)
            {
                die($this->conn->error);
            }
            else 
            {
                $stmt->bind_param("iis", $user_id, $card_id, $barcode);
                $stmt->execute();
                $num_affected_rows = $stmt->affected_rows;
                $stmt->close();
                return $num_affected_rows > 0;
            }
      }

我尝试使用新值更新“条形码”列。但它没有用。

这是我在 index.php 中的代码

        $app->put('/cards/users/:id', 'authenticate', function($id) use ($app) 
        {
            // check for required params
            verifyRequiredParams(array('barcode'));

            global $user_id;
            $barcode = $app->request->put('barcode');


            $db = new UserCard();
            $response = array();

            // updating card
            $result = $db->updateUserCard($user_id, $card_id, $barcode);
            if ($result) 
            {
                // card updated successfully
                $response["error"] = false;
                $response["message"] = "Card updated successfully";
            } 
            else 
            {
                // card failed to update
                $response["error"] = true;
                $response["message"] = "Card failed to update. Please try again!";
            }
            echoRespnse(200, $response);
        });

回复总是像$response["message"] = "Card failed to update. Please try again!";

我写错代码了吗? 谢谢你们的帮助:)

【问题讨论】:

  • 您的函数 updateUserCard 采用两个参数($user_id, $barcode),在调用 updateUserCard 时,您首先传递条形码,然后传递用户 ID。检查这一行,$result = $db->updateUserCard($barcode, $user_id);

标签: php mysql web-services rest slim


【解决方案1】:

您在传递调用 updateUserCard 的参数时似乎有错误的顺序。

您的函数 updateUserCard 使用 ($user_id, $barcode) 接受两个参数,并且在调用 updateUserCard 时,您首先传递条形码,然后传递用户 ID。

检查这一行,$result = $db->updateUserCard($barcode, $user_id);

【讨论】:

  • 你有没有更正论证的顺序并尝试过?
  • 是的,哈哈我还是不明白是什么问题
【解决方案2】:

函数 updateUserCard($user_id, $barcode) 内部没有 $card_id

您要么忘记在参数中传递它,将其设为全局,从 '$this-> ' 获取它,要么从其他地方获取它。

【讨论】:

  • 我已经试过了,但还是不行。我认为我的查询出错了。
【解决方案3】:

您需要更改 index.php 代码。试试下面的代码:

 $app->put('/cards/users/:id', 'authenticate', function($id) use ($app) 
        {
            // check for required params
            verifyRequiredParams(array('barcode'));

            global $user_id;
            $barcode = $app->request->put('barcode');


            $db = new UserCard();
            $response = array();

            // updating card
            $result = $db->updateUserCard($card_id, $user_id, $barcode);
            if ($result) 
            {
                // card updated successfully
                $response["error"] = false;
                $response["message"] = "Card updated successfully";
            } 
            else 
            {
                // card failed to update
                $response["error"] = true;
                $response["message"] = "Card failed to update. Please try again!";
            }
            echoRespnse(200, $response);
        });

【讨论】:

  • 还是不行,总是 $response["message"] = "卡更新失败,请重试!";
猜你喜欢
  • 1970-01-01
  • 2016-05-01
  • 1970-01-01
  • 2020-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
相关资源
最近更新 更多