所以我的问题是,我试图在文件被确认转移和上传后运行这个IF语句。唯一的问题是这些操作中只有一个运行。我有一个加载栏,上传完成后应该达到100%。我在浏览器中看到了这个:未捕获的SyntaxError:意外的令牌W - jquery-1.6.3.min.js:2 e.extend.parseJSON jquery-1.6.3.min.js:2 xhr.onload ..但是,如果我使用
INSERT INTO
之前
exit_status('File was uploaded successfuly!');
进度条似乎没有达到100%。但是,如果我把它放在第一位,它确实达到100%,但我没有在我的SQL表中插入任何东西。我怎样才能解决这个问题?我的错误在这里是什么?
<?php
// If you want to ignore the uploaded files,
// set $demo_mode to true;
$demo_mode = false;
$upload_dir = '../img/gallery/';
$allowed_ext = array('jpg','jpeg','png','gif');
if(strtolower($_SERVER['REQUEST_METHOD']) != 'post'){
exit_status('Error! Wrong HTTP method!');
}
if(array_key_exists('pic',$_FILES) && $_FILES['pic']['error'] == 0 ){
$pic = $_FILES['pic'];
if(!in_array(get_extension($pic['name']),$allowed_ext)){
exit_status('Only '.implode(',',$allowed_ext).' files are allowed!');
}
if($demo_mode){
// File uploads are ignored. We only log them.
$line = implode(' ', array( date('r'), $_SERVER['REMOTE_ADDR'], $pic['size'], $pic['name']));
file_put_contents('log.txt', $line.PHP_EOL, FILE_APPEND);
exit_status('Uploads are ignored in demo mode.');
}
// Move the uploaded file from the temporary
// directory to the uploads folder:
$db = new PDO('mysql:DB INFO);
$menuitemcount = $db->prepare("SELECT id FROM bilder");
$menuitemcount->execute();
$menucount = $menuitemcount->rowCount();
$newpicturename = $menucount.$pic['name'];
if(move_uploaded_file($pic['tmp_name'], $upload_dir.$newpicturename)){
date_default_timezone_set('Europe/Oslo');
$namepic = $pic['name'];
$urlpic = "../img/gallery/".strval($menucount).$pic['name'];
$altpic = $pic['name'];
$datepic = date(Y,m,d);
$query = $db->prepare("INSERT INTO bilder (name,url,alt,date) VALUES (:name,:url,:alt,:date)");
$query->execute(array(':name' => $namepic, ':url' => $urlpic, ':alt' => $altpic, ':date' => $datepic));
exit_status('File was uploaded successfuly!');
}
}
exit_status('Something went wrong with your upload!');
// Helper functions
function exit_status($str){
echo json_encode(array('status'=>$str));
exit;
}
function get_extension($file_name){
$ext = explode('.', $file_name);
$ext = array_pop($ext);
return strtolower($ext);
}
?>
这是我在INSERT操作时看到的:
这是我更改插入操作的点和exit_status:
完整的文字:
http://demo.tutorialzine.com/2011/09/html5-file-upload-jquery-php/assets/js/script.js http://demo.tutorialzine.com/2011/09/html5-file-upload-jquery-php/assets/js/jquery.filedrop.js
可能导致错误:
xhr.onload = function() {
if (xhr.responseText) {
var now = new Date().getTime(),
timeDiff = now - start_time,
result = opts.uploadFinished(index, file, jQuery.parseJSON(xhr.responseText), timeDiff);
filesDone++;
if (filesDone == files_count - filesRejected) {
afterAll();
}
if (result === false) stop_loop = true;
答案 0 :(得分:0)
您的SQL查询可能会出现语法错误,因为date
是保留关键字。您应该始终用反引号括起您的表名和列名。
INSERT INTO bilder (`name`,`url`,`alt`,`date`) VALUES (:name,:url,:alt,:date)