Here is such a code, how correctly in $ query to implement the insert record if it does not exist.

<?php if ($_SERVER["REQUEST_METHOD"]=="POST"){ require 'connect.php'; createMessage(); } function createMessage(){ global $connect; $fname_lastname = $_POST["fname_lastname"]; $query="INSERT INTO quest_db (fname_lastname) VALUES ('$fname_lastname');"; mysqli_query ($connect,$query)or die (mysqli_error($connect)); mysqli_close($connect); } ?> 

    1 answer 1

    how to properly implement the insert record if it does not exist

    1. Add a unique index to the field to be inserted.
    2. Add IGNORE modifier to INSERT request

       $query="INSERT IGNORE INTO quest_db (fname_lastname) VALUES (...);"; 

    Insertion will occur only if there is no inserted value in the fname_lastname field.

    • I did not understand a bit, I have 2 fields in the database, this is an id with auto-increment, well, a field with a last name and a name that is pulled in the android application. And I did not understand why VALUES is empty, I thought that the unique index in the table replaces the auto-increment. And how to add this same index to the inserted field. Sorry for stupid questions but php, this is new) - Romik romikromik
    • как добавить этот самый индекс в вставляемое поле query. php, это новое This is MySQL, not PHP. - Akina
    • Thank you, figured out. The issue is resolved - Romik romikromik