title: mysql insert error check
body: <p>I have a PHP script that collects form data and inserts some of that data into a MySQL database.  I just noticed that some inserts/records were NOT, or never created in the database.  I would like to write a retry routine  that if the insert fails to retry 3 times and then error out to the user.</p>
  
  <p>Just so you can see my code for the DB and the insert so you can see that I am NOT nuts...</p>
  
  <pre><code>mysql_connect($hostname,$username, $password) or die(mysql_error());
  mysql_select_db($dbname) or die(mysql_error());
  </code></pre>
  
  <p>Here is my insert code:</p>
  
  <pre><code>$query = "INSERT INTO contacts VALUES      ('','$name','$address','$city','$state','$zip','$phone','$email_address','$arrive','$depart','$room','$found','$promocode','$message','$datetimestamp','$ip')";
  mysql_query($query);
  mysql_close();
  </code></pre>
  
  <p>I started out with an IF statement then into a loop but got lost.</p>
  
  <p>@Jay:</p>
  
  <p>So something like this:</p>
  
  <pre><code>$conn = new mysqli($hostname, $username, $password, $dbname);
         // check connection
         if (mysqli_connect_errno())
         {
            exit('Connect failed: '. mysqli_connect_error());
         }
  
         $query = "INSERT INTO contacts VALUES ('','$name','$address','$city','$state','$zip','$phone','$email_address','$arrive','$depart','$room','$found','$promocode','$message','$datetimestamp','$ip')";
         // Performs the $query on the server to insert the values
         if ($conn-&gt;query($query) === TRUE) {
         //echo 'users entry saved successfully';
         }
         else {
           echo 'Error: '. $conn-&gt;error;
         }
         $conn-&gt;close();
  </code></pre>
  
  <p>I am already checking for injection before the insert query</p>