小弟在做一个上传图片的练习,
传了图片以后老是显示
Unknown column '$image_id' in 'where clause'
这样的问题,下面是代码,谢谢各位帮帮忙,<?php
$db=mysql_connect('localhost','root','xiaozhuo') or die ('Funk you!');
mysql_select_db('moviesite',$db) or die(mysql_error($db));$dir='c:/Progrm Files/Apache Software Foundation/Apache2.2/htdocs/images';if($_FILES['uploadfile']['error']!=UPLOAD_ERR_OK)
{
switch ($_FILES['uploadfile']['error']){
case UPLOAD_ERR_INI_SIZE:
die('The uploaded file exceeds the upload_max_filesize directvie'.'in php.ini.');
break;
case UPLOAD_ERR_FORM_SIZE:
die('The uploaded file exceeds the MAX_FILE_SIZE directive that'.'was specofied in the HTML form.');
break;
case UPLOAD_ERR_PARTIAL:
die('The uploaded file was only partially uploaded.');
break;
case UPLOAD_ERR_NO_FILE:
die('No file was uploaded.');
break;
case UPLOAD_ERR_NO_TMP_DIR:
die('The server is missing a temporary folder.');
break;
case UPLOAD_ERR_CANT_WRITE:
die('The server failed to write the uploaded file to disk.');
break;
case UPLOAD_ERR_EXTENSION:
die('File upload stopped by extension.');
break;
}
}
$image_caption=$_POST['caption'];
$image_username=$_POST['usernama'];
$image_date=@date('y-m-d');
list($width,$height,$type,$attr)=getimagesize($_FILES['uploadfile']['tmp_name']);
switch($type){
case '1':
$image=imagecreatefromgif($_FILES['uploadfile']['tmp_name'])or die ('The file you uploaded was not a supported filetype.');
$ext='.gif';
break;
case '2':
$image=imagecreatefromjpeg($_FILES['uploadfile']['tmp_name'])or die ('The file you uploaded was not a supported filetype.');
$ext='.jpg';
break;
case '3':
$image=imagecreatefrompng($_FILES['uploadfile']['tmp_name'])or die ('The file you uploaded was not a supported filetype.');
$ext='.png';
break;
default:
die('The file you uploaded was not a supported filetype.');
}$query='INSERT INTO images
(image_caption,image_username,image_date)
VALUES
("'.$image_caption.'","'.$image_username.'","'.$image_date.'")';
$result=mysql_query($query,$db) or die (mysql_error());$last_id=mysql_insert_id();$imagename=$last_id.$ext;
$query='UPDATE images
SET image_filename="'.$imagename.'"
WHERE $image_id ='.$last_id;
$result=mysql_query($query,$db)or die(mysql_error($db));switch($type){
case '1':
$image=imagegif($dir.'/'.$imagename);
break;
case '2':
$image=imagejpeg($dir.'/'.$imagename);
break;
case '3':
$image=imagepng($dir.'/'.$imagename);
break;
}//imagedestroy($image);?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Here is your pic!</title>
</head><body>
<h1>So how does it feel to be famous?</h1>
<p>Here is the picture you just uploaded to our servers:</p>
<img src=image/"<?php echo $imagename; ?>" style="float:left;" />
<table>
<tr><td>Image Saved as:</td><td><?php echo $imagename; ?></td></tr>
    <tr><td>Image Saved as:</td><td><?php echo $ext;?></td></tr>
    <tr><td>Height:</td><td><?php echo $height; ?></td></tr>
    <tr><td>Width:</td><td><?php echo $width; ?></td></tr>
    <tr><td>Upload Date:</td><td><?php echo $image_date; ?></td></tr>
    <tr><td><?php print_r(getimagesize($_FILES['uploadfile']['tmp_name'])); ?></td></tr>
</table>
</body>
</html>

解决方案 »

  1.   

    $query='UPDATE images
    SET image_filename="'.$imagename.'"
    WHERE $image_id ='.$last_id;
    这一句我估计应该是
    $query='UPDATE images
    SET image_filename="'.$imagename.'"
    WHERE image_id ='.$last_id;
      

  2.   

    $query='UPDATE images
    SET image_filename="'.$imagename.'"
    WHERE $image_id ='.$last_id;
    字段名是$image_id吗?
      

  3.   

    数据库里应该不可能有个字段名取成$image_id吧!应该就是这里有问题