使用php进行显示时,我的图像被破坏了

时间:2018-01-06 09:25:51

标签: php html

当我显示餐厅的图像,但图像被破坏。任何人帮我解决或找出问题?我试图谷歌搜索或寻找教程但我发现有几种方式来存储图像,要么存储文件路径到数据库或将图像存储到数据库,在我的情况下是存储数据库的文件路径。 这是我数据库中的数据。图像的类型是blob。enter image description here enter image description here

这是我用于显示的HTML代码

<?php
          $sql = "SELECT * FROM restaurant";
            $result = $conn->query($sql) or die($conn->error);
          while ($row = $result->fetch_assoc()){
            <tr>
                        <td>{$row['ID']}</td>
                        <td>";echo "<img src='".$row['Logo']."' />";echo "</td>
                        <td>{$row['Name']}</td>
                        <td>{$row['ContactNumber']}</td>
                        <td>{$row['Address']}</td>
                        <td>{$row['CuisineType']}</td> 
                        <td>{$row['SupportArea']}</td> 
            </tr>\n";
            }
?>

这是添加餐厅的PHP代码

<?php
    include 'db.php';
            //define other variables with submitted values from $_POST
            $name = $_POST['restaurant_name'];
            $contactnum = $_POST['restaurant_contactnum'];
            $address = $_POST['restaurant_address'];
            $cuisinetype = $_POST['cuisinetype'];
            $checkbox1 = $_POST['area'];


            $allowedExts = array("png","jpg","jpeg"); /* ACCEPTED FILE FORMAT */
            $extension = pathinfo($filename, PATHINFO_EXTENSION); /* GET THE FILE EXTENSION */
            $extension = strtolower($extension); /* LOWER THE STRINGS OF THE EXTENSION */

            $chk=""; 
            foreach($checkbox1 as $chk1) 
            { 
            $chk.= $chk1.","; 
            }


         if(in_array($extension,$allowedExts)){ /* IF FILE IS INDEED AN IMAGE */

            $photo="restaurantlogo/" . $_FILES["restaurant_logo"]["name"];
            }

                //insert user data into database
                $sql = "INSERT INTO restaurant (Logo, Name, Password, ContactNumber, Address, CuisineType, SupportArea ) VALUES ('$photo', '$name', '$password', '$contactnum', '$address', '$cuisinetype','$chk')";

            $insert = mysqli_query($conn, $sql) or die(mysqli_error($conn));

                //check if mysql query is successful
                if ($sql){
                    //redirect the user to welcome.php
                    header("Location: ../restaurant-add.php");
                }

2 个答案:

答案 0 :(得分:1)

问题出在您的PHP输出中。您在多个地方遗漏了echo,我与"交换了',因此HTML <img>'s src标记包含在" "中。

以下是更正后的代码:

<?php
      $sql = "SELECT * FROM restaurant";
      $result = $conn->query($sql) or die($conn->error);
      while ($row = $result->fetch_assoc())
      {
         echo '<tr>
            <td>'.$row['ID'].'</td>
            <td><img src="'.$row['Logo'].'" /></td>
            <td>'.$row['Name'].'</td>
            <td>'.$row['ContactNumber'].'</td>
            <td>'.$row['Address'].'</td>
            <td>'.$row['CuisineType'].'</td> 
            <td>'.$row['SupportArea'].'</td> 
        </tr>";
    }
?>

答案 1 :(得分:-1)

单击F12打开检查元素,检查您是否获得了有效的图像src路径。

如果有效,请尝试在新标签中打开并查看是否显示在此处。显然你会在那里找到你的错误