我正在尝试显示已存储在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> </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_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> </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>© <?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');
}
?>
上图显示了您可以在其中添加或删除团队的页面。
答案 0 :(得分:0)
用于测试目的
首先,您需要知道图像是否确实存在。假设您的数据库中有一个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" />
让我知道您是否仍然需要帮助