Delete row using checkbox

hey guys so I’m trying to delete a row from table which is connected to database using checkbox(and it wouldnt matter if its just one row or multiple rows) but it is not working, as in nothing happens. it doesn’t delete, no errors or warnings appear just a refresh.


<form class="button-wraps" method="post" action="contact_data.php">
	<a href="add_contact.php"><input type="button" id="add_contact" name="add_contact" value="Add Contact"/></a>
	<input type="submit" id="del_contact" name="del_contact" value="Delete Contact"/>
</form>

<form method="post" action="contact_data.php">
<table class="contact-list-table">
	<thead>
			<tr>
				<th >Salutation</th>
				<th>Name</th>
				<th>House Address</th>
				<th>Email Address</th>
				<th>Telephone No.</th>
				<th>Office No.</th>
			</tr>
		</thead>
		<tbody>
			<?php
						require "connection.php";
						
						$check = mysql_query("SELECT * FROM contact") or die(mysql_error());
						$count=mysql_num_rows($check);
						if($count > 0)
						{
							while($rows = mysql_fetch_array($check))
							{
								$salutation = $rows['salutation'];
								$firstname = $rows['fname'];
								$lastname = $rows['lname'];
								$houseno = $rows['houseno'];
								$streetname = $rows['streetname'];
								$postcode = $rows['postcode'];
								$city = $rows['city'];
								$state = $rows['state'];
								$country = $rows['country'];
								$tel = $rows['tel'];
								$off = $rows['off'];
								$email = $rows['email'];
								$id = $rows['contact_id'];
								
								echo
								"<tr>
									<td ><input type='checkbox' name='check[]' class='check' value='$id'>$salutation</td>
									<td>$firstname $lastname</td>
									<td>$houseno $streetname, $postcode, $city, $state, $country</td>
									<td>$email</td>
									<td>$tel</td>
									<td>$off</td>
								</tr>";
							}
						}	
						else
							{
								echo
								"<tr>
									<td colspan='6'>Contact Database is empty.</td>
								</tr>";
							}	
							?>
		</tbody>
<?php
	if (isset($_POST['del_contact']) && isset($_POST['check']))
	{
		foreach($_POST['check'] as $del_id)
		{
			$del_id = (int)$del_id;
			$sql = "DELETE FROM contact WHERE contact_id = $del_id";
			mysql_query($sql);
		}
	}
?>
</table>
</form>

‘del_contact’ is disabled unless a checkbox is checked.

any help is much appreciated.

You’ve got two different forms going on here. Make it one form by removing these lines.

</form>

<form method="post" action="contact_data.php">

ah… didn’t think about that. thank you very much drummin :slight_smile: one more thing how do i refresh the page after a deletion? i tried:


header("Location : contact_data.php");

after

$sql = "DELETE FROM contact WHERE contact_id = $del_id"; mysql_query($sql);

but i got this :

Warning: Cannot modify header information - headers already sent by (output started at D:\Xampp\htdocs\EMS2\contact_data.php:87) in D:\Xampp\htdocs\EMS2\contact_data.php on line 106

thanks for the help! :slight_smile:

its okay i got it! i used:


if($sql)
{
  echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=contact_data.php\\">";
}

anyways, thanks for noticing the form thing drummin. and my problem is solved!(for this one anyway haha)

ps: is there a way to mark this question as solved?

The bulk of your php should always be done before anything is sent to the browser. If the processing was done before your opening <html> tag, you would be able to use headers.

Could you edit the thread title (by editing your first post, I haven’t tried it) and add “Solved” on the start or end of the title? Obviously don’t delete the title.