[SOLVED] 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?



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

Answered By – Edjjj

Answer Checked By – Clifford M. (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *