0

I'm trying to delete multiple record with DELETE and IN with MySQL as example below:

DELETE FROM product WHERE sku IN ("rrr","123")

And also, I've request input in array form

["rrr","123"]

So far, this is my PHP PDO Backend code

public function deleteProduct($skuArray){
                try {
                        if ($skuArray) :
                            $str = implode(',', $skuArray);
                            $delete_post = sprintf("DELETE FROM product WHERE sku IN (%s)",$str);
                            $delete_post_stmt = $this->conn->prepare($delete_post);

                    
                            if ($delete_post_stmt->execute()) {
                    
                                echo json_encode([
                                    'success' => 1,
                                    'message' => 'Product Deleted successfully'
                                ]);
                                exit;
                            }
                    
                            echo json_encode([
                                'success' => 0,
                                'message' => 'Product Not Deleted. Something is going wrong.'
                            ]);
                            exit;
                    
                        else :
                            echo json_encode(['success' => 0, 'message' => 'Invalid SKU. No product found by the SKU.']);
                            exit;
                        endif;
                    
                    } catch (PDOException $e) {
                        http_response_code(500);
                        echo json_encode([
                            'success' => 0,
                            'message' => $e->getMessage()
                        ]);
                        exit;
                    }
        }}

And this is the file while act as a controller

<?php
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Headers: access");
    header("Access-Control-Allow-Methods: DELETE");
    header("Content-Type: application/json; charset=UTF-8");
    header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
    
    require_once "../config/database.php";
    include_once "../models/Product.php";

    if ($_SERVER['REQUEST_METHOD'] !== 'DELETE') :
      http_response_code(405);
      echo json_encode([
          'success' => 0,
          'message' => 'Invalid Request Method. HTTP method should be DELETE',
      ]);
      exit;
  endif;

  $database = new Database();
  $conn = $database->dbConnection();
  $items = new Product($conn);
  $data = json_decode(file_get_contents("php://input"));

  if (!isset($data)) {
    echo json_encode(['success' => 0, 'message' => 'Please pick product to delete.']);
    exit;
}

echo json_encode(implode(',',$data));
$items->deleteProduct($data);



?>

When i tried to request Delete in POSTMAN, it says that

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'rrr' in 'where clause'"

enter image description here Based on above error, i don't have 'rrr' column, only 'sku' column.

What could be caused an error from my above code? Please correct it for me

0 Answers0