PHP图片上传 - 发现错误

时间:2012-07-20 10:45:59

标签: php mysql

我的PHP图片上传代码有一个小问题,我没有将正确的文件名保存到我的MySQL数据库。

请告诉我我做错了什么。

PHP

// Upload File to Directory
$code = md5(time());
$image = $_FILES['userfile']['name'];
$imagename = $code.$image;
$uploaddir = '../_gallery/' . $imagename;
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);

MySQL查询

$sql="UPDATE b_events 
      SET ename = '$event_name', 
          edescription = '$event_description', 
          edate = '$event_date', 
          etime = '$event_time', 
          ecost = '$event_cost', 
          eimage = '$imagename' 
      WHERE id = '$update'";
$result=mysql_query($sql);

1 个答案:

答案 0 :(得分:1)

您需要将此名称插入数据库,因为您通过此名称将文件上传到目录

或将值赋给新变量

$sql="UPDATE b_events 
          SET ename = '$event_name', 
              edescription = '$event_description', 
              edate = '$event_date', 
              etime = '$event_time', 
              ecost = '$event_cost', 
              eimage = '$uploadfile' 
        WHERE id = '$update'";

OR

  $file_name =basename($_FILES['userfile']['name'];

$sql="UPDATE b_events 
          SET ename = '$event_name', 
              edescription = '$event_description', 
              edate = '$event_date', 
              etime = '$event_time', 
              ecost = '$event_cost', 
              eimage = '$file_name' 
        WHERE id = '$update'";