扩展PHP SQL查询不起作用

时间:2014-02-21 22:22:56

标签: php sql blueimp

我正在使用Blueimp jQuery文件上传脚本,并且这部分代码正在运行:

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;
 }    

现在我想只插入一个值为Memberid的值。我使用此列“usr_id”成功扩展了sql表,并修改了这样的代码:

     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`, `usr_id`)'
            .' VALUES (?, ?, ?, ?, ?, ';
        $sql = $sql. $_SESSION['Memberid'] .")";
        $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;
}

但它不起作用。我从未见过这种插入SQL查询的方式。 我希望有人可以提供帮助。

TIA:)

//编辑

我现在也尝试了这个,它也不起作用。我做错了什么?

     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`, `usr_id`)'
            .' VALUES (?, ?, ?, ?, ?, ?)';
        $query = $this->db->prepare($sql);
        $query->bind_param(
            'sisssi',
            $file->name,
            $file->size,
            $file->type,
            $file->title,
            $file->description,
            "2"
        );
        $query->execute();
        $file->id = $this->db->insert_id;
    }
    return $file;
}

1 个答案:

答案 0 :(得分:0)

根据'bind_param'的手册,它必须传递变量。唉,你传递的是常量而不是变量。我没有尝试过以下代码,但它应该做一些明智的事情。

     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`, `usr_id`)'
            .' VALUES (?, ?, ?, ?, ?, ?)';
        $query = $this->db->prepare($sql);
        $usrId = 2; // temporary variable to store the usr_id
        $query->bind_param(
            'sisssi',
            $file->name,
            $file->size,
            $file->type,
            $file->title,
            $file->description,
            $usrId
        );
        $query->execute();
        $file->id = $this->db->insert_id;
    }
    return $file;
}