I'm trying to check for the existence of certain data in the database before performing a function, my code looks like this at the moment. I can't seem to figure this out.
Asked
Active
Viewed 62 times
-1
-
2what is not working? – RisingSun Feb 07 '19 at 22:57
-
this part is wrong: `...$menuitemcode& reservering=$re...` for a starter. But we don't know what your problem is... – Jeff Feb 07 '19 at 23:01
-
_"as soon as I get home"_ doesn't sound like you are prepared to co-work in solving your unknown problem... – Jeff Feb 07 '19 at 23:04
-
@TorbjörnStabo I don't think the space after `&` will get parsed correctly. either the value is in `$_GET['%20reserving']` or there's no param at all. No, I didn't test it, just an assumption – Jeff Feb 07 '19 at 23:07
1 Answers
1
One option you might want to be aware of is an "upsert": Insert a new row (of the column doesn't exist); otherwise, update the existing row:
- How to Perform an UPSERT so that I can use both new and old values in update part
- How to INSERT If Row Does Not Exist (UPSERT) in MySQL
EXAMPLE:
SET @id = 1,
@title = 'In Search of Lost Time',
@author = 'Marcel Proust',
@year_published = 1913;
INSERT INTO books
(id, title, author, year_published)
VALUES
(@id, @title, @author, @year_published)
ON DUPLICATE KEY UPDATE
title = @title,
author = @author,
year_published = @year_published;
Another caveat, since you're updating your database with respect to a PHP (possibly internet-facing) app: ALWAYS use a prepared statement (as opposed to a raw SQL "insert" or "update"):
This will help mitigate SQL injection attacks.

FoggyDay
- 11,962
- 4
- 34
- 48