【问题标题】:Inserting and Displaying image from MySQL从 MySQL 插入和显示图像
【发布时间】:2019-02-24 14:23:48
【问题描述】:

我正在尝试显示已存储在 MySQL 中但尚未成功的图像。显然呼应表头(img)给了我这样的东西

此外,我希望能够在网站本身中添加图像,而不是使用 phpmyadmin 并在那里插入图像。

到目前为止,这是我为 standing.php 页面提供的代码

<?php
    require_once('database.php');

    // Get all categories
    $query = 'SELECT * FROM categories
              ORDER BY categoryID';
    $statement = $db->prepare($query);
    $statement->execute();
    $teams = $statement->fetchAll();
    $statement->closeCursor();
?>
<!DOCTYPE html>
<html>

<!-- the head section -->
<head>
    <title>NBA</title>
    <link rel="stylesheet" type="text/css" href="css/index.css">
    <link rel="shortcut icon" type="image/png" href="images/favicon.ico"/>

</head>

<!-- the body section -->

<body>
    <main id="standingListMain">

    <h1 id="addCategoryh1">Team Standings</h1>
    <table id="standingListTable">
        <tr>
            <th>Team</th>
            <th>&nbsp;</th>
        </tr>
        <?php foreach ($teams as $team) : ?>
        <tr>
            <td><?php echo $team['categoryID']; ?></td>
            <td>
              <?php echo $team['categoryName']; ?>
              <?php echo $team['img']; ?>
            </td>
        </tr>
        <?php endforeach; ?>
    </table>
    <br>

    </main>
    <!-- <footer id="standingListFooter">
        <p>&copy; <?php echo date("Y"); ?> NBA</p>
    </footer> -->
</body>
</html>

基本上,用户可以从team_list.php 页面添加或删除团队并在排名页面上查看它

<?php
    require_once('../Model/database.php');

    // Get all categories
    $query = 'SELECT * FROM categories
              ORDER BY categoryID';
    $statement = $db->prepare($query);
    $statement->execute();
    $teams = $statement->fetchAll();
    $statement->closeCursor();
?>
<!DOCTYPE html>
<html>

<!-- the head section -->
<head>
    <title>NBA</title>
    <link rel="stylesheet" type="text/css" href="../css/index.css">
    <link rel="shortcut icon" type="image/png" href="images/favicon.ico"/>

</head>

<!-- the body section -->

<body>
    <main>
    <h1 id="addCategoryh1">Teams</h1>
    <table id="categoryListTable">
        <tr>
            <th>Name</th>
            <th>&nbsp;</th>
        </tr>
        <?php foreach ($teams as $team) : ?>
        <tr>
            <td><?php echo $team['categoryName']; ?></td>
            <td>
                <form action="delete_team.php" method="post"
                      id="delete_product_form">
                    <input type="hidden" name="team_id"
                           value="<?php echo $team['categoryID']; ?>">
                    <input id="deleteCategoryList" type="submit" value="Delete">
                </form>
            </td>
        </tr>
        <?php endforeach; ?>
    </table>
    <br>

    <h2 id="add_category_h2">Add Team</h2>
    <form action="add_team.php" method="post"
          id="add_category_form">

        <label>Name:</label>
        <input type="input" name="name">
        <input id="add_category_button" type="submit" value="Add">
    </form>
    <br>
    <p><a href="../index.php">View Team List</a></p>

    </main>
    <footer id="categoryListFooter">
        <p>&copy; <?php echo date("Y"); ?> NBA</p>
    </footer>
</body>
</html>

上面的代码是team_list.php页面,下面是连接到名为add_team.php的数据库的代码

<?php
// Get the team data
$name = filter_input(INPUT_POST, 'name');

// Validate inputs
if ($name == null) {
    $error = "Invalid team data. Check all fields and try again.";
    include('../Error/error.php');
} else {
    require_once('../Model/database.php');

    // Add the product to the database
    $query = 'INSERT INTO categories (categoryName)
              VALUES (:team_name)';
    $statement = $db->prepare($query);
    $statement->bindValue(':team_name', $name);
    $statement->execute();
    $statement->closeCursor();

    // Display the team List page
    include('team_list.php');
}
?>

上图显示了您可以添加或删除团队的页面。

【问题讨论】:

  • 如果您希望浏览器正确显示图像,您必须将图像放入 HTML &lt;img....&gt; 标签中
  • 另外phpMyAdmin 不存储这个,MYSQL 是。 phpMyAdmin 是一个用 PHP 编写的工具,使维护 MYSQL 数据库变得更容易
  • 如果你不问我该怎么做
  • 那么在standing.php 页面中,我把图片标签放对了吗?
  • 是的,而不是将其放在 &lt;td&gt; 中,例如 &lt;td&gt; &lt;img .......&gt;&lt;/td&gt;

标签: php mysql


【解决方案1】:

用于测试目的

首先您需要知道图像是否真的存在。假设您的数据库中有一个类别 ID 为 1 的图像。因此创建另一个文件,例如“image.php”。

(请确保此代码正确运行。我尚未对其进行测试,但它应该适合您)。

image.php

<?php 

 require_once('database.php');

    // Get all categories
    $query = "SELECT img FROM categories where categoryID=1";
    $statement = $db->prepare($query);
    $statement->execute();
    $num = $statement->rowCount();
 
if( $num ){

    $teams = $statement->fetchAll();
   
    // Ensure to specify header with content type,
    // you can do header("Content-type: image/jpg"); for jpg,
    // header("Content-type: image/gif"); for gif, etc.

    header("Content-type: image/png");
    
    //display the image file
    print $teams['img'];
    exit;
}else{
 //echo no image found with that Category Id.
   
}

?>

然后在你的“standing.php”中,删除这段代码:

  <?php echo $team['img']; ?>

并将其替换为:

<img src="image.php?id=1" />

【讨论】:

  • 所以我所做的就是将&lt;?php echo $team['img']; ?&gt; 替换为&lt;?php echo '&lt;img id="teamImage" src="data:image/jpeg;base64,'.base64_encode( $team['img'] ).'"/&gt;'; ?&gt;,它似乎工作得很好。
  • 没关系。只是您的问题令人困惑。我教过您的图像在数据库中保存为 blob。无论如何,亲爱的,祝你好运......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-16
  • 2013-01-16
  • 2014-05-15
  • 2020-03-28
  • 2017-09-28
  • 1970-01-01
相关资源
最近更新 更多