Php 从MySQL插入和显示图像
我正在尝试显示一个存储在MySQL中的图像,但是还没有成功。显然,重复表格标题(img)给了我类似的信息 此外,我想能够添加在网站本身的形象,而不是使用phpmyadmin和插入那里的形象 到目前为止,这是我为Php 从MySQL插入和显示图像,php,mysql,Php,Mysql,我正在尝试显示一个存储在MySQL中的图像,但是还没有成功。显然,重复表格标题(img)给了我类似的信息 此外,我想能够添加在网站本身的形象,而不是使用phpmyadmin和插入那里的形象 到目前为止,这是我为standing.php页面编写的代码 <?php require_once('database.php'); // Get all categories $query = 'SELECT * FROM categories O
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> </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>© <?php echo date("Y"); ?> NBA</p>
</footer> -->
</body>
</html>
上面的代码是team\u list.php
页面,下面的代码是连接到名为add\u 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');
}
?>
上图显示了可以添加或删除团队的页面。用于测试目的
首先,你需要知道图像是否真的存在。假设数据库中有一个类别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']; ?>
并将其替换为:
用于测试目的
首先,你需要知道图像是否真的存在。假设数据库中有一个类别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']; ?>
并将其替换为:
如果希望浏览器正确显示图像,必须将图像放入HTML
标记中。因此phpMyAdmin
没有存储此图像,MYSQL是phpMyAdmin
是一个用PHP编写的工具,它使维护MYSQL数据库变得更容易。如果你不问我,我怎么能做到这一点呢?所以在standing.PHP
页面中,我把图像标记放对了吗?是的,例如,如果您想让浏览器正确显示图像,您必须将图像放入HTML
标记中,而不是将其放入
中。因此phpMyAdmin
没有存储图像,MYSQL是phpMyAdmin
是一个用PHP编写的工具,它使维护MYSQL数据库变得更容易。如果你不问我,我怎么能做到这一点呢?所以在standing.PHP
页面中,我把图像标记放对了吗?是的,例如,我没有把它放在
中,而是用
替换了
而且似乎工作正常。没关系。只是你的问题让人困惑。我教过你,你的图像在数据库中保存为blob。无论如何,祝你好运,亲爱的……所以我所做的就是用
而且似乎工作正常。没关系。只是你的问题让人困惑。我教过你,你的图像在数据库中保存为blob。无论如何,祝你好运,亲爱的。。。。