在更新API中获取查询结果

时间:2019-01-28 16:24:07

标签: php mysql api

在PHP中使用我的更新API。我得到了硬编码的结果,例如,如果函数成功运行,则显示success,否则显示fail。我想在编辑之前使用一些if语句来查看特定行是否存在,如果找到并更新了该行,则应返回该行,否则应显示fail

我的代码:

<?php 
  // Headers
  header('Access-Control-Allow-Origin: *');
  header('Content-Type: application/json');
  header('Access-Control-Allow-Methods: PUT');
  header('Access-Control-Allow-Headers: Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');

  include_once '../../../config/Database.php';
  include_once '../models/appPost.php';

  $database = new Database();
  $db = $database->connect();
  $post = new Post($db);
  $data = json_decode(file_get_contents("php://input"));

  $post->status = $data->status;
  $post->trackId = $data->trackId;
    if($post->transaction_update()) {
    echo json_encode(
      array('message' => 'Post Updated')
    );
  } else {
    echo json_encode(
      array('message' => 'Post Not Updated')
    );
  }

...这:

public function transaction_update() {
          // Create query
          $query = 'UPDATE yp_transactions
                                SET status = :status
                                WHERE trackId = :trackId';

          // Prepare statement
          $stmt = $this->conn->prepare($query);

          // Clean data
          $this->status = htmlspecialchars(strip_tags($this->status));
          $this->trackId = htmlspecialchars(strip_tags($this->trackId));

          // Bind data
          $stmt->bindParam(':status', $this->status);
          $stmt->bindParam(':trackId', $this->trackId);

          // Execute query
          if($stmt->execute()) {
            return true;
          }

          // Print error if something goes wrong
          printf("Error: %s.\n", $stmt->error);

          return false;
    }

0 个答案:

没有答案