1

I am trying to add a 'delete' button on my item table and have a delete button to delete the item and the information about the both item and the seller. I have the delete button on the table but I cannot figure out how to process that button when it is clicked. Please help! Thank you in advance!!

    <?php
    require 'authentication.inc';

    // connect to the server
    $connection = sqlsrv_connect( $hostName, $connectionInfo )
        or die("ERROR: selecting database server failed");

    // prepare SQL query
    $UserID = $_SESSION['userID'];
    $query = "SELECT * FROM ITEM WHERE userID= '$UserID'";


    // Execute SQL query
    $query_result = sqlsrv_query($connection, $query)
        or die( "ERROR: Query is wrong");

    // Output query results: HTML table
    echo "<table border=1>";
    echo "<tr>";

    // fetch attribute names
    foreach( sqlsrv_field_metadata($query_result) as $fieldMetadata)
        echo "<th>".$fieldMetadata['Name']."</th>";
    echo "</tr>";

    // fetch table records
    while ($line = sqlsrv_fetch_array($query_result, SQLSRV_FETCH_ASSOC)) {
        echo "<tr>\n";

        foreach ($line as $cell) {
            echo "<td> $cell </td>";
        }
        echo "<td></td>";
        echo "</tr>\n";
    }
    echo "</table>";

    // close the connection with database
    sqlsrv_close($connection);
?>
  • You need to [give the delete button a url](http://stackoverflow.com/questions/2906582/how-to-create-an-html-button-that-acts-like-a-link) which tells the server to delete the item. – Michael Plotke Nov 30 '15 at 16:56
  • But I am trying to delete the attributes from my database when I hit the delete button. For example deleting the whole row about the item including name, description, seller id and etc. – Yusuf Yılmaz Nov 30 '15 at 16:59
  • Indeed, without reloading the page you would need to use AJAX, but the simplest method is calling a URL where the PHP runs some SQL to delete the data, then sends you immediately back to the original page. It looks like a page refresh, and the mission is accomplished. – Michael Plotke Nov 30 '15 at 17:05

1 Answers1

0

To add a delete feature you need two things, a button and secondly the processing of said button. I am not sure what your unique value is in the table, so I am using this fictitious key: itemID. Update with whatever your unique column name is.

Replace your table loop with:

<table border=1>
    <tr>
<?php
    // fetch attribute names
    foreach( sqlsrv_field_metadata($query_result) as $fieldMetadata)
        echo "<th>".$fieldMetadata['Name']."</th>"; ?>
        </tr>
<?php
    // fetch table records
    while ($line = sqlsrv_fetch_array($query_result, SQLSRV_FETCH_ASSOC)) { ?>
        <tr>
<?php   foreach ($line as $cell) {
            echo "<td> $cell </td>";
        } ?>
            <td>
                <form method="post">
                    <input type="hidden" name="itemID" value="<?php echo $line['itemID']; ?>" />
                    <input type="submit" name="action" value="DELETE" />
                </form>
            </td>
        </tr>
<?php } ?>
</table>

In the processing portion at the top, add processing:

if(!empty($_POST['action']) && ($_POST['action'] == 'DELETE')) {
        // Do some sort of validation here
        if(is_numeric($_POST['itemID']))
            sqlsrv_query($connection, "delete from ITEM where itemID = '".$_POST['itemID']."'");
    }
Rasclatt
  • 12,498
  • 3
  • 25
  • 33