PHP-mySQL:向表中添加删除、编辑按钮
我在表末尾的两个单独的列中添加删除和编辑按钮时遇到问题 下面是我当前用于从数据库中提取信息的代码PHP-mySQL:向表中添加删除、编辑按钮,php,mysql,html-table,Php,Mysql,Html Table,我在表末尾的两个单独的列中添加删除和编辑按钮时遇到问题 下面是我当前用于从数据库中提取信息的代码 <?php echo "<table style='border: solid 1px black;'>"; echo "<tr><th>Asset ID</th><th>Device</th><th>Brand</th><th>Model
<?php
echo "<table style='border: solid 1px black;'>";
echo "<tr><th>Asset ID</th><th>Device</th><th>Brand</th><th>Model</th><th>CPU</th><th>RAM</th><th>Storage</th><th>Screen Size</th><th>Serial Number</th><th>Price</th><th>Last Image</th><th>Comment</th><th>Action</th></tr>";
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current() {
return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
}
function beginChildren() {
echo "<tr>";
}
function endChildren() {
echo "</tr>" . "\n";
}
}
$servername = "localhost";
$username = "";
$password = "";
$dbname = "laptoplease";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM laptoplist");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>
保持简单
<?php
$servername = "localhost";
$username = "hsfcadmin";
$password = "G7qMtJats6bQsRLp";
$dbname = "laptoplease";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->query("SELECT * FROM laptoplist"); // no need to use prepare statement
$laptops = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
<table style="border: solid 1px black;">
<tr>
<th>Asset ID</th>
<th>Device</th>
<th>Brand</th>
<th>Model</th>
<th>CPU</th>
<th>RAM</th>
<th>Storage</th>
<th>Screen Size</th>
<th>Serial Number</th>
<th>Price</th>
<th>Last Image</th>
<th>Comment</th>
<th>Action</th>
</tr>
<?php if ($laptops) : foreach ($laptops as $laptop) : ?>
<tr>
<td style="width:150px; border:1px solid black;"><?= $laptop['asset_id'] ?></td>
<td style="width:150px; border:1px solid black;"><?= $laptop['device'] ?></td>
<td style="width:150px; border:1px solid black;"><?= $laptop['brand'] ?></td>
<td style="width:150px; border:1px solid black;"><?= $laptop['model'] ?></td>
<td style="width:150px; border:1px solid black;"><?= $laptop['cpu'] ?></td>
<td style="width:150px; border:1px solid black;"><?= $laptop['ram'] ?></td>
<td style="width:150px; border:1px solid black;"><?= $laptop['storage'] ?></td>
<td style="width:150px; border:1px solid black;"><?= $laptop['screen_size'] ?></td>
<td style="width:150px; border:1px solid black;"><?= $laptop['serial_number'] ?></td>
<td style="width:150px; border:1px solid black;"><?= $laptop['price'] ?></td>
<td style="width:150px; border:1px solid black;"><?= $laptop['last_image'] ?></td>
<td style="width:150px; border:1px solid black;"><?= $laptop['comment'] ?></td>
<td style="width:150px; border:1px solid black;">
<button>Delete</button>
<button>Edit</button>
</td>
</tr>
<?php endforeach; endif ?>
</table>
资产ID
装置
烙印
模型
中央处理器
皇家音乐学院
保管部
屏幕大小
序列号
价格
最后图像
评论
行动
删除
编辑
我只是根据标题猜测表中列的名称
与其使用内联样式,不如只使用外部样式表。我可以问一下,为什么要尝试使用迭代器进行此操作吗?我以前从未用PHP/mySQL编写过代码,我认为这是正确的方法。