简体   繁体   中英

How to use PATCH on PHP REST API on the following scenario?

I am building a rest api with basic php code and the following code updates the records for one of my table:

$conn = $this->conn;
$returnArr = array();

$query = 'UPDATE `items` SET `name` = ?, `description` = ? WHERE `itemID` = ?';
$stmt = $conn->prepare( $query );
$stmt->bind_param( 'ssi', $this->name, $this->description, $this->itemId );

$result = $stmt->execute();
if ( $result ) {
    $stmt->close();
    return true;
} else {
    $stmt->close();
    return false;
}

So, let us say the code updates the 'items' table columns: 'name and description' from the following request made from front-end:

{
    "name": "Test item",
    "description": "Test description",
    "itemId" : 1
}

Now what if I wanted my code to only update the name when I send only the name from front-end:

{
    "name": "Test item",
    "itemId" : 1
}

What currently happens is the description is updated with NULL since $this->description is null (not initialized)

How to perform patch here with just simple basic PHP logic?

Build the query dynamic on the fields you want to change only.

This may look a bit complicated for only 2-3 values, but if you have a large structure (many columns/fields) it is definitely worth it.

// The dynamic data
$data = json_decode('{
    "name": "Test item",
    "description": "Test description",
    "itemId" : 1
}', true);

// a list of allowed fields for the query
$allowedFields = ['name', 'description', 'itemId'];

// build a list of fields and values for the query
$fields = [];
$values = [];
$itemId = 0;

// Go thru data and check if it is in the allowed list
// Also special treatment for the itemId

foreach($data as $field => $value) {
    if(!in_array($field, $allowedFields)) continue;
    if($field == 'itemId') {
        $itemId = $value;
        continue;
    }
    $fields[] = "`$field` = ?";
    $values[] = $data[$field];
}

// Place the itemId as very last
$values[] = $itemId;

// Build the query string
$query = 'UPDATE `items` SET ' . join(', ', $fields) . ' WHERE `itemID` = ?';

// Prepare and execute with the values
$sth = $dbh->prepare($query);
$sth->execute($values);

UPDATE items SET name =?, description =? WHERE itemID =?

Use store procedure in mysql and call it in PHP with every inputs contains NULL. You can filter when input is null in mysql SP.

DELIMITER \\

CREATE PROCEDURE `Update_Info`(
  IN p_name varchar(64),
  IN p_description varchar(64),
  IN p_itemID INT
)
BEGIN    
  UPDATE items
  SET `name` = IF(p_name = '', `name`, p_name),
  `description` = IF(p_description = '', `description`, p_description)
  WHERE itemID = p_itemID;
END

and in php something like this:

// calling stored procedure command
$sql = 'CALL sp_takes_string_returns_table(?,?,?)';

// prepare for execution of the stored procedure
$stmt = $pdo->prepare($sql);

// pass value to the command
$stmt->bind_param( 'ssi', $this->name, $this->description, $this->itemId );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM