Add Delete Button to PHP results table

95,642

Solution 1

You have to pass variable in delete link. You must have to pass <?php echo $contact['name']; ?> name value in hidden field or pass this value in URL

Replace

<td class="contact-delete">
      <form action='delete.php' method="post">
      <input type="hidden" name="name" value="">
      <input type="submit" name="submit" value="Delete">
      </form>
</td>

With

<td class="contact-delete">
    <form action='delete.php?name="<?php echo $contact['name']; ?>"' method="post">
        <input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
        <input type="submit" name="submit" value="Delete">
    </form>
</td>

Solution 2

USe javascript

<input name="Submit2" type="button" class="button" onclick="javascript:location.href='delete.php?id=<?php echo $your_id;?>';" value="&laquo; Back" />

and in delet.php

$id=$_GET['id'];

and put $id in your sql statement.

Share:
95,642
Lucero79
Author by

Lucero79

I'm a Windows Client and Cloud Architect from Azure to O365 and everything in between.

Updated on July 17, 2022

Comments

  • Lucero79
    Lucero79 almost 2 years

    I have outputted the results of a MySQL table to an HTML table. In the last column, I want to add a delete option which calls another form and deletes the user. I can't seem to get it to work though.

    This is my code for the results page:

    <?php
    
        $contacts = mysql_query("
            SELECT * FROM contacts ORDER BY ID ASC") or die( mysql_error() );
    
        // If results
        if( mysql_num_rows( $contacts ) > 0 )
        ?>
    
        <table id="contact-list">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Telephone</th>
                    <th>Address</th>
      <th>Delete</th>
                </tr>
            </thead>
            <tbody>
    
            <?php while( $contact = mysql_fetch_array( $contacts ) ) : ?>
    
    
    
                <tr>
                    <td class="contact-name"><?php echo $contact['name']; ?></td>
                    <td class="contact-email"><?php echo $contact['email']; ?></td>
                    <td class="contact-telephone"><?php echo $contact['telephone']; ?></td>
                    <td class="contact-address"><?php echo $contact['address']; ?></td>
                    <td class="contact-delete"><form action='delete.php' method="post">
    <input type="hidden" name="name" value="">
    <input type="submit" name="submit" value="Delete">
    </form></td>                
                </tr>
    
            <?php endwhile; ?>
    
            </tbody>
        </table>
    

    and, this is my delete.php script

    <?php
    
    //Define the query
    $query = "DELETE FROM contacts WHERE name={$_POST['name']} LIMIT 1";
    
    //sends the query to delete the entry
    mysql_query ($query);
    
    if (mysql_affected_rows() == 1) { 
    //if it updated
    ?>
    
                <strong>Contact Has Been Deleted</strong><br /><br />
    
    <?php
     } else { 
    //if it failed
    ?>
    
                <strong>Deletion Failed</strong><br /><br />
    
    
    <?php
    } 
    ?>
    

    Pretty sure I'm just missing something, but I can't figure out what that is :(