我使用jQuery的Blueimp文件上傳腳本,並擁有這部分代碼這是工作:PHP擴展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`)'
.' 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;
}
你爲什麼不看[越來越添加其他值的方式(http://ca1.php.net/manual/en/mysqli.quickstart.prepared-statements.php)至查詢,並做同樣的事情? [你可能會暴露自己的SQL注入](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1)。 – miken32
感謝您的評論。我試着像你說的那樣去做。但它不起作用。我已將代碼添加到我的第一篇文章中。我做錯了什麼? TIA – user2096388