How to INSERT VALUES when file upload is successful
<p>I have an application <a href="http://er5325.aisites.com/scripts/stack/test.html" rel="nofollow">here</a> where the user can upload a file using the Mas's Ajax File Up-loader. The user has to add a row first by clicking on the "Add Question" button and then be able to use the file up-loader. What I want to know is that how can it be coded and where do I place the code so that every time the user selects a file for each row, when the user clicks on the "Upload" button, it will insert the file location in that row from the text box into the database but it would only do that if the file location is not in the database and if the file upload is successful?</p>

<p>I want to insert the values in the 'ImageFile' field and for the ImageId field every time a value is inserted it should add the string 'IMG' and then the next number after the string. E.g if the last ImageId was 'IMG3' then when a value is inserted then the next ImageId should be 'IMG4', then next value inserted means next id is 'IMG5' and etc. I  already have database connected between.</p>

<p>Below is php code which contains the INSERT VALUES and query code which I have at moment:</p>

<pre><code>if (isset($_POST['fileImage'])) {

$_SESSION['fileImage'] = $_POST['fileImage'];

}

if (isset($_POST['submitImageBtn'])) {


$imagequery = "SELECT ImageId, ImageFile FROM Image";

    $insertimage[] = "'". mysql_real_escape_string( $_SESSION['fileImage'] ) ."'";


  $imagequery = "INSERT INTO Image (ImageId, ImageFile) 
  VALUES (" . implode('), (', $insertimage) . ")";


  mysql_query($imagequery);
</code></pre>

<p>Below is the code where it appends the file up-loader in each table row and where the uploading process occurs: (This is in JavaScript even though I know inserting the values occurs in php)</p>

<p>Below is the php page where it uploads the file:</p>

<pre><code>&lt;?php

   $destination_path = str_replace("//", "/", $_SERVER['DOCUMENT_ROOT']."/")."ImageFiles";

   $result = 0;

   $target_path = $destination_path . basename( $_FILES['fileImage']['name']);

   if(@move_uploaded_file($_FILES['fileImage']['tmp_name'], $target_path)) {
      $result = 1;
   }

   sleep(1);
?&gt;

&lt;script language="javascript" type="text/javascript"&gt;window.top.window.stopImageUpload(&lt;?php echo $result; ?&gt;);&lt;/script&gt;   


 &lt;script type="text/javascript"&gt;
         var sourceForm; 

        function insertQuestion(form) {   

            var $tbody = $('#qandatbl &gt; tbody'); 
            var $tr = $("&lt;tr class='optionAndAnswer' align='center'&gt;&lt;/tr&gt;");
            var $image = $("&lt;td class='image'&gt;&lt;/td&gt;"); 


           var $fileImage = $("&lt;form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='startUpload(this);' class='imageuploadform' &gt;" + 
            "&lt;p class='imagef1_upload_process' align='center'&gt;Loading...&lt;br/&gt;&lt;img src='Images/loader.gif' /&gt;&lt;br/&gt;&lt;/p&gt;&lt;p class='imagef1_upload_form' align='center'&gt;&lt;br/&gt;&lt;label&gt;" + 
            "File: &lt;input name='fileImage' type='file' class='fileImage' /&gt;&lt;/label&gt;&lt;br/&gt;&lt;label&gt;" + 
            "(jpg, jpeg, pjpeg, gif, png, tif)&lt;/label&gt;&lt;br/&gt;&lt;br/&gt;&lt;label&gt;" + 
            "&lt;input type='submit' name='submitBtn' class='sbtn' value='Upload' /&gt;&lt;/label&gt;" +
            "&lt;/p&gt; &lt;iframe class='upload_target' name='upload_target' src='#' style='wclassth:0;height:0;border:0px;solclass #fff;'&gt;&lt;/iframe&gt;&lt;/form&gt;");

            $image.append($fileImage);

            $tr.append($image);  
            $tbody.append($tr); 

        }

       function startUpload(imageuploadform){
      $(imageuploadform).find('.imagef1_upload_process').css('visibility','visible');
      $(imageuploadform).find('.imagef1_upload_form').css('visibility','hidden');
      sourceForm = imageuploadform;
          return true;
    }

        function stopUpload(success){
              var result = '';
              if (success == 1){
                 result = '&lt;span class="msg"&gt;The file was uploaded successfully!&lt;\/span&gt;&lt;br/&gt;&lt;br/&gt;';
              }
              else {
                 result = '&lt;span class="emsg"&gt;There was an error during file upload!&lt;\/span&gt;&lt;br/&gt; &lt;br/&gt;' ;
              }
              $(sourceForm).find('.imagef1_upload_process').css('visibility','hidden');
              $(sourceForm).find('.imagef1_upload_form').html(result + '&lt;label&gt;File: &lt;input name="fileImage" type="file"/&gt;&lt;\/label&gt;&lt;br/&gt;&lt;label&gt;(jpg, jpeg, pjpeg, gif, png, tif)&lt;/label&gt;&lt;br/&gt;&lt;br/&gt;&lt;label&gt;&lt;input type="submit" name="submitBtn" class="sbtn" value="Upload" /&gt;&lt;\/label&gt;');
              $(sourceForm).find('.imagef1_upload_form').css('visibility','visible');     
              return true;   
        }
        &lt;/script&gt;
</code></pre>
