2016-05-12 45 views
-2

我在添加刪除和編輯按鈕在表的末尾兩個單獨的列中有問題。PHP-mySQL:添加刪除,編輯按鈕到表

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

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

請問爲什麼你要爲此使用迭代器? – Mikey

+0

我從來沒有用PHP/mySQL編碼過,並認爲這是正確的方法。 – Razargh

回答

0

保持簡單。

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

我只是猜測基於標題的表中列的名稱。

而不是使用內聯樣式,最好只使用外部樣式表。

相關問題