stanlex Posted March 26, 2014 Share Posted March 26, 2014 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:<?phpini_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 Quote Link to comment https://forums.phpfreaks.com/topic/287294-php-delete-button-help/ Share on other sites More sharing options...
ginerjm Posted March 26, 2014 Share Posted March 26, 2014 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! Quote Link to comment https://forums.phpfreaks.com/topic/287294-php-delete-button-help/#findComment-1473960 Share on other sites More sharing options...
Ansego Posted March 26, 2014 Share Posted March 26, 2014 @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> Quote Link to comment https://forums.phpfreaks.com/topic/287294-php-delete-button-help/#findComment-1473963 Share on other sites More sharing options...
ginerjm Posted March 26, 2014 Share Posted March 26, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287294-php-delete-button-help/#findComment-1473967 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.