title: Truncate before inserting data to mysql with PHP
body: <p>I'm trying to upload an excel file to a mysql DB using PHP. I've got this working.
  However, if I refresh the page or upload again another file, it gets duplicated.</p>
  
  <p>I would like before it uploads a new file to clear (truncate) the table and then insert the new data.</p>
  
  <p>Bue I cannot find where to put or how to put the <code>TRUNCATE TABLE existencias_2018;</code> if the Submit button is clicked and before it inserts the data.</p>
  
  <p>Another issue is the refreshing thing. It is a way to stop the refresh after I've uploaded the data? Or a way that refreshing doesn't duplicate it?</p>
  
  <p>So in summary the help i need is:</p>
  
  <ul>
  <li>Where to put and how the <code>TRUNCATE TABLE existencias_2018;</code>.</li>
  <li>Stop duplicating data if page gets refreshed.</li>
  </ul>
  
  <p>Here is my piece of code:</p>
  
  <pre><code>&lt;?php
  $conn = mysqli_connect("localhost","root","","papa");
  require_once('vendor/php-excel-reader/excel_reader2.php');
  require_once('vendor/SpreadsheetReader.php');
  
  if (isset($_POST["import"])){
      $allowedFileType = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
      if(in_array($_FILES["file"]["type"],$allowedFileType)){
          $targetPath = 'uploads/'.$_FILES['file']['name'];
          move_uploaded_file($_FILES['file']['tmp_name'], $targetPath);
          $Reader = new SpreadsheetReader($targetPath);
          $sheetCount = count($Reader-&gt;sheets());
          for($i=0;$i&lt;$sheetCount;$i++){
              $Reader-&gt;ChangeSheet($i);
              foreach ($Reader as $Row){
                  $model = "";
                  if(isset($Row[0])) {
                      $model = mysqli_real_escape_string($conn,$Row[0]);
                  }
                  $cup = "";
                  if(isset($Row[1])) {
                      $cup = mysqli_real_escape_string($conn,$Row[1]);
                  }
                  $color = "";
                  if(isset($Row[1])) {
                      $color = mysqli_real_escape_string($conn,$Row[2]);
                  }
                  $description = "";
                  if(isset($Row[1])) {
                      $description = mysqli_real_escape_string($conn,$Row[3]);
                  }
                  $size36 = "";
                  if(isset($Row[1])) {
                      $size36 = mysqli_real_escape_string($conn,$Row[4]);
                  }
                  $size38 = "";
                  if(isset($Row[1])) {
                      $size38 = mysqli_real_escape_string($conn,$Row[5]);
                  }
                  $size40 = "";
                  if(isset($Row[1])) {
                      $size40 = mysqli_real_escape_string($conn,$Row[6]);
                  }
                  $size42 = "";
                  if(isset($Row[1])) {
                      $size42 = mysqli_real_escape_string($conn,$Row[7]);
                  }
                  $size44 = "";
                  if(isset($Row[1])) {
                      $size44 = mysqli_real_escape_string($conn,$Row[8]);
                  }
                  $size46 = "";
                  if(isset($Row[1])) {
                      $size46 = mysqli_real_escape_string($conn,$Row[9]);
                  }
                  $size48 = "";
                  if(isset($Row[1])) {
                      $size48 = mysqli_real_escape_string($conn,$Row[10]);
                  }
                  $size50 = "";
                  if(isset($Row[1])) {
                      $size50 = mysqli_real_escape_string($conn,$Row[11]);
                  }
                  $size52 = "";
                  if(isset($Row[1])) {
                      $size52 = mysqli_real_escape_string($conn,$Row[12]);
                  }
                  $size54 = "";
                  if(isset($Row[1])) {
                      $size54 = mysqli_real_escape_string($conn,$Row[13]);
                  }
                  if (!empty($model) || !empty($cup) || !empty($color) || !empty($description) || !empty($size36) || !empty($size38) || !empty($size40) || !empty($size42) || !empty($size44) || !empty($size46) || !empty($size48) || !empty($size50) || !empty($size52) || !empty($size54)) {
                      $query = "insert into existencias_2018(model,cup,color,description,size36,size38,size40,size42,size44,size46,size48,size50,size52,size54) values('".$model."','".$cup."','".$color."','".$description."','".$size36."','".$size38."','".$size40."','".$size42."','".$size44."','".$size46."','".$size48."','".$size50."','".$size52."','".$size54."')";
                      $result = mysqli_query($conn, $query);
                      if (! empty($result)) {
                          $type = "success";
                          $message = "Datos de Excel importados en la base de datos satisfactoriamente";
                      } else {
                          $type = "error";
                          $message = "Ha habido un problema al importar los datos de Excel";
                      }
                  }
              }
          }
      }else{ 
          $type = "error";
          $message = "Tipo de archivo invalido. Suba un archivo de Excel.";
      }
  }
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;    
      &lt;head&gt;
          &lt;style&gt;    
              body {
                  font-family: Arial;
                  width: 1000px;
              }
              .outer-container {
                  background: #F0F0F0;
                  border: #e0dfdf 1px solid;
                  padding: 40px 20px;
                  border-radius: 2px;
              }
              .btn-submit {
                  background: #333;
                  border: #1d1d1d 1px solid;
                  border-radius: 2px;
                  color: #f0f0f0;
                  cursor: pointer;
                  padding: 5px 20px;
                  font-size:0.9em;
              }
              .tutorial-table {
                  margin-top: 40px;
                  font-size: 0.8em;
                  border-collapse: collapse;
                  width: 100%;
              }
              .tutorial-table th {
                  background: #f0f0f0;
                  border-bottom: 1px solid #dddddd;
                  padding: 8px;
                  text-align: left;
              }
              .tutorial-table td {
                  background: #FFF;
                  border-bottom: 1px solid #dddddd;
                  padding: 8px;
                  text-align: left;
              }
              #response {
                  padding: 10px;
                  margin-top: 10px;
                  border-radius: 2px;
                  display:none;
              }
              .success {
                  background: #c7efd9;
                  border: #bbe2cd 1px solid;
              }
              .error {
                  background: #fbcfcf;
                  border: #f3c6c7 1px solid;
              }
              div#response.display-block {
                  display: block;
              }
          &lt;/style&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h2&gt;Importar existencias actualizadas&lt;/h2&gt;
          &lt;div class="outer-container"&gt;
              &lt;form action="" method="post"
              name="frmExcelImport" id="frmExcelImport" enctype="multipart/form-data"&gt;
                  &lt;div&gt;
                      &lt;label&gt;Buscar archivo Excel&lt;/label&gt;
                      &lt;input type="file" name="file" id="file" accept=".xls,.xlsx"&gt;
                      &lt;button type="submit" id="submit" name="import" class="btn-submit"&gt;Importar&lt;/button&gt;
                  &lt;/div&gt;
              &lt;/form&gt;
          &lt;/div&gt;
         &lt;div id="response" class="&lt;?php if(!empty($type)) { echo $type . " display-block"; } ?&gt;"&gt;&lt;?php if(!empty($message)) { echo $message; } ?&gt;&lt;/div&gt;
  &lt;?php
  $sqlSelect = "SELECT * FROM existencias_2018";
  $result = mysqli_query($conn, $sqlSelect);
  if (mysqli_num_rows($result) &gt; 0){
  ?&gt;
         &lt;table class='tutorial-table'&gt;
             &lt;thead&gt;
                 &lt;tr&gt;
                     &lt;th&gt;Modelo&lt;/th&gt;
                     &lt;th&gt;Copa&lt;/th&gt;
                     &lt;th&gt;Color&lt;/th&gt;
                     &lt;th&gt;Descripcion&lt;/th&gt;
                     &lt;th&gt;36&lt;/th&gt;
                     &lt;th&gt;38&lt;/th&gt;
                     &lt;th&gt;40&lt;/th&gt;
                     &lt;th&gt;42&lt;/th&gt;
                     &lt;th&gt;44&lt;/th&gt;
                     &lt;th&gt;46&lt;/th&gt;
                     &lt;th&gt;48&lt;/th&gt;
                     &lt;th&gt;50&lt;/th&gt;
                     &lt;th&gt;52&lt;/th&gt;
                     &lt;th&gt;54&lt;/th&gt;
                 &lt;/tr&gt;
             &lt;/thead&gt;
  &lt;?php
  $sql = "TRUNCATE TABLE existencias_2018";
  while ($row = mysqli_fetch_array($result)) {
  ?&gt;                  
             &lt;tbody&gt;
                 &lt;tr&gt;
                     &lt;td&gt;&lt;?php  echo $row['model']; ?&gt;&lt;/td&gt;
                     &lt;td&gt;&lt;?php  echo $row['cup']; ?&gt;&lt;/td&gt;
                     &lt;td&gt;&lt;?php  echo $row['color']; ?&gt;&lt;/td&gt;
                     &lt;td&gt;&lt;?php  echo $row['description']; ?&gt;&lt;/td&gt;
                     &lt;td&gt;&lt;?php  echo $row['size36']; ?&gt;&lt;/td&gt;
                     &lt;td&gt;&lt;?php  echo $row['size38']; ?&gt;&lt;/td&gt;
                     &lt;td&gt;&lt;?php  echo $row['size40']; ?&gt;&lt;/td&gt;
                     &lt;td&gt;&lt;?php  echo $row['size42']; ?&gt;&lt;/td&gt;
                     &lt;td&gt;&lt;?php  echo $row['size44']; ?&gt;&lt;/td&gt;
                     &lt;td&gt;&lt;?php  echo $row['size46']; ?&gt;&lt;/td&gt;
                     &lt;td&gt;&lt;?php  echo $row['size48']; ?&gt;&lt;/td&gt;
                     &lt;td&gt;&lt;?php  echo $row['size50']; ?&gt;&lt;/td&gt;
                     &lt;td&gt;&lt;?php  echo $row['size52']; ?&gt;&lt;/td&gt;
                     &lt;td&gt;&lt;?php  echo $row['size54']; ?&gt;&lt;/td&gt;
                 &lt;/tr&gt;
  &lt;?php
  }
  ?&gt;
             &lt;/tbody&gt;
         &lt;/table&gt;
  &lt;?php 
  } 
  ?&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>