0

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

语法错误:意外的令牌<

开始我的问题的代码是:

<?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

4

2 回答 2

0

我遇到了同样的问题。我无法替换$file->anything为特定值。我试过3, '3', "3"... 作为intor String。所有这些都返回相同的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;

希望这有帮助。

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

于 2014-12-19T19:00:32.677 回答
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]'  
        );
于 2013-09-24T20:50:47.053 回答