Jump to content

PHP Delete Button HELP


stanlex

Recommended Posts

Hello, 

I recently started learning PHP on my own and started to use mysql and apache as well. I made a basic table in mysql and using some php code i displayed the table in a html table in a browser. Now I'd like to add a delete button beside each row and when clicked, it would delete that row. I am very new to this and I'm just practicing. Could anyone please help me? This is the code I have so far:


<?php

ini_set('display_errors',1); 
error_reporting(E_ALL);

mysql_connect('localhost', 'root', '');

mysql_select_db('testdb');

$result = mysql_query('select * from products');

$numrows = mysql_numrows($result);

//****************************************************************
print "<table border = 3 style = width:400px>";


for($i = 0; $i < $numrows; $i++)
{
$row = mysql_fetch_row($result);

print "<tr>";


foreach($row as $cell)
{

print "<td>";
print $cell;
print "</td>";


}
print "</tr>";


}


print "</table>";


mysql_close();

?>

I also have a delete.php page but I really don't know where to start. I've looked online tutorials and many say different ways. 

Thanks.
stanlex Forum Newbie   Posts: 1 Joined: Wed Mar 26, 2014 1:46 pm
  •  
  •  
 
Link to comment
Share on other sites

Great that you are learning php and that you are showing some real indications of doing the right things.

 

One point - since you are just beginning you must backup a step and learn to use either the mySqli or PDO sql interface, since the mysql one is being eliminated shortly.  I suggest PDO and learning about prepared statements.

 

Your code looks pretty good except:

- make it a practice to check the result of a query call before trying to use the results (such as checking # rows).  It's good practice to utilize the returns that function calls give you when offered so that you don't continue on with invalid results.

 

- for the sake of readability, use indents/tabs to write your code.  It will make your life a lot easier when trying to debug your code.

 

 As for adding a delete button for each row, you will need to understand how forms work and either output one form wrapped around all of your rows with a separate button on each row.  You will have to have a way of knowing which row the button is to be associated with too.  Or you could just have a field where you provide the record number or key and a single button that looks at that key value and then deletes accordingly.  Another good feature of a delete button is to use a simple JS confirm box to give the user a last chance to change his/her mind.

 

Lots to learn yet!

Link to comment
Share on other sites

@ginerjm Great information, I got a lot from that too.

 

Make and use a form something like (Mock code):

<form id="form1" name="form1" method="post" action="page.php">
    <input type="hidden" name="hiddenField" id="hiddenField" value="<id from record here>" />
    <input type="submit" name="id" id="id" value="Delete" />
</form>
Link to comment
Share on other sites

Yes - something like that.  The form would be created for each row of the html table.  And don't forget to include an onclick= attribute on the submit.

 

BTW - it is not necessary to provide both a name= and id= attribute on elements.  Name - yes - since you need it to reference in php.  Id - no - unless you have some JS code that will use it.  Seems that there are many posters who do this and I wonder if they realize that it is not always needed.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.