php上传图片并显示数据库(错误)

时间:2016-12-19 12:26:53

标签: php

我正在尝试上传图片并使用数据库显示它 我设法上传并连接到数据库,但它没有显示它 有人可以看看我的代码,告诉我我做错了什么:

db.php:

    <?php   
 $host = "localhost";
 $username = "root";
 $password = "";
 $db_name  = "db_php_school";

  mysql_connect($host,$username,$password) or die("Database not connect");
 mysql_select_db($db_name) or die ("could not connect databse");
 error_reporting("E_ERROR_WORKING");
 echo "Database connecet success";


?>

这是index.php:

<?php 


include './classes/image_class.php';

$obj_image = new Image();

if(@$_POST['Submit'])
{
 $obj_image->image_name=str_replace("'", "''", $_POST['txt_image_name']);
 $obj_image->image=str_replace("'", "''", $_POST['txt_image']);

  $obj_image->Insert_into_image();

  $data_image=$obj_image->get_all_image_list();
 $row=mysql_num_rows($data_image);
}

?>

<!DOCTYPE html>
<html>
<head>
 <title>NOvaeye </title>
</head>
<body>
 <CENTER><H1>Novaeyewear</H1></CENTER>
 <CENTER><H2>Sunglass</H2></CENTER>

  <CENTER>
  <form method="post" enctype="multipart/form-data">
   <table border="1" width="80%">
    <tr>
     <th width="50%">Image NAme</th>
     <td width="50%"><input type="text" name="txt_image_name"></input></td>
    </tr>
    <tr>
     <th width="50%">Upload IMage</th>
     <td width="50%"><input type="file" name="txt_image"></input></td>
    </tr>
    <tr>
     <td></td>
     <td width="50%"><input type="submit" name="Submit" value="Submit"></input></td>
    </tr>
   </table>
  </form>
 </CENTER>

  <?php 
  if($row!=0)
  {
  ?>
  <center>
   <table width="80%" border="1"> 
   <?php
    $icount = 1;
    while($data= mysql_fetch_assoc($data_image))
    {
     ?>
     <tr>
      <td style="text-align:center" style="width:10%;"><?php echo $icount; ?></td>
      <td style="text-align:center" style="width:20%;"><?php echo $data['image_name']?></td>
      <td style="text-align:center" style="width:50%;><img src="images/<?php echo $data['image']; ?>" width="400px" height="200px"></td>
     </tr>
     <?php
     $icount++;
     }
     ?>
   </table>
  </center>
  <?php
 }

  ?>
</body>
</html>

这是image_class.php

<?php
 include 'db/db.php' ;

  class Image{

   var 
   $image_id,
   $image_name,
   $image;

  function Insert_into_image(){
   if(isset($_FILES['txt_image']))
   {
        $tempname = $_FILES['txt_image']['tmp_name'];
        $originalname =$_FILES['txt_image']['name'];
        $size =($_FILES['txt_image']['size']/5242888). "MB<br>";
        $type=$_FILES['txt_image']['type'];
        $image=$_FILES['txt_image']['name'];
        move_uploaded_file($_FILES['txt_image']['tmp_name'],"images/".$_FILES['txt_image']['name']);
      }


    $query = "Insert into t_image_upload
    (
     image_name,
     image
    )
    values
    (
     '$this->image_name',
     '$image'
    )";
    if(mysql_query($query)){
     echo "Insert success";
    }
    else
    {
     echo "Insert not success";
    }
  }

   function get_all_image_list(){
   $query = "select *from t_image_upload";
   $result = mysql_query($query);
   return $result;
  }

}
?>

但问题可能在第63行的index.php中列出

 <td style="text-align:center" style="width:50%;><img src="images/<?php echo $data['image']; ?>" width="400px" height="200px"></td>
     </tr>

这里有一些屏幕截图:

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

首先,检查图像上传到文件夹,然后尝试给出像

这样的绝对路径
<tr>
<td style="text-align:center" style="width:50%;>
<img src="http://localhost/project/images/<?php echo $data['image']; ?>" width="400px" height="200px">
</td>
</tr>