How do i update a record when the post value is not empty, and it the post is empty i will set it with the default value.
user table
id | key | date
-----|-------|-------------
100 | 2 | 26/03/2017
| |
MY PHP
<?php
$setKey = (isset($_POST['change_key']) ? $_POST['key'] : null);
$handler = $conn->prepare("
UPDATE user SET key = IF(key IS NOT NULL, :key, NULL),
date = :date
WHERE id = :id
");
$handler->bindParam(':key', $setKey);
$handler->bindParam(':date', $_POST['date']);
$handler->bindParam(':id', $_POST['id']);
?>
From the above example i want if $setKey
is null the sql will update the date and other columns and set the KEY
to the current value which is 2 from the above example.
答案 0 :(得分:0)
<?php
$setKey = (isset($_POST['change_key']) ? $_POST['key'] : null);
$handler = $conn->prepare("
UPDATE user SET key = IFNULL(:key, NULL),
date = :date
WHERE id = :id
");
$handler->bindParam(':key', $setKey);
$handler->bindParam(':date', $_POST['date']);
$handler->bindParam(':id', $_POST['id']);
?>