php_begins Posted February 26, 2012 Share Posted February 26, 2012 I have a code where i can edit or delete certain details from the database. Right now, if the user clicks on the edit button it takes him edit page where he can edit the details. But, I am not able to Incorporate a Delete button such that, when the user clicks on a delete button, it should ask for a confirmation box. If the user clicks YES, then do the following: DELETE from emp WHERE emp_id='$emp_id'; When there are multiple entries and I click on delete it deletes everything from the database. how can i make it to delete only the entry that is besides the delete button? if(mysql_num_rows($emp_query) > 0){ echo "<table border='1'>"; echo "<th>Employee Id </th>"; echo "<th>Employee Name </th>"; while($get_emp = mysql_fetch_assoc($emp_query)){ $emp_id = $get_emp['emp_id']; $emp_name = $get_emp['first_name']." ".$get_emp['last_name']; echo "<tr>"; echo "<td width='100'>"; echo $emp_id; echo "</td>"; echo "<td width='400'>"; echo $emp_name; $edit_path = 'edit_employee.php?id='.$emp_id; ?> <INPUT TYPE="button" style="display:inline;" value="VIEW / EDIT" onClick="location.href='<?php echo $edit_path; ?>'"> <form style='margin: 0; padding: 0; display:inline;' method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" onSubmit="return confirm('Are you sure this is correct?');"> <input style='display:inline;' name="delbutton" type="submit" value="DELETE"> <?php if(isset($_POST['delbutton'])){ $del_emp = mysql_query("DELETE from employee WHERE emp_id = '$emp_id'") or die(mysql_error()); //header('Location:view_employee.php'); } echo '</form>'; echo "</td>"; echo "</tr>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/257794-delete-only-the-entry-with-the-specified-id/ Share on other sites More sharing options...
blacknight Posted February 26, 2012 Share Posted February 26, 2012 i wouldent put your post script in the while loop place it at the top of your screen and make a hidden input for the emp_id value and then you press delete it will only post 1 value because once you submit then refresh the post is still set and will delete your table Quote Link to comment https://forums.phpfreaks.com/topic/257794-delete-only-the-entry-with-the-specified-id/#findComment-1321311 Share on other sites More sharing options...
php_begins Posted February 26, 2012 Author Share Posted February 26, 2012 Thanks that worked! Quote Link to comment https://forums.phpfreaks.com/topic/257794-delete-only-the-entry-with-the-specified-id/#findComment-1321319 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.