PHP从mysql数据库显示图像

时间:2017-03-19 01:41:05

标签: php mysql

我正在关注此在线教程:https://www.youtube.com/watch?v=Ipa9xAs_nTg。我很好奇,因为我的代码与他的代码相同,但它不起作用。这是我的代码:

<?php
$msg="";
if(isset($_POST['upload'])){
    $target="images/".basename($_FILES['image']['name']);

    $db=mysqli_connect("localhost","root","","photos");

    $image=$_FILES['image']['name'];
    $text=$_POST['text'];

    $sql="INSERT INTO images(image,text)VALUES('$image','$text')";
    mysqli_query($db,$sql);

    if(move_uploaded_file($_FILES['image']['tmp_name'],$target)){
        $msg="Image uploaded succesfully";
    }
    else{
        $msg="There was a problem uploading image";
    }


}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
This is a file upload page where you can upload your picture 
</div>

<?php
    $db=mysqli_connect("localhost","root","","photos");
    $sql="SELECT *FROM images";
    $result=mysqli_query($db,$sql);
    $path="images/";
    while($row = mysqli_fetch_array($result)){
        echo "<div id='img_div'>";
            echo "<img src='images/".$row['image']."'>";
            echo"<p>".$row['text']."</p>";
            echo "</div>";
    }
?>


<form method="POST" action="index.php" enctype="multipart/form-data">
<input type="hidden" name="size" value="100000">
 Choose file:<input type="file" name="image" value="Choose file">
 <textarea name="text" cols="40" rows="4"></textarea>
 <input type="submit" name="upload" value="Upload Image">
</form>

</body>
</html>

数据库的名称是照片。它有一个名为images的表,它有三行,id(每张照片自动增加id),image(存储从计算机上传的图像的名称)和文本(与图像相关的文本)一起显示)。 但是,当我尝试运行程序时,与视频中发生的情况不同,我的localhost看起来像这样: the texts displayed successfully but all the images are broken

我其实非常好奇,因为我只是遵循指示。代码是相同的(除了我没有CSS文件)和数据库是相同的,出了什么问题?我需要更改图像src吗?

更新:每当我上传图片时,都会收到以下警告:

警告:move_uploaded_file(images / bird.jpg):无法打开流:第14行的C:\ k \ htdocs \ index.php中没有此类文件或目录

警告:move_uploaded_file():无法在第14行的C:\ k \ htdocs \ index.php中将'C:\ k \ tmp \ phpE864.tmp'移动到'images / bird.jpg'

2 个答案:

答案 0 :(得分:0)

我认为问题归因于single quotes在线路回复"<img src='images/".$row['image']."'>";,所以请尝试这样做..

while($row = mysqli_fetch_array($result)){
    $img = $row['image'];
    echo "<div id='img_div'>";
    echo "<img src='images/".$img."'>";
    echo"<p>".$row['text']."</p>";
    echo "</div>";
}

答案 1 :(得分:0)

该错误表明在提交表单时可能没有文件上传到临时目录。你应该检查以确保它是。

另外,检查上传错误,以便更好地了解正在发生的事情,以防文件与请求一起发送。

以下是使用以下两个方面更新的代码:

if(isset($_POST['upload'], $_FILES['image'])){
    $target="images/".basename($_FILES['image']['name']);

    $db=mysqli_connect("localhost","root","","photos");

    $image=$_FILES['image']['name'];
    $text=$_POST['text'];

    $sql="INSERT INTO images(image,text)VALUES('$image','$text')";
    mysqli_query($db,$sql);

   if ($_FILES['image']['error'] !== UPLOAD_ERR_OK) {

      if(move_uploaded_file($_FILES['image']['tmp_name'],$target)){
         $msg="Image uploaded succesfully";
      }
      else{
        $msg="There was a problem uploading image";
      }
    } else {
       echo $_FILES['image']['error'];
    }
}