How to use if else statement for SQL in PHP
-
Hy I am trying to store some data in the database
I have the PHP file ‘activity.php’ where I am getting the dataSomehow I am able to store the data in the database using this statement
INSERT INTO
activity(ip_address,activity) VALUES ('$i','activity);
now what I want is that I want to look for the already existing ip address value in the database
like if the value of the same IP address already exists then update the data else insert the data in a new rowI am using this in the PHP file
IF EXISTS (SELECT * FROM
activity` WHERE ip_address = $i)
BEGIN
UPDATEactivity
SET activity=’this is some random value’
WHERE ip_address = $i;
END
ELSE
BEGIN
INSERT INTOactivity
(ip_address,activity) VALUES (‘$i’,’activity);
END”;`but its giving me an error saying
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘IF EXISTS
I pasted the same code in the console of PHPmyadmin and got an error there too
‘Unrecognised statement type neat If-Else’
Why I am getting this error and how can I resolve it
NOTE: I just want to check if a value exists then update the row else insert a new row
Thanks
- The topic ‘How to use if else statement for SQL in PHP’ is closed to new replies.