2013-08-16 29 views
1

我想讓它有這樣一個刪除按鈕,可以按下它並刪除您按下刪除按鈕的列。我做了大量的研究,我似乎無法弄清楚。在php表格中添加刪除按鈕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"); 
?> 
+0

你到底要幹什麼?刪除一行或一列(你提到兩者)?你是否希望它只從輸出表中出現,或者是否想從數據庫中刪除它? – Butt4cak3

回答

0

你可以調用JS函數,你可以通過唯一的ID。藉助此ID以及Ajax的基本用法,您可以執行刪除操作。

0
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> 
+0

有問題作爲在哪裏把回聲「Delete」; –

+0

哦,尚未學習ajax:/ –

+0

你使用標籤獲得什麼問題? –

5
<?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'); 
} 
+0

有一個想法!高超! +1 –