Jump to content

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

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.