Delete using check box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jamrop
Forum Commoner
Posts: 80
Joined: Fri May 16, 2003 5:38 pm

Delete using check box

Post by jamrop »

Hey , I am trying to delete information from mysql table using check boxs. If i tick the first record and next and next etc it deletes them, but if i dont tick the first record and do e.g the 3rd record, it does not delete it.
Here is the code for listing the records with check boxs

Code: Select all

<?php
<form name="delete" method="post" action="delmsg.php">
<input type="Submit" name="submit" value="Delete">
<?php $sql = "SELECT * FROM contact where type = 'msg'" ; 
$rs=mysql_query($sql,$db);
							while ($r = mysql_fetch_array($rs))
								{
									$msg=$r["msg"];
									$name=$r["name"];
									$email=$r["email"];
									$comments=$r["comments"];
									$date = strtotime($r["date"]); 
    								$date = date("F jS  y", $date); ?>
<?php echo $name ?>
<input type="Checkbox" name="msg[]" value="<? echo $msg; ?>">
</form>

<?php 
}

?>
I have missed out database connection and some echo

Here is the code for deleteing the information from database

Code: Select all

<?php


for ($x=0; $x<sizeof($msg); $x++)

{
	
$db =mysql_connect("l","j", "");
mysql_select_db("",$db) or die ("cannot connect to database");
$query = "delete from contact where msg = '$msg[$x]'";
mysql_query($query);
}
header("Location:msg.php");






?>
Can anyone help

Many thanks
Judas
Forum Commoner
Posts: 67
Joined: Tue Jun 10, 2003 3:34 pm
Location: Netherlands

Post by Judas »

are there nul value's ?


(deleting code note)
Connect to the database before looping, and close the database after the loop .
:)
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Maybe it's because your closing the form with your </form> tag inside the loop?
jamrop
Forum Commoner
Posts: 80
Joined: Fri May 16, 2003 5:38 pm

Post by jamrop »

hey

thanks it was the </form> in the wrong place :)
Post Reply