简体   繁体   中英

Adding delete button for rows on html table in php

I am trying to make it such that there is a delete button that can be pressed and it deletes the column where you pushed the delete button. I have done extensive research and I can't seem to figure it out.

Here is the PHP and 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>
<? } ?>

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

You can call a JS function with that you can pass the unique id. And with the help of this id and the basic use of Ajax, you can perform the delete operation .

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

and You can get this id with script function

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM