How to make a record check for a match, before adding it to the database?



    3 answers 3

    $result = mysql_query ("SELECT link_site FROM link WHERE link_site ='$les'"); if (mysql_num_rows($result) <= 0) { $result = mysql_query ("INSERT INTO link(link_site) VALUES ('$les')",$db); } else { echo 'уже есть такая запись'; } 
    • This example is not quite working. It works for me (it adds an entry to the database) it only once, then the message “there is already such an entry” flies all the time. What could it be ??? - webkostya

    You can put in the table the type of the field PRIMARY with which the matching test is done. There will be no repetition, that is, if the field value is repeated, the request will not be executed. And if the request is not executed, you can display an error that the transferred data is repeated.

      Like this ... You can search in Google

       $query = mysql_query("select * from table_name where '$name' = pole_name"); $row = fetch_array($query); 
      • one
        Have you accidentally made a mistake? Instead of mysql_fetch_array, you have fetch_array. Plus we do so extra query in the database. If implemented with this method, I would recommend select * from table_name where '$name' = pole_name , replace * write one field. - Node_pro pm
      • 2
        Example code: $a = @mysql_fetch_array(mysql_query("SELECT 'pole_name' FROM 'table' WHERE 'pole_name'='".$name."' LIMIT 1")); IF(isset($a['pole_name'])) echo 'Повтор'; else echo 'Уникальная запись'; $a = @mysql_fetch_array(mysql_query("SELECT 'pole_name' FROM 'table' WHERE 'pole_name'='".$name."' LIMIT 1")); IF(isset($a['pole_name'])) echo 'Повтор'; else echo 'Уникальная запись'; - Node_pro