hahakomisch
hahakomisch

Reputation: 49

SQL INSERT from HTML Input with checking for already existing

With a HTML/php Webinterface i am putting some variables to a mysql datebase. The query works fine like this:

INSERT INTO ". $tabledb_stack ." (ANr, Operator) VALUES ('". $ANr ."', '". $Operator ."')

But now i need to check if the ANr Value is already in the database. But just the ANr number and no other Value. Then it should not write it into the database. I tried with a WHERE NOT EXISTS statement, but it isnt working.

Any hints how i can try it?

Thx

Upvotes: 0

Views: 95

Answers (1)

0x45646A
0x45646A

Reputation: 177

If the ANr column has a primary or unique key assigned to it, then it is possible to use INSERT INTO x ON DUPLICATE KEY UPDATE ANr = ANr so it will not update any rows, if the ANr will have any existing rows not depending on Operator column.

In case ANr can have more than one Operator it is possible to use INSERT IGNORE INTO. Thus it will ignore the insert, if there will be an existing row with ANr + Operator already.

For more information, feel free to read MySQL documentation here - https://dev.mysql.com/doc/refman/8.0/en/insert.html

Upvotes: 1

Related Questions