简体   繁体   中英

PHP - mySQL: Adding Delete, Edit buttons to Table

I am having issues adding a Delete and Edit button in two separate columns on the end of the table.

Here is the code I am currently using to pull the information from the database.

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

Keep things simple.

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

I am only guessing the name of the columns in your table based off the headers.

Instead of using inline styles, it's better to just use an external stylesheet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM