我在这里有一个应用程序,用户可以使用 Mas 的 Ajax 文件上传器上传文件。用户必须先通过单击“添加问题”按钮添加一行,然后才能使用文件上传器。我想知道的是如何对其进行编码以及将代码放置在哪里,以便每次用户为每一行选择一个文件时,当用户单击“上传”按钮时,它会将文件位置插入文本框中的那一行到数据库中,但只有当文件位置不在数据库中并且文件上传成功时才会这样做?
我想在“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);
下面是它在每个表行中附加文件上传器以及上传过程发生的代码:(这是在 JavaScript 中,尽管我知道在 php 中插入值)
下面是它上传文件的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>