title: If image exists, display that, otherwise show new selected upload
body: <p>I have a post.php where you fill in the post information, post_display.php where it shows the post obviously, and post_edit.php where you can edit the post.
  The post.php and post_edit.php are very similar as the edit just gets all the information from the database and updates it if any changes are made.</p>
  
  <p>In the post_edit.php file - in the form I have the upload button called 'cover' and hidden input called 'cover'.</p>
  
  <pre><code>&lt;?php
      if (file_exists($cover) === true){
        echo '&lt;img src="'. $cover . '" alt="'. $title .'" id="cover"&gt;';
      }else{
        echo '&lt;img src="images/blank_cover.jpg" alt="'. $title .'" id="cover"&gt;';
          }
   ?&gt; 
  
      &lt;div class="ad_upload_btn"&gt;
      &lt;input type="file" name="cover" onchange="readURL(this);"&gt;&lt;div class="ad_upload_btn_text"&gt;Change Cover Image&lt;/div&gt;
      &lt;input type="hidden" name="cover" value="&lt;?php echo $cover; ?&gt;"&gt;
      &lt;/div&gt;  
  </code></pre>
  
  <p>then when the form is submitted, i have this check</p>
  
  <pre><code>if (!empty($_FILES['cover'])){
      if ($_FILES['cover']['size'] == 0) {        
              $file_path = $_POST['cover'];
          }
  
      }       
      else{
          $allowed = array('jpg', 'jpeg', 'gif', 'png');
          $file_name = $_FILES['cover']['name'];
          $file_size = $_FILES['cover']['size'];
          $file_extn = strtolower(end(explode('.', $file_name)));
          $file_temp = $_FILES['cover']['tmp_name'];
          if(in_array($file_extn, $allowed) === true){            
              $file_path = 'images/cover/' . substr(md5(time()), 0, 10) . '.' . $file_extn;
              move_uploaded_file($file_temp, $file_path);
          }else{
              $errors[] = 'Incorrect file type. Only allowed: ' . implode(', ', $allowed) . '';                                       
          }
      }
  }
  </code></pre>
  
  <p>in my database table i have a <code>cover</code> row that takes the path of the image and when i echo it, i simply retrieve image/cover/imagename.jpg into my  code.</p>
  
  <p>Right now, if i upload an image it will work fine. But if I don't upload any image, it will remove the old one that was there and replace it with nothing. How can i tell it to display the old stored image rather than removing everything if no new file is selected to be uploaded?</p>
  
  <p>UPDATE:
  insert code:</p>
  
  <pre><code>if (empty($_POST) === false &amp;&amp; empty($errors) === true){
                  $update_ad = array(
                  'title'         =&gt; $_POST['title'], 
                  'price'         =&gt; $_POST['price'], 
                  'description'   =&gt; $_POST['description'],
                  'category_id'   =&gt; $_POST['category_id'],
                  'cover'         =&gt; mysql_real_escape_string($file_path)
                  );
                  edit_ad($id, $update_ad);                       
                  header('Location: post_display.php?id=' . $id);
                  exit();  
              } 
  
  function create_ad($create_data){
  array_walk($create_data, 'array_sanitize');
  
  $fields = '`' . implode('`, `', array_keys($create_data)) . '`';
  $data = '\'' . implode('\', \'', $create_data) . '\'';
  
  mysql_query("INSERT INTO `posts` ($fields) VALUES ($data)");
  }
  </code></pre>