在blueimp/jquery-file-upload上向mysql插入中添加更多自定义变量 [英] Add more custom variables to mysql insert on blueimp/jquery-file-upload

查看:69
本文介绍了在blueimp/jquery-file-upload上向mysql插入中添加更多自定义变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在通过mysql在blueimp/jquery-file-upload脚本中插入标题和描述.我使用了教程来达到目标​​,但是,我需要添加另一个变量.该变量是当前登录用户ID $_SESSION["userid"]的会话,我想将其插入到我添加的称为uid的列中.通常,将另一列包含在插入内容中很简单,但是此脚本非常敏感,每当我弄乱它时,即使是最细微的地方,我都会得到"SyntaxError: Unexpected token <".任何帮助将不胜感激.

I am currently inserting a title and description, via mysql, inside of the blueimp/jquery-file-upload script. I used this tutorial to get me there, however, i need to add another variable. The variable is the session of the current logged in user's ID $_SESSION["userid"], and i want to insert it into a column i added called uid. Usually it's simple to impliment another column into the insert, however this script is very touchy and anytime i mess with it, even the slightest bit, i get "SyntaxError: Unexpected token <". Any help would be greatly appreciated.

$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);

推荐答案

上载文件时,您遇到的错误是在javascript控制台中吗?在我看来,正在加载的文件之一未找到404,并且它实际上尝试将接收到的404页解析为脚本文件,而实际上它是一个以< ...开头的html文件.我以前曾经发生过这种情况.检查网络下的情况,并查看发生时正在加载的所有资源.可能也没有返回正确的404标头,这就是为什么它试图解析它的原因.

The error you're getting is in the javascript console when you upload the file right? I seems to me that one of the files thats being loaded is 404 not found, and its trying to parse the received 404 page as a script file when really its an html file that starts with <... I had this happen once before. Check under network and view all the resources that are being loaded when it happens. Its likely that the correct 404 headers are not being returned either which is why it would be attempting to parse it.

这篇关于在blueimp/jquery-file-upload上向mysql插入中添加更多自定义变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆