在php中的html表上添加行的删除按钮

时间:2013-08-16 11:46:05

标签: php html mysql

我试图让它有一个可以按下的删除按钮,它会删除你按下删除按钮的列。我做了大量研究,似乎无法弄明白。

这是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");
?>

3 个答案:

答案 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');
}

答案 1 :(得分:0)

你可以调用JS函数,你可以传递唯一的id。借助此id和Ajax的基本使用,您可以执行删除操作。

答案 2 :(得分: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>