如何在文件上载成功时插入值

时间:2012-04-10 23:55:17

标签: php javascript jquery

我有一个应用程序here,用户可以使用Mas的Ajax File Up-loader上传文件。用户必须首先通过单击“添加问题”按钮添加行,然后才能使用文件上载器。我想知道的是它如何编码以及我在何处放置代码,以便每次用户为每行选择一个文件时,当用户点击“上传”按钮时,它将插入文件位置从文本框到数据库的那一行,但只有在文件位置不在数据库中且文件上传成功的情况下才会这样做?

我想在'ImageFile'字段和ImageId字段中插入值,每次插入一个值时,它应该添加字符串'IMG',然后添加字符串后面的下一个数字。例如,如果最后一个ImageId是'IMG3',那么当插入一个值时,下一个ImageId应该是'IMG4',然后插入下一个值意味着下一个id是'IMG5'等等。我已经连接了数据库。

下面是php代码,其中包含我现在拥有的INSERT VALUES和查询代码:

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);

下面是代码,它在每个表行中附加文件up-loader以及上传过程发生的位置:(即使我知道插入值发生在php中,这是在JavaScript中)

下面是上传文件的php页面:

<?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);
?>

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


 <script type="text/javascript">
         var sourceForm; 

        function insertQuestion(form) {   

            var $tbody = $('#qandatbl > tbody'); 
            var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
            var $image = $("<td class='image'></td>"); 


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

            $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 = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
              }
              else {
                 result = '<span class="emsg">There was an error during file upload!<\/span><br/> <br/>' ;
              }
              $(sourceForm).find('.imagef1_upload_process').css('visibility','hidden');
              $(sourceForm).find('.imagef1_upload_form').html(result + '<label>File: <input name="fileImage" type="file"/><\/label><br/><label>(jpg, jpeg, pjpeg, gif, png, tif)</label><br/><br/><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /><\/label>');
              $(sourceForm).find('.imagef1_upload_form').css('visibility','visible');     
              return true;   
        }
        </script>

1 个答案:

答案 0 :(得分:0)

您可以将INSERT放入

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

    ... insert image details into database here ...
}

我还建议使用图像ID的自动增量ID和存储图像名称的安全随机字符串。然后,您可以返回最后一个插入ID或-1以表示失败(或包含这些和任何其他详细信息的XML / JSON对象)。

祝你好运