Search code examples
phpbuttondelete-row

I want to make a delete button using php for my website


I want to write a "delete" button in php. So far I have echoed out the form needed with a delete button for every row of data. But now I'm trying to make the delete button work so that whenever you press the delete button it only deletes that individual row of data.

Please help me. I am a beginner at this. Thanks.

echo '<form>';
              echo "<tr>
                  <th>Contact Person</th>
                      <th>E-mail / MSN address</th>
                      <th>Mobile</th>
                      <th>Direct Line</th>
                      <th>Fax</th>
                      <th>Delete</th>
                      </tr>";
              while($duty = mysql_fetch_array($result)){
              echo '<tr><td>';
              echo $duty[3].'</td>';
              echo '<td><input id='.email.' value='.$duty[4].'></td>';
              echo '<td>'.$duty[5].'</td>';
              echo '<td>'.$duty[6].'</td>';
              echo '<td>'.$duty[7].'</td>';
              echo '<td>'.'<input type='.submit.'value='.Delete.'>'.'</td></tr>';
              echo '</form></tr>';

This is what I have at the moment. Please tell me how to write this. Thank you.


Solution

    • first you need to add some Ids to identify which record should be deleted. (usually the primary key of the target table)

    • Then store that Id in the form using value attributes in a checkbox or a hidden text input field.

    • Finally, when user click on submit, the browser will send a POST request to the server that you have to detect and then process SQL DELETE request according to Ids recieved in the post argument.

    Here is a nice code sample as reference for your work http://www.softwareforeducation.com/lamp/phpMySQL/deleteData.php . It is using the "traditional" checkbox per record rather than a button per record as suggested in your sample.