title: Edit duplicate if exists mysqli php
body: <p>I have some troubles with my mysqli INSERT code.
  I don't really know how to deal with duplicates but I have read many posts there and I tryied to get it work, but when I execute this code with same variables the count of rows increases. Can you please help me fixing this code or find another way to check for duplicates?</p>
  
  <pre><code>  // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn-&gt;connect_error) {
       die("Connection failed: " . $conn-&gt;connect_error);
    }
  
  
    if ($stmt = $conn-&gt;prepare(
      "INSERT INTO matka (jmeno, prijmeni, bydliste, telefon, email)
      VALUES (?,?,?,?,?)
      ON DUPLICATE KEY UPDATE
      jmeno = VALUES(jmeno),
      prijmeni = VALUES(prijmeni),
      bydliste = VALUES(bydliste),
      telefon = VALUES(telefon),
      email = VALUES(email)" )) {
    $stmt-&gt;bind_param("sssss", $jmeno_matky, $prijmeni_matky, $bydliste_matky, $telefon_matky, $email_matky);
    $stmt-&gt;execute();
    printf("%d Row inserted.\n", $stmt-&gt;affected_rows);
    $stmt-&gt;close();
    $id_matky = $conn-&gt;insert_id;
    $conn-&gt;close();
    }
  </code></pre>