次のコードを使用して、フォームを処理し、イメージをMySQLデータベースに格納します。イメージをBLOBフィールドにアップロードしようとするとSQL構文でエラーが発生する
$name=htmlentities(stripslashes($_POST['fname']));
$pname=htmlentities(stripslashes($_POST['pname']));
$email=htmlentities(stripslashes($_POST['email']));
$phone=htmlentities(stripslashes($_POST['phone']));
$des=nl2br(htmlentities(stripslashes($_POST['description2'])));
$cost=htmlentities(stripslashes($_POST['price']));
$category=htmlentities(stripslashes($_POST['category']));
$date=htmlentities(stripslashes($_POST['date22']));
$image=htmlentities(stripslashes($_POST['pic']));
$imagedata=file_get_contents($image);
$query="INSERT INTO records
VALUES('','$name','$pname','$email','$phone','$cost','$des','$category','$date','$imagedata');";
if ($connect->query($query) === TRUE) {
echo "Inserted! <a href=\"display.php\">Click here to view database records</a>";
} else {
echo "Error: " . $connect->error;
}
私は、コードを実行すると、私はSQL構文で次のエラーを取得する:
Error: 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 'ݤ‰;(IƒiHôBüŸ¤#Žø#&ad„„¹Ì’¼þý…dÀe‘'Ky÷ ð‰ˆË•¿ffµúßÄe%KÁ€DdѧÑÊÕÂRO÷' at line 2
私は列とそのBLOBをチェックしています。私は列の順序をチェックして、彼らは正常です。何がうまくいかないのか分かりません。
シンプル:データをエスケープする必要があります –
'htmlentities'と' stripslashes'はSQLインジェクションを防ぐために何もしません。 '$ _POST ['pic']'はあなたのシステム上のファイルの場所ですか? – chris85
@ Fred-ii- mysqli_string_real_escape()を使用してエスケープすると、データベースに挿入することができました。しかし、次のように試してみてください。echo ''; – Arihant