簡體   English   中英

PHP - mySQL:向表中添加刪除、編輯按鈕

[英]PHP - mySQL: Adding Delete, Edit buttons to Table

我在表格末尾的兩個單獨列中添加刪除和編輯按鈕時遇到問題。

這是我目前用於從數據庫中提取信息的代碼。

        <?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>

我只是根據標題猜測表中列的名稱。

與其使用內聯樣式,不如只使用外部樣式表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM