Fileupload.js + MySQL

时间:2013-09-24 20:19:21

标签: php jquery mysql ajax file-upload

我在php中使用javascript / jQuery有点新...我正在开发一个必须处理上传的系统。使用fileupload.js一切正常但是......我无法检索文件信息并存储在mysql中,如名称,扩展名,大小,网址......当我尝试某些内容时,我收到了错误消息:

  

SyntaxError:意外的令牌<

启动我的问题的代码是:

<?php
$options = array(
    'delete_type' => 'POST',
    'db_host' => 'localhost:3306',
    'db_user' => 'root',
    'db_pass' => '123456',
    'db_name' => 'house',
    'db_table' => 'midias'
);

error_reporting(E_ALL | E_STRICT);
require('UploadHandler.php');

class CustomUploadHandler extends UploadHandler {

    protected function initialize() {
        $this->db = new mysqli(
            $this->options['db_host'],
            $this->options['db_user'],
            $this->options['db_pass'],
            $this->options['db_name']
        );
        parent::initialize();
        $this->db->close();
    }

    protected function handle_form_data($file, $index) {
        $file->title = @$_REQUEST['title'][$index];
        $file->description = @$_REQUEST['description'][$index];
    }

    protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
            $index = null, $content_range = null) {
        $file = parent::handle_file_upload(
            $uploaded_file, $name, $size, $type, $error, $index, $content_range
        );
         if (empty($file->error)) {
            $sql = 'INSERT INTO `'.$this->options['db_table']
                .'` (`name`, `size`, `type`, `title`, `description`)'
                .' VALUES (?, ?, ?, ?, ?)';
            $query = $this->db->prepare($sql);
            $query->bind_param(
                $file->name,
                $file->size,
                $file->type,
                $file->title, // I will try change for $_SESSION[userID]
                $file->description // I will try change for dateOfUpload
            );
            $query->execute();
            $file->id = $this->db->insert_id;
        }
        return $file;
    }

    protected function set_additional_file_properties($file) {
        parent::set_additional_file_properties($file);
        if ($_SERVER['REQUEST_METHOD'] === 'GET') {
            $sql = 'SELECT `id`, `type`, `title`, `description` FROM `'
                .$this->options['db_table'].'` WHERE `name`=?';
            $query = $this->db->prepare($sql);
            $query->bind_param('s', $file->name);
            $query->execute();
            $query->bind_result(
                $id,
                $type,
                $title,
                $description
            );
            while ($query->fetch()) {
                $file->id = $id;
                $file->type = $type;
                $file->title = $title;
                $file->description = $description;
            }
        }
    }

    public function delete($print_response = true) {
        $response = parent::delete(false);
        foreach ($response as $name => $deleted) {
            if ($deleted) {
                $sql = 'DELETE FROM `'
                    .$this->options['db_table'].'` WHERE `name`=?';
                $query = $this->db->prepare($sql);
                $query->bind_param('s', $name);
                $query->execute();
            }
        } 
       return $this->generate_response($response, $print_response);
    }

}

$upload_handler = new CustomUploadHandler($options);

我无法弄清楚发生了什么,我希望有人能给我一些启发 感谢您的关注,对于巨大的代码感到抱歉; s

2 个答案:

答案 0 :(得分:0)

试试这个:

我通过date_description更改说明;确保数据库中包含该字段,并且它是日期时间类型。

   session_start();

   $sql = 'INSERT INTO `'.$this->options['db_table']
            .'` (`name`, `size`, `type`, `title`, `date_description`)'
            .' VALUES (?, ?, ?, ?, now())';
        $query = $this->db->prepare($sql);
        $query->bind_param(
            $file->name,
            $file->size,
            $file->type,
            '$_SESSION[userID]'  
        );

答案 1 :(得分:0)

我遇到了同样的问题。 我不能用特定值替换$file->anything。我尝试3'3'"3" ...作为intString。所有这些都返回了相同的SyntaxError: Unexpected token

我的解决方案

$myvar = "hvhjvcvv";
$sql = 'INSERT INTO `'.$this->options['db_table']
            .'` (`randomField`, `name`, `size`, `type`, `title`, `description`)'
            .' VALUES (?, ?, ?, ?, ?, ?)';

$query = $this->db->prepare($sql);
$query->bind_param('ssisss', $myvar, $file->name, $file->size, $file->type, $file->title, $file->description);
$query->execute();
$file->id = $this->db->insert_id;

希望得到这个帮助。

另请注意,错误的数据库连接将返回相同的错误;)