0

我在哪里放置我的 PHP SQL 查询,以将图像信息插入我的数据库?我在回声“成功”之前尝试过;但是它没有效果。

<!-- Upload Button-->
<div id="upload" >Upload File</div><span id="status" ></span>
<!--List Files-->
<ul id="files" ></ul>

PHP处理

<?php
$uploaddir = './uploads/';
$file = $uploaddir . basename($_FILES['uploadfile']['name']); 

if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
echo "success";
} else {
echo "error";
}
?>

Javascript 部分

$(function () {
    var btnUpload = $('#upload');
    var status = $('#status');
    new AjaxUpload(btnUpload, {
        action: 'upload-file.php',
        //Name of the file input box
        name: 'uploadfile',
        onSubmit: function (file, ext) {
            if (!(ext && /^(jpg|png|jpeg|gif)$/.test(ext))) {
                // check for valid file extension
                status.text('Only JPG, PNG or GIF files are allowed');
                return false;
            }
            status.text('Uploading...');
        },
        onComplete: function (file, response) {
            //On completion clear the status
            status.text('');
            //Add uploaded file to list
            if (response === "success") {
                $('<li></li>').appendTo('#files').html('<img src="./uploads/' + file + '" alt="" /><br />' + file).addClass('success');
            } else {
                $('<li></li>').appendTo('#files').text(file).addClass('error');
            }
        }
    });
});
4

1 回答 1

1

查询应该在成功回显之前正确,因此您必须验证 move_uploaded_file 方法返回给您的内容。

验证 ./upload/ 是否是正确的路径,以及您是否对该目录具有读/写访问权限 (0777)。

还要确保您已连接到数据库:

<?php 

    $conn = mysql_connect("HOST","USER","PASSWORD");
    $db = mysql_select_db("DB_NAME");

    $uploaddir = './uploads/'; 
    $file = $uploaddir . basename($_FILES['uploadfile']['name']);  

    if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) { 
        mysql_query("INSERT INTO photos VALUES ('your_values')");
        echo "success"; 
    } else { 
        echo "error"; 
    } 
 ?> 
于 2012-07-23T10:47:01.850 回答