PHP数据库文件上传错误

时间:2012-11-20 18:16:47

标签: php mysql sql database file-upload

我正在使用以下代码上传图像文件,当上传进程到路径运行良好时,但当文件路径插入数据库表字段时出现错误

uploader.php

<?php
$db = mysql_connect('localhost', 'root', '') or die('Could not connect:' . mysql_error());
mysql_select_db('fileupload4',$db);
$allowed_filetypes = array('.jpg','.gif','.bmp','.png');
$max_filesize = 10000000;
$upload_path = 'images/'; 
$filename = $_FILES['userfile']['name'];
$extension = substr($filename, strpos($filename,'.'), strlen($filename)-1);
if(!in_array($extension , $allowed_filetypes)) {
    die('The file you attempted to upload is not allowed.');
}
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize) {
    die('The file you attempted to upload is too large.');
}
if(!is_writable($upload_path)) {
   die('You cannot upload to the specified directory, please CHMOD it to 777.');
}
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_path.$filename)) {
    echo 'Your file upload was successful, view the file <a href="'.$upload_path.$filename.'" title="Your File">Here</a>';
} else {
    echo 'There was an error during the file upload.  Please try again.'; // It failed
}
$sql = "UPDATE students SET image = '".$upload_path."' WHERE id = 'student_id'";
mysql_query($sql) OR die(mysql_error());
?>

这是上传图片时出现的错误

Unknown column 'id' in 'where clause'

感谢您的帮助...

我的表格结构

CREATE TABLE IF NOT EXISTS `students` (
  `student_id` int(11) NOT NULL AUTO_INCREMENT,
  `image` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '',
  PRIMARY KEY (`student_id`),
  KEY `student_id` (`student_id`),
  KEY `image` (`image`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

3 个答案:

答案 0 :(得分:1)

检查你的MYSQL表结构,显然列id不存在。

也许发布你的SQL表?

答案 1 :(得分:1)

正如您在此处发布的查询所示,您似乎没有使用id列(正如我之前在评论中已经告诉过您的那样),正如@saratis告诉您的那样,所以您的查询应该是

UPDATE students SET image = '".$upload_path."' WHERE student_id= 'pass your id here';

答案 2 :(得分:0)

看起来像

$sql = "UPDATE students SET image = '".$upload_path."' WHERE id = 'student_id'";

需要更改为

$sql = "UPDATE students SET image = '".$upload_path."' WHERE student_id = 'student_id'";

因为您没有名为id的列,但您有一个名为student_id的列