在blueimp/jquery-file-upload上添加更多自定义变量到mysql insert


Add more custom variables to mysql insert on blueimp/jquery-file-upload

我目前正在插入一个标题和描述,通过mysql,在blueimp/jquery-file-upload脚本。我使用本教程来实现这一点,但是,我需要添加另一个变量。该变量是当前登录用户ID $_SESSION["userid"]的会话,我想将其插入到我添加的名为uid的列中。通常很容易实现另一列插入,但是这个脚本是非常敏感的,任何时候我弄乱了它,即使是最轻微的一点,我得到"SyntaxError: Unexpected token <"。如有任何帮助,我将不胜感激。

/服务器/php/index . php

$options = array(
    'delete_type' => 'POST',
    'db_host' => 'localhost',
    'db_user' => 'fpform_fanuser',
    'db_pass' => '*****',
    'db_name' => 'fpform_fandata',
    'db_table' => 'files'
);
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(
                'sisss',
                $file->name,
                $file->size,
                $file->type,
                $file->title,
                $file->description,
            );
            $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);

假设您想要更改INSERT查询(在您发布的代码中只有一个INSERT查询),这是您需要更改的内容:

if (empty($file->error)) {
    $sql = 'INSERT INTO `'.$this->options['db_table']
            .'` (`name`, `size`, `type`, `title`, `description`, `uid`)'
            .' VALUES (?, ?, ?, ?, ?, ?)';
    $query = $this->db->prepare($sql);
    $query->bind_param(
            'sisss',
            $file->name,
            $file->size,
            $file->type,
            $file->title,
            $file->description,
            $_SESSION['userid']
        );
    $query->execute();
    $file->id = $this->db->insert_id;
}

我认为你必须在bind_param的第一个参数中添加另一个条目:

$query->bind_param(
        **'sisss',**
        $file->name,
        $file->size,
        $file->type,
        $file->title,
        $file->description,
        $_SESSION['userid']
应该

$query->bind_param(
        **'sissss',**
        $file->name,
        $file->size,
        $file->type,
        $file->title,
        $file->description,
        $_SESSION['userid']

减去星号,当然,或者任何正确的数据类型(参见http://us.php.net/manual/en/mysqli-stmt.bind-param.php的参数类型一节)。

一个奇怪的是,我试图传递一个字面值字符串作为最后一个参数,它抛出了这个错误:

"PHP致命错误:无法通过引用/var/www/html/jqfileuploadertest/server/PHP/index.php第66行传递参数7 "

但是当我将'mystring'更改为$file->name时,它工作正常。这需要一些努力,因为我想给这些加上时间戳。我有一种感觉,只是添加"NOW()"是不会工作的…


Updated:我想为每个文件添加两个字段,上传它的ID(我认为这是OP想要做的)以及时间戳。我添加了这些作为隐藏输入的形式:

<input name="contributor[]" type="hidden" value="myuserid" />
<input name="uploadedOn[]" type="hidden" value="2014-03-28 10:00:00" />

如果你的表单是一个php脚本,你可以动态地生成这两个。

然后将此添加到index.php行~44:

    $file->contributor = @$_REQUEST['contributor'][$index];
    $file->uploadedOn = @$_REQUEST['uploadedOn'][$index];

修改了index.php中准备sql语句的部分:

if (empty($file->error)) {
        $sql = 'INSERT INTO `'.$this->options['db_table']
            .'` (`name`, `size`, `type`, `title`, `description`,`contributor`, `dateUploaded`)'
            .' VALUES (?, ?, ?, ?, ?, ?, ?)';
        $query = $this->db->prepare($sql);
        $query->bind_param(
            'sisssss',
            $file->name,
            $file->size,
            $file->type,
            $file->title,
            $file->description,
            $file->contributor,
            $file->uploadedOn
        );

效果很好

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

你得到的错误是在javascript控制台当你上传文件对吗?在我看来,其中一个正在加载的文件是404没有找到,它试图解析收到的404页面作为一个脚本文件时,它真的是一个html文件,以<…我以前也遇到过这种情况。检查网络并查看正在加载的所有资源。很可能正确的404标头也没有被返回,这就是为什么它会试图解析它。