Jump to content

delete only the entry with the specified id


php_begins

Recommended Posts

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>";
	}

}

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.