我想将图片上传到数据库。
首先我创建了一个表单并尝试将图像上传到数据库中。我还创建了数据库和表格和图像文件夹,我想加载图像。但是我收到以下错误:
INSERT到images_tbl时出现错误(' images_path',' submission_date')VALUES(' images / 24-01-2015-1422097448.jpg',&# 39; 2015-01-24')== ---->您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以获得正确的语法,以便在' images_path',' submission_date')VALUES(' images / 24-01)附近使用-2015-1422097448.jpg'第1行
我做错了什么?
上传图片的表单部分:
<html>
<body>
<form action="saveimage.php" enctype="multipart/form-data" method="post">
<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5" cellpadding="5">
<tbody><tr>
<td>
<input name="uploadedimage" type="file">
</td>
</tr>
<tr>
<td>
<input name="Upload Now" type="submit" value="Upload Image">
</td>
</tr>
</tbody></table>
</form>
</body>
</html>
将图像图像插入数据库的代码部分:
<?php
$con=mysql_connect("localhost","root","");
if(!$con)
{
die('could not connect'.mysql_error());
}
mysql_select_db("image",$con) or die(mysql_error());
function GetImageExtension($imagetype)
{
if(empty($imagetype)) return false;
switch($imagetype)
{
case 'image/bmp': return '.bmp';
case 'image/gif': return '.gif';
case 'image/jpeg': return '.jpg';
case 'image/png': return '.png';
default: return false;
}
}
//Here I try to upload the selected image
if (!empty($_FILES["uploadedimage"]["name"])) {
$file_name=$_FILES["uploadedimage"]["name"];
$temp_name=$_FILES["uploadedimage"]["tmp_name"];
$imgtype=$_FILES["uploadedimage"]["type"];
$ext= GetImageExtension($imgtype);
$imagename=date("d-m-Y")."-".time().$ext;
$target_path = "images/".$imagename;
if(move_uploaded_file($temp_name, $target_path)) {
$query_upload="INSERT into 'images_tbl' ('images_path','submission_date') VALUES
('".$target_path."','".date("Y-m-d")."')";
mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error());
}else{
exit("Error While uploading image on the server");
}
}
?>
答案 0 :(得分:0)
尝试对MYSQL保留字使用`反引号。
您的查询应如下所示。
$sql="INSERT INTO `images_tbl` (`images_path`, `submission_date`)
VALUES
('".$target_path."','".date("Y-m-d")."')";
答案 1 :(得分:0)
您的错误发生在表名和字段名之间的引号中。 查询应该是
$query_upload="INSERT into `images_tbl` (`images_path`,`submission_date`) VALUES ('".$target_path."','".date("Y-m-d")."')";
答案 2 :(得分:0)
试试这个:
将''
替换为table name
和column name
中的``。
使用强>
$query_upload="INSERT into `images_tbl` (`images_path`,`submission_date`) VALUES ('".$target_path."','".date("Y-m-d")."')";
而不是
$query_upload="INSERT into 'images_tbl' ('images_path','submission_date') VALUES ('".$target_path."','".date("Y-m-d")."')";