0

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

2
  • Please also read meta.stackoverflow.com/questions/388759/… Commented Mar 8, 2022 at 10:39
  • 1
    use select query with where condition and check ANr in table if exists don't insert else insert Commented Mar 8, 2022 at 12:43

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.