簡體   English   中英

在PHP中的HTML表上添加行的刪除按鈕

[英]Adding delete button for rows on html table in php

我正在嘗試使其具有可以按下的刪除按鈕,並且它會刪除您按下刪除按鈕的列。 我已經進行了廣泛的研究,但似乎無法弄清楚。

這是PHP和HTML:

<?php


$username="xxx";
$password="xxx";
$database="xxx";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");


$query="SELECT * FROM reservation__date ORDER BY reservation_date DESC";
$result = mysql_query ($query) or die(mysql_error());

$num=mysql_numrows($result);
mysql_close();
?>
            <table width="700" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC" id="myTable" class="tablesorter">
  <thead> 
        <tr valign="bottom" bgcolor="#000000">
          <th width="128"><span class="style1b"><strong>Reservation&nbsp;ID</strong></span></th>
          <th width="829" bgcolor="#2E64FE"><span class="style1b"><strong>Reservation&nbsp;Date</strong></span></th>
          <th width="829"><span class="style1b"></span></th>
          <!--  <th width="90"><span class="style1b"><strong>Agent/client</strong></span></th>-->
          </tr>
        </thead> 
<?php

$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"reservation_id");
$f2=mysql_result($result,$i,"reservation_date");
?>
<?php    
$i=0;
while ($i < $num) {
    $f1=mysql_result($result,$i,"reservation_id");
    $f2=mysql_result($result,$i,"reservation_date");
?>
   <tr>
     <td><?echo $f1; ?></td>
     <td><?echo $f2; ?></td>
     <td><a href='delete.php?id=<?php echo $f1; ?>'>del</a></td>
   </tr>
<? } ?>

在delete.php中

$username="xxx";
$password="xxx";
$database="xxx";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "delete from reservation__date where reservation_id=$_GET[id]";
$rs = mysql_query ($query);
if($rs){
  header('Location: yourfile.php');
}

您可以調用JS函數,並可以傳遞唯一的ID。 借助此id和Ajax的基本用法,您可以執行delete操作。

while ($i < $num) {

$f1=mysql_result($result,$i,"reservation_id");
$f2=mysql_result($result,$i,"reservation_date");

echo  "<a href='#' onclick ='delete($f1);'>Delete</a>";
?>

您可以使用腳本功能獲取此ID

<script>
function delete(id){
//Now you can delete the data related to this id form the database using Ajax
}
</script>

暫無
暫無

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

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