Using my update API in PHP. I am getting hard coded results, such as if the function runs successfully it says success
, else fail
. I want to to use some if statements before editing to see if the particular row exist, if it finds and updates, it should return that row, else it should say fail
.
My code:
<?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')
);
}
...and this:
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.
", $stmt->error);
return false;
}