PHPを使用して、PDFファイルをデータベースに挿入しようとしています。私はそれが一般的に良い習慣ではないことを知っていますが、それでも私はそうする必要があります.
したがって、pdfを挿入しようとしているテーブルのスキーマは次のとおりです。
create table manager_certificate(
manager_id int,
certificate_id int,
certificate blob,
primary key(manager_id, certificate_id),
foreign key(manager_id) references manager(id)
) ENGINE=InnoDB;
ファイルを選択できるようにするhtmlページがあり、ファイルをデータベースにアップロードする必要があるphpファイルを呼び出すアップロードボタンがあります。
html:
<!DOCTYPE html>
<head>
<title>MySQL file upload example</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="manager_upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploaded_file"><br>
<input type="submit" value="Upload file">
</form>
<p>
<a href="list_files.php">See all files</a>
</p>
</body>
</html>
php:
<?php
// Check if a file has been uploaded
if(isset($_FILES['uploaded_file'])) {
// Make sure the file was sent without errors
if($_FILES['uploaded_file']['error'] == 0) {
// Connect to the database
$dbLink = new mysqli('dbclass.cs.nmsu.edu', 'corbinc', 'corbinc430', 'cs482corbinc');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
// Gather all required data
$data = $dbLink->real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name']));
// Create the SQL query
$query = "
INSERT INTO 'manager_certificate'('certificate')
VALUES ( '{$data}' )";
// Execute the query
$result = $dbLink->query($query);
// Check if it was successfull
if($result) {
echo 'Success! Your file was successfully added!';
}
else {
echo 'Error! Failed to insert the file'
. "<pre>{$dbLink->error}</pre>";
}
}
else {
echo 'An error accured while the file was being uploaded. '
. 'Error code: '. intval($_FILES['uploaded_file']['error']);
}
// Close the mysql connection
$dbLink->close();
}
else {
echo 'Error! A file was not sent!';
}
?>
したがって、上記のコードを試してみましたが、次のエラーが発生しています。
Error! Failed to insert the file
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''manager_certificate'('certificate')
VALUES ( '%PDF-1.5\n%ÐÔÅØ\n3 0 ' at line 1
このエラーを修正するにはどうすればよいですか? 問題は私のスキーマにありますか? それともphpでしょうか?