title: PHP: how to display an uploaded filename in the right place in a table
body: <p>I have been asked to create a website (to be used as an internal resource) where the results of a MySQL table are displayed as an HTML table. Each record is to have an upload button where people can upload certain images and PDFs. Then, they can send this list of records and uploaded files.</p>
  
  <p>I have managed to make a (very) rudimentary upload working. My problems is that I dunno how to display the uploaded file name ONLY in the record that it's supposed to go. How can I accomplish this?</p>
  
  <p>I have tried the stupid thing and just display <code>{$file}</code> in a <code>&lt;td&gt;</code> but of course that displays the filename in every record. I have tried to set $file to empty if the <code>t20pctID</code> isn't equal to part of the filename, but then that ends up in a never-ending loop that displays the same record and then my Firefox crashes.</p>
  
  <p>This is the code I'm using, which is an adaptation of code found on several external resources. Thank you in advance.</p>
  
  <p><strong>Claims.php (following a similar pattern to a cart in an online shop)</strong></p>
  
  <pre><code>&lt;?php
      include '../config/database.php';
      include 'layout_head.php';
  
      $action = isset($_GET['action']) ? $_GET['action'] : "";
      $t20pctID = isset($_GET['song_id']) ? $_GET['song_id'] : "";
      $track_title = isset($_GET['track_title']) ? $_GET['track_title'] : "";
      $file_name = isset($_GET['file']) ? $_GET['file'] : "";
      $user_id = 1;
  
      //display a message
      if ($action =='removed') {
          echo "&lt;div class='alertInfo'&gt;
                   &lt;strong&gt;{$track_title}&lt;/strong&gt; was removed!
                &lt;/div&gt;
          ";
      } elseif ($action =='file_uploaded') {
          echo "&lt;div class='alertInfo'&gt;
                   New file &lt;strong&gt;successfully&lt;/strong&gt; uploaded!
                &lt;/div&gt;
          ";
      } elseif ($action =='sent') { 
          echo "&lt;div class='alertInfo'&gt;
                   &lt;strong&gt;Sent OK!&lt;/strong&gt;
                &lt;/div&gt;
          ";
      } elseif ($action =='sent_failed') { 
          echo "&lt;div class='alertInfo'&gt;
                   &lt;strong&gt;Sent failed :/&lt;/strong&gt;
                &lt;/div&gt;
          ";
      }
  
      $query = "SELECT t.t20pctID, t.main_artist, t.track_title, t.original_album, t.record_label, t.publication_year, c.file FROM cart c LEFT JOIN tblclaims t ON t.t20pctID = c.t20pctID WHERE t.t20pctID LIKE '%Sony%' ORDER BY t.main_artist, t.track_title ASC";  
      $stmt=$con-&gt;prepare($query);
      $stmt-&gt;execute();
  
      $num = $stmt-&gt;rowCount(); 
      if ($num &gt; 0) { 
          echo "&lt;table class='records'&gt;
                  &lt;tr&gt;
                      &lt;th&gt;Main Artist&lt;/th&gt;
                      &lt;th&gt;Track Title&lt;/th&gt;
                      &lt;th&gt;Original Album&lt;/th&gt;
                      &lt;th&gt;Record Label&lt;/th&gt;
                      &lt;th&gt;Publication Year&lt;/th&gt;
                      &lt;th&gt;Documentation (only .jpg, .gif, .png, .pdf allowed)&lt;/th&gt;
                      &lt;th style='width:100px;'&gt;&lt;/th&gt;
                &lt;/tr&gt;
  
          ";
  
          while ($row = $stmt-&gt;fetch(PDO::FETCH_ASSOC)) {
              extract($row);
  
              $form_name = "file_upload_".$t20pctID;
              echo "&lt;tr&gt;
                      &lt;td&gt;
                          &lt;div class='t20pctID' style='display:none;'&gt;{$t20pctID}&lt;/div&gt;
                          &lt;span&gt;{$main_artist}&lt;/span&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;div class='track_title'&gt;{$track_title}&lt;/div&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;span&gt;{$original_album}&lt;/span&gt;
                      &lt;/td&gt;
                      &lt;td&gt;{$record_label}&lt;/td&gt;
                      &lt;td&gt;{$publication_year}&lt;/td&gt;
                      &lt;td&gt;
                          &lt;form name='$form_name' action='upload.php?id=$t20pctID' method='post' enctype='multipart/form-data'&gt;
                              &lt;input type='file' name='upload_file'&gt;
                              &lt;input type='submit' value='Upload' class=''&gt;
                          &lt;/form&gt;";
                          if ($file_name) {
                              $user_id = 1;
                              $query_update = "UPDATE cart SET file='$file_name', uploaded_id='$uploaded_id' WHERE t20pctID=? AND user_id=?";
                              $stmt = $con-&gt;prepare($query_update);
  
                              // bind values
                              $stmt-&gt;bindParam(1, $t20pctID);
                              $stmt-&gt;bindParam(2, $user_id);
                              //update
                              $stmt-&gt;execute();
                          }
                          echo "{$file}"; //
  
              echo "&lt;/td&gt;
                      &lt;td&gt;
                          &lt;a href='remove_from_cart.php?song_id={$t20pctID}&amp;track_title={$track_title}' class='btnCTARemove'&gt;
                              &lt;span class='glyphicon glyphicon-remove'&gt;&lt;/span&gt;Remove&amp;nbsp;&amp;nbsp;
                          &lt;/a&gt;
                      &lt;/td&gt;
                    &lt;/tr&gt;
  
              ";
          }
  
          echo "&lt;tr&gt;
                  &lt;td colspan='6'&gt;&lt;/td&gt;
                  &lt;td id='checkout'&gt;
                      &lt;a href='checkout.php' class='btnCTASend'&gt;
                          &lt;span class='glyphicon glyphicon-shopping-cart'&gt;&lt;/span&gt;Send
                      &lt;/a&gt;
                  &lt;/td&gt;
                &lt;/tr&gt;
          ";
          echo "&lt;/table&gt;";
  
      } else { 
  
          if ($action !='sent' &amp;&amp; $action !='removed') { 
              echo "&lt;div class='alertInfo'&gt;
                       &lt;strong&gt;No songs found&lt;/strong&gt; in your cart!
                    &lt;/div&gt;
              ";
          }
      }
  
      include 'layout_foot.php';
  ?&gt;
  </code></pre>
  
  <p><strong>Upload.php</strong></p>
  
  <pre><code>&lt;?php
      $t20pctID = $_GET['id'];
      date_default_timezone_set('CET');
      $date = date('Y-m-d-H:i:s');
  
      if (isset($_FILES['upload_file'])) {
          $file = $_FILES['upload_file'];
  
          //file properties
          $file_name = $file['name'];
          $file_tmp = $file['tmp_name'];
          $file_size = $file['size'];
          $file_error = $file['error'];
          $file_ext = explode('.', $file_name);
          $file_ext = strtolower(end($file_ext));
          $allowed = array('jpg', 'pdf');
  
          if (in_array($file_ext, $allowed)) {
                  if ($file_size &lt;= 2097152) { 
                      $file_name_new = $t20pctID. '_' . $date . '.' . $file_ext;
                      $file_destination = 'tmp_uploads/' .$file_name_new;
  
                      if (move_uploaded_file($file_tmp, $file_destination)) {
                          header('Location: claims.php?action=file_uploaded&amp;song_id=' . $t20pctID .'&amp;file='. $file_name_new);
                      } else {
                          echo "Error moving the file(s).";
                      }
  
                  } else {
                      echo "Error: your file is too big. The maximum size permitted is 2 MB.";
              }
          } else {
              echo "Error: your file is not permitted (". $file_name."). Only .jpg and .pdf permitted.";
          }
  
      }
  ?&gt;
  </code></pre>