我无法在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查询运行正常。
答案 0 :(得分:1)
代码中的问题:
<thread>
而不是<thead>
。<thead>
必须在<tbody>
之前关闭。<thead>
来<tr>
后<th>
。td
s <td><?php echo $id; ?></td>
内写“php”的内容:<?#php endwhile ?>
。}
被错误地放置。完全删除它。<td><? echo $description; }?></td>
中的Undefined index
位置错误。删除它。echo
的{{1}}次通知。检查isset
。我的代码提案:
name
属性。每个按钮的相应供应标识为value
属性。<head>
index.php
标记中的表单提交或数据包提取操作。放在页面的开头。Location
标头。不好的主意。fetchAll()
而不是while
循环fetch()
。<meta>
标记放在您的html / php页面的<head>
标记内。<?php
// Create the db connection.
$conn = new PDO(
'mysql:host=localhost;port=3306;dbname=tests;charset=utf8'
, 'user'
, 'pass'
, array(
// Important! Research on the subject "PDO::ERRMODE_EXCEPTION".
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => FALSE,
PDO::ATTR_PERSISTENT => TRUE
)
);
<?php
require_once 'db.php';
/*
* =========================
* Activate error reporting.
* =========================
*/
error_reporting(E_ALL);
// Set to 0 on the live server!
ini_set('display_errors', 1);
/*
* ====================================
* Run operations upon form submission.
* ====================================
*/
if (isset($_POST['submitButton'])) {
$id = $_POST['submitButton'];
/*
* ======================
* Update quantity by id.
* ======================
*/
$sth = $conn->prepare('UPDATE supplies SET quantity = quantity + 1 WHERE Id = :id');
$sth->execute(array(
'id' => $id
));
/*
* ===============
* Fetch supplies.
* ===============
*/
$sth = $conn->prepare('SELECT id, name_of_supply, quantity, description from supplies');
$sth->execute();
$supplies = $sth->fetchAll(PDO::FETCH_ASSOC);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
<meta charset="UTF-8" />
<!-- The above 3 meta tags must come first in the head -->
<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">
<button type="submit" name="submitButton" value="1">Cyan Xerox</button>
<button type="submit" name="submitButton" value="2">Magenta Xerox</button>
<button type="submit" name="submitButton" value="3">Black Xerox</button>
<button type="submit" name="submitButton" value="4">Yellow Xerox</button>
</form>
<br/><br/>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Number in Stock</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
if (isset($supplies)) {
foreach ($supplies as $supply) {
$id = $supply['id'];
$nameOfSupply = $supply['name_of_supply'];
$quantity = $supply['quantity'];
$description = $supply['description'];
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $nameOfSupply; ?></td>
<td><?php echo $quantity; ?></td>
<td><?php echo $description; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</body>
</html>
答案 1 :(得分:0)
最佳做法是将PHP和HTML分开,但暂时忽略这一点,问题是你的循环和输出没有连接。
您有几个问题,您应该查看一些HTML结构,并已经过更正和评论。
header
重定向,因此标题已经发送。这应该移动到文件的顶部(或者最好是另一个文件)。<thead>
封装了表格的头部,而不是<thread>
<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); // This all but defeats the point of preparing statements, use bound parameters
$sth->execute();
header('Location: index.php'); // This won't work since you've already sent headers with the HTML code above this...
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>
<thead> <!-- thead not thread -->
<tr> <!-- should be a row, not th -->
<th>ID</th>
<th>Name</th>
<th>Number in Stock</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
while ($row = $result->fetch(PDO::FETCH_ASSOC)):
$id= $row['id'];
$name_of_supply = $row['name_of_supply'];
$quantity = $row['quantity'];
$description = $row['description'];
<tr>
<td><? echo $id; ?></td>
<td><? echo $name_of_supply; ?></td>
<td><? echo $quantity; ?></td>
<td><? echo $description; }?></td>
</tr>
<?php endwhile; // Table loop ?>
</tbody>
</table>
</body>
</html> <!-- close your HTML tag -->