How to not insert database row if file upload is canceled
<p>I have a cancel button in the form below where if the user is uploading a file, they can click on the "Cancel" button to cancel an upload if they wish. But my the problem I have is that no matter what if the file is in the uploading process, it is always inserting a row into the database containing the file name. What I want is that if an existing or new file is uploaded into the "ImageFiles" folder in the server then insert a database row containing the file name else if upload is cancelled then don't insert a database row, is this possible?</p>

<p>Below is the current form code:</p>

<pre><code>var $fileImage = $("&lt;form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='return startImageUpload(this);' class='imageuploadform' &gt;" + 
  "Image File: &lt;input name='fileImage' type='file' class='fileImage' /&gt;&lt;/label&gt;&lt;br/&gt;&lt;br/&gt;&lt;label class='imagelbl'&gt;" + 
  "&lt;input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /&gt;&lt;/label&gt;" +     
  "&lt;/p&gt;&lt;p class='imagef1_cancel' align='center'&gt;&lt;/p&gt;" +
  "&lt;iframe class='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px;solid;#fff;'&gt;&lt;/iframe&gt;&lt;/form&gt;");
</code></pre>

<p>Below is the imageupload.php script where it uploads the files and where it inserts the database rows:</p>

<pre><code>&lt;?php

session_start();


...//Connect to DB

$result = 0;

if( is_file("ImageFiles/".$_FILES['fileImage']['name'])) {
    $parts = explode(".",$_FILES['fileImage']['name']);
    $ext = array_pop($parts);
    $base = implode(".",$parts);
    $n = 2;

    while( is_file("ImageFiles/".$base."_".$n.".".$ext)) $n++;
    $_FILES['fileImage']['name'] = $base."_".$n.".".$ext;

    move_uploaded_file($_FILES["fileImage"]["tmp_name"],
    "ImageFiles/" . $_FILES["fileImage"]["name"]);
    $result = 1;

    $imagesql = "INSERT INTO Image (ImageFile) 
    VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')";

    mysql_query($imagesql);

}
    else
      {
      move_uploaded_file($_FILES["fileImage"]["tmp_name"],
      "ImageFiles/" . $_FILES["fileImage"]["name"]);
      $result = 1;

        $imagesql = "INSERT INTO Image (ImageFile) 
        VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')";

mysql_query($imagesql);

      }


      mysql_close();

?&gt;
</code></pre>
