【发布时间】:2016-01-24 06:43:56
【问题描述】:
我正在我的门户中创建一个单独的页面,从中我可以将图像上传到我的数据库。但是在提交图像时,我收到以下错误:
“插入到'images_tbl'('images_path','submission_date')中的错误 VALUES ('images/24-01-2016-1453612538.jpg','2016-01-24') 你有一个 SQL 语法错误;检查与您对应的手册 MySQL 服务器版本,用于在“images_tbl”附近使用正确的语法 ('images_path','submission_date') 值 ('images/24-01-2016-14' at 第 1 行"
我的 saveimage.php 文件中出现此错误。为什么会出现这个错误?
这是我的 HTML 代码:
<html>
<head>
<title>Image Upload</title>
</head>
<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>
我的 saveimage.php 文件的 PHP 代码:
<?php
include("mysqlconnect.php");
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;
}
}
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");
}
}
?>
我的 mysqlconnect.php 文件的代码是:
<?php
/**********MYSQL Settings****************/
$host="localhost";
$databasename="demo";
$user="root";
$pass="";
/**********MYSQL Settings****************/
$conn=mysql_connect($host,$user,$pass);
if($conn)
{
$db_selected = mysql_select_db($databasename, $conn);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
}
else
{
die('Not connected : ' . mysql_error());
}
?>
【问题讨论】:
-
不要引用表名。
-
$query_upload="INSERT into images_tbl ('images_path','submission_date') VALUES ('".$target_path."','".date("Y-m-d")."')";
-
@GeraldSchneider 我删除了引号并使用了 images_tbl 而不是 'images_tbl' 但这也给了我同样的错误。
-
另请注意
mysql_*函数已被弃用并且不再存在于 PHP7 中。不要使用它们,改用 mysqli 或 PDO。 -
另外你的专栏是什么类型的?你能在这里说一下吗?
标签: php html sql database phpmyadmin