我有一個while循環顯示我的數據庫列和表中的行。
從PHP刪除MySQL行while循環
我想製作一個按鈕,它可以刪除特定的MySQL行,但不幸的是我不能選擇刪除所有行中的哪一行,因爲我使用「while循環」來顯示我的數據庫內容。
提供更好的理解圖片是我想做的事:
所以,是的,我想是點擊綠色按鈕時 - 發送MySQL查詢,刪除該行(這是在我的DB現有的)。
提供代碼:
<?php
//CONECT TO MYSQL
include 'database.php';
//GET VALUES
$sql = "SELECT * FROM amountcontainer";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
//Table headlines - NOT A PHP
echo "<table class='moneytable'>
<tr>
<th style='background-color: #2d323a; color: white;'>Date</th>
<th style='background-color: #2d323a; color: white;'>Amount</th>
<th style='background-color: #2d323a; color: white;'>Reason</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc())
{
//RED //Make statement of amount red if it has a " - "
if(strpos($row['amount'],'-') !== false)
{
echo
"
<tr>
<th style='font-weight: normal;background-color: #ff9999;' class='row-time'>" . $row['time'] . "</th>
<th style='font-weight: normal;background-color: #ff9999;' class='row-amount'>" . $row['amount'] . "</th>
<th style='font-weight: normal;background-color: #ff9999;' class='row-reason'>" . $row['reason'] . "</th>
</tr>
";
}
//NORMAL //Make statement of amount normal if it doesn't have a " - "
else
{
echo
"
<tr>
<th style='font-weight: normal;' class='row-time'>" . $row['time'] . "</th>
<th style='font-weight: normal;' class='row-amount'>" . $row['amount'] . "</th>
<th style='font-weight: normal;' class='row-reason'>" . $row['reason'] . "</th>
</tr>
";
}
}
echo "</table>";
}
else
{
echo "0 results";
}
您是否有可以參考刪除行的表上的主索引或唯一鍵? – Ohgodwhy
我沒有。我正在考慮做同樣的事情,但不知道如何。 –
[看看這裏,朋友](http://stackoverflow.com/questions/15255304/how-add-unique-key-to-existing-table-with-non-uniques-rows) – Ohgodwhy