phpgoal Posted July 5, 2013 Share Posted July 5, 2013 Hello, Below code is working fine except deleting rows. 1. My code simply search mysql to select data. 2. Once data is found, it displays row by row. Working fine so far. 3. I am trying to add a delete button next to each row. When i click Delete only that row gets deleted. Would you provide any help? Thanks. echo "<table border='1'> <tr> <th>Date</th> <th>Income</th> <th>Expense</th> <th>MS</th> <th>MK</th><th>Delete</th> </tr>";while($row = mysql_fetch_array($result)){$myear = $row['monthyear'];$income = $row['income'];$expense = $row['expense'];$ms = $row['ms'];$mk = $row['mk']; echo "<tr>"; echo "<td>" . $row['monthyear'] . "</td>"; echo "<td>" . $row['income'] . "</td>"; echo "<td>" . $row['expense'] . "</td>"; echo "<td>" . $row['ms'] . "</td>"; echo "<td>" . $row['mk'] . "</td>"; echo "<td>" . "<form name=\"kk\" method=\"post\" action=\"\"> <input type=\"submit\" value=\"Delete\" /></form>". "</td>"; echo "</tr>"; } echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/279892-delete-row-in-php/ Share on other sites More sharing options...
_EmilsM Posted July 5, 2013 Share Posted July 5, 2013 (edited) I'd propably create a link with ?id=$row['id'](or your unique row) and just then in that link delete it.. or just redirect back with a get param to the same file and if isset.. EDIT. or.. you could use js + ajax to send data to an independent file to delete it Edited July 5, 2013 by _EmilsM Quote Link to comment https://forums.phpfreaks.com/topic/279892-delete-row-in-php/#findComment-1439527 Share on other sites More sharing options...
phpgoal Posted July 5, 2013 Author Share Posted July 5, 2013 Thank you. I will give a try. Quote Link to comment https://forums.phpfreaks.com/topic/279892-delete-row-in-php/#findComment-1439542 Share on other sites More sharing options...
thara Posted July 5, 2013 Share Posted July 5, 2013 Use hidden form input to pass your unique row ID which relevant to your deleted item with form submitting. Something like this <input type="hidden" name="delete-item" value="your id" /> Here you need to select your unique id from MySql table along with other table columns. Then your code should be something similar to this. NOTE: I assumed your unique row id as item_id (It should be change with your real ID) echo "<table border='1'> <tr> <th>Date</th> <th>Income</th> <th>Expense</th> <th>MS</th> <th>MK</th> <th>Delete</th> </tr>"; while($row = mysql_fetch_array($result)){ $myear = $row['monthyear']; $income = $row['income']; $expense = $row['expense']; $ms = $row['ms']; $mk = $row['mk']; $item_id = $row['item_id']; echo "<tr> <td>" . $row['monthyear'] . "</td> <td>" . $row['income'] . "</td> <td>" . $row['expense'] . "</td> <td>" . $row['ms'] . "</td> <td>" . $row['mk'] . "</td> <td> <form name='kk' method='post' action=''> <input type='submit' value='Delete' /> <input type='hidden' value='" . $item_id . "' name='item_id' /> </form> </td> </tr>"; } echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/279892-delete-row-in-php/#findComment-1439547 Share on other sites More sharing options...
AbraCadaver Posted July 5, 2013 Share Posted July 5, 2013 Don't use GET except to retrieve data. Use POST to modify data. You can wrap your row output in the form and use a checkbox array with the value of the row unique id: <form method="post"> <?php if(isset($_POST['submit']) && isset($_POST['ids'])) { $ids = implode(',', $_POST['ids']); //DELETE FROM table_name WHERE id IN ($ids) } while($row = mysql_fetch_array($result)){ echo 'row stuff... <input type="checkbox" name="ids[]" value="' . $row['id'] . '"><br />'; } ?> <input type="submit" name="submit" value="submit"> </form> There needs to be some security / sanitization added, this is just an example. Look at mysqli or PDO. Quote Link to comment https://forums.phpfreaks.com/topic/279892-delete-row-in-php/#findComment-1439548 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.