【发布时间】:2017-11-02 19:44:36
【问题描述】:
我无法在 html 表中显示我的数据库内容。 该表已创建,但只有标题,我确实找到了另一个有相同问题的线程,但对我来说似乎是另一个问题。
<!DOCTYPE html>
<html>
<head>
<?php
require_once 'db.php';
if(isset($_POST['cyanxerox'])){$id = 1; }
if(isset($_POST['magentaxerox'])){$id = 2;}
if(isset($_POST['blackxerox'])){$id = 3;}
if(isset($_POST['yellowxerox'])){$id = 4;}
if(isset($id)){
$sth = $conn->prepare("UPDATE supplies SET quantity = quantity + 1 WHERE Id=".$id);
$sth->execute();
header('Location: index.php');
die("Posted, now redirecting");
}
#this is the part that is not working
$result = $conn->prepare('SELECT id, name_of_supply, quantity, description from supplies');
$result->execute();
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$id= $row['id'];
$name_of_supply = $row['name_of_supply'];
$quantity = $row['quantity'];
$description = $row['description'];
}
?>
<title>Homepage</title>
<link rel="stylesheet" type="text/css" href="style/main.css">
</head>
<body>
<h1>ICT Support Printer Supplies Inventory</h1>
<form method="POST" action="index.php">
<input type="submit" name="cyanxerox" value="Cyan Xerox"/>
</form>
<form method="POST" action="index.php">
<input type="submit" name="magentaxerox" value="Magenta Xerox"/>
</form>
<form method="POST" action="index.php">
<input type="submit" name="blackxerox" value="Black Xerox"/>
</form>
<form method="POST" action="index.php">
<input type="submit" name="yellowxerox" value="Yellow Xerox"/>
</form>
//this is the part that is not working
<table>
<thread>
<th>
<th>ID</th>
<th>Name</th>
<th>Number in Stock</th>
<th>Description</th>
</th>
<tbody>
<tr>
<td><? echo $id; ?></td>
<td><? echo $name_of_supply; ?></td>
<td><? echo $quantity; ?></td>
<td><? echo $description; }?></td>
</tr>
<?#php endwhile ?>
</tbody>
</thread>
</table>
</body>
编辑:我将代码作为整个文件添加, 也可以说sql查询运行良好。
【问题讨论】:
-
您的表格 html 和您的数据库记录获取代码在两个单独的文件中还是在同一个文件中?
-
我已经编辑了帖子以包含整个 php 文件。抱歉,如果编码不好,我是菜鸟:P @AlivetoDie
-
在你的while循环中,你只是给变量赋值,就是这样。 while循环结束后,变量会消失,因为它们超出了while循环的范围。
-
你不需要那么多表格。
-
你在这里遇到了一个范围问题,应该使用错误报告,你会看到这里发生了什么。