Checking for a record in mysql

I am writing my three-wheeled CRUD. In this regard, the following questions arose: I want the INSERT and UPDATE operations to be implemented in one setEntry () wrapper function (I understand that the logic of operations is separated for safety, but I will try not to mess it up) the question is how appropriate is this implementation?

And the second question: when inserting a new entry (setEntry()), it is necessary to determine whether there is a given entry at all, but this turns out to be two queries – is it advisable to load mysql like this?

And actually, how is it generally better to make this setEntry so that, depending on the existence of the entry, it generates either INSERT or UPDATE.

Answer 1, authority 100%

You can use the extended syntax of the INSERT INTO statement

INSERT INTO tablename
    key_field = 1,
    data_field1 = 2,
    data_field2 = 3
    data_field1 = 2,
    data_field2 = 3;

In this case, an entry with the key key_field = 1will be added if it did not exist before, and if it already exists, it will be updated.

Answer 2

For such complex queries where you need to check something against the database. You can use stored procedures. So MySQL will be loaded with less than a few queries from PHP.