title: Can't set image upload variable to NULL
body: <p>So weird well annoying problem, I'm trying to upload images from a form (all works fine) However when there is no image it should set itself to NULL, but because i'm using $target_dir . basename  it keeps taking the last part of the $target_dir instead of setting to NULL and inserting the word products into the database instead of nothing. But obv need the $target_dir for when the upload takes place to put in correct place. Please see code below, all help much appreciated.</p>
  <pre><code>$target_dir = &quot;../images/products/&quot;;
  
  
        if (!isset ($_FILES[&quot;img1&quot;][&quot;name&quot;])) {
          $target_file1 = NULL;
        } else {
          $target_file1 = $target_dir . basename($_FILES[&quot;img1&quot;][&quot;name&quot;]);
            }
  
            if (!isset ($_FILES[&quot;img2&quot;][&quot;name&quot;])) {
              $target_file2 = NULL;
            } else {
              $target_file2 = $target_dir . basename($_FILES[&quot;img2&quot;][&quot;name&quot;]);
            }
  
            if (!isset ($_FILES[&quot;img3&quot;][&quot;name&quot;])) {
              $target_file3 = NULL;
            } else {
              $target_file3 = $target_dir . basename($_FILES[&quot;img3&quot;][&quot;name&quot;]);
            }
  
            if (!isset ($_FILES[&quot;img4&quot;][&quot;name&quot;])) {
              $target_file4 = NULL;
            } else {
              $target_file4 = $target_dir . basename($_FILES[&quot;img4&quot;][&quot;name&quot;]);
            }
  </code></pre>
  <p>SQL query and relevant info</p>
  <pre><code>&lt;pre&gt;
  $image1 = basename($target_file1);
  $image2 = basename($target_file2);
  $image3 = basename($target_file3);
  $image4 = basename($target_file4);
  
  echo $image1;
  echo $image2;
  echo $image3;
  echo $image4;
  
  // Create connection
  $conn = mysqli_connect($servername, $username, $password, $dbname);
  // Check connection
  if (!$conn) {
    die(&quot;Connection failed: &quot; . mysqli_connect_error());
  }
  $sql = &quot;INSERT INTO products (product_code, product_name, category, 
  filter, description, specification, img1, img2, img3, img4, price) 
  VALUES('$product_code', '$product_name', '$category', '$filter', 
  '$description', '$specification', '$image1', '$image2', '$image3', 
  '$image4', '$price')&quot;;
  
  if (mysqli_query($conn, $sql)) {
    echo &quot;New record created successfully&quot;;
  } else {
    echo &quot;Error: &quot; . $sql . &quot;&lt;br&gt;&quot; . mysqli_error($conn);
  }
                ?&gt;
  &lt;/pre&gt;
  </code></pre>
  <p>K, changed it to set the values first and only assign value if isset</p>
  <p>same thing, see screenshot of output below.</p>
  <p><a href="https://i.stack.imgur.com/40UuX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/40UuX.png" alt="enter image description here" /></a></p>