-2

我正在使用以下代码上传图像文件,当将过程上传到路径时效果很好,但是当文件路径插入到数据库表字段时出现错误

上传者.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 ;
4

3 回答 3

1

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

也许发布你的 SQL 表?

于 2012-11-20T18:19:21.150 回答
1

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

UPDATE students SET image = '".$upload_path."' WHERE student_id= 'pass your id here';
于 2012-11-20T18:32:42.100 回答
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 的列

于 2012-11-20T18:32:30.813 回答