Page 1 of 1

Delete using check box

Posted: Fri Jul 04, 2003 8:22 pm
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

Posted: Fri Jul 04, 2003 9:10 pm
by Judas
are there nul value's ?


(deleting code note)
Connect to the database before looping, and close the database after the loop .
:)

Posted: Fri Jul 04, 2003 9:28 pm
by jason
Maybe it's because your closing the form with your </form> tag inside the loop?

Posted: Sat Jul 05, 2003 5:15 am
by jamrop
hey

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