This is my table in mysql db:
CREATE TABLE admins (
id int NOT NULL AUTO_INCREMENT,
bez varchar(55) DEFAULT 'unknown',
type varchar(7) NOT NULL DEFAULT 'unknown',
exist char NOT NULL DEFAULT '0',
tm TIMESTAMP default CURRENT_TIMESTAMP;
PRIMARY KEY ('id')
);
I want to update my timestamp only when i set exist to '1'. Not when I change any column.
This is my UPDATE statement:
<?php
if ( isset($_GET["id"]) ) {
$id = $_GET["id"];
$sql = "UPDATE admins SET exist='1' WHERE id=$id";
$con->query($sql);
}
header("location: /index.php");
exit;
?>
I tried different ways to combine it with tm but it didnt worked!
I tried stuff like this:
if ( isset($_GET["id"]) ) {
$id = $_GET["id"];
$tm = date('Y-m-d H:i:s');
$sql = "UPDATE admins SET exist='0', tm=$tm WHERE id=$id";
}
using AND didnt worked.
I tried ON UPDATE CURRENT_TIMESTAMP in sql but it didnt works for me. Because i want only new tm if exist is '1'.
I tried this in sql:
DELIMITER $$
CREATE TRIGGER trigger_name
AFTER UPDATE
ON admins FOR EACH ROW
BEGIN
IF NEW.exists = 1 THEN
SET NEW.tm = NOW();
END IF;
END$$
DELIMITER ;
but i get a sql error message:
ERROR 1362 (HY000): Updating of NEW row is not allowed in after trigger
so i tried to use BEFORE instead of AFTER but i didnt changed the tm at all.
Do you have any ideas? What can i do to solve my issue?
Goal: UPDATE tm when setting exist to '1'
dont tell me that i am open to sql injections! Thanks for your help!