How do you put an infu through PHP code to MySQL database without duplicates?



  • I've just started teaching PHP, and MySQL is a bad friend. Now I'm writing a code to do my own self-learning task. And now there's a problem in the code - we need to make the best possible matching or something to keep the duplicates off. I'm bringing the code in text form and squeakyshot for easy reading.


    P.S: Reading about INSERT IGNORE, but somehow I don't understand his logic, please explain. Or offer a better option. When I started the code with if-- (logic) the reference check in the database (i.e. the reference is unique and if it's not in the database, it's possible to record) the code in the 700+ database (no duplicates checked) and the idea was that it was only 10 (i.e., only 10).


    question


    It's a piece of code.

    /*запись данных в базу*/
    $link_test=mysqli_query($bd, "SELECT * FROM `php_dns`.`habra_post`");
    if(!$link_test){
      $bd-> query("INSERT IGNORE INTO habra_post SET title='$title1', text_body='$text1', reg_date='$date1', 
        full_text='$full_text', next_date='$date_nextd', link='$html_in'");
          if($bd==false)
          {
              // echo "<br>Oll OK";
          // }
          // else
          // {
             echo mysqli_error();
          }
    }}}
    


  • INSERT IGNORE is only relevant if you have a unique or primary key in the table if there is no meaningless expression. In this case, you need to first check the existence of the record in the table and then make the record. First, check the existence of a record.

    SELECT * FROM `php_dns`.`habra_post` WHERE title='$title1' AND text_body='$text1' AND reg_date='$date1' AND full_text='$full_text' AND next_date='$date_nextd' AND link='$html_in'
    

    If the record is found, ignore it if you don't, create a new one.

    But this recording is too bad.

    SELECT * FROM `php_dns`.`habra_post` WHERE title='$title1' AND text_body='$text1' AND reg_date='$date1' AND full_text='$full_text' AND next_date='$date_nextd' AND link='$html_in'
    

    You need to figure out what kind of records you don't have to duplicate and just check them, and you'd better start a unique key. If you don't want to do this, optimize or properly organize the table, for example. full_text If he's too loud, make another column. md5 full_text and compare them, and you better move him to another table. Or you have. link If it's a reference to a page, it's unique to every post, you can only check it out - if it's a record.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2