Jump to content

Multiple checkbox delete query problems


beam1985

Recommended Posts

After a good 24 hours of work on this code, ive finally gotten rid of all the error messages, but the page doesnt work yet, it doesn't correctly delete any rows. Perhaps someone sees something in the code that i dont? thank you so much!

 

heres the code in action(or inaction)

http://iam.colum.edu/students/Robert.Agra/delete.php

 

and heres the code

 

<?php

$host="localhost"; // Host name
$username="user"; // Mysql username
$password="pass"; // Mysql password

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("ragra");

if (!isset($_POST['delete'])){

$sql="SELECT * FROM article";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

?>

<table>
<tr>
	<td><img src="comcarblog.jpg"></td>
</tr>
</table>

<table>
<tr>
	<td><a href="main.php" alt="Home"><img src="home.jpg"></a></td>
	<td><a href="" alt=""><img src="blank.jpg"></a></td>
	<td><a href="add.php" alt="Admin"><img src="admin.jpg"></a></td>
</tr>
</table>

<form name="form1" method="post">

<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
	<tr>
		<td colspan="4" bgcolor="#41a1f3">
			You're about to delete some posts!
		</td>
	</tr>
</table>

<table width="400" border="0" cellspacing="1" cellpadding="0">
	<tr>
		<td align="center" bgcolor="#f3d841">Number</td>
		<td align="center" bgcolor="#f3d841">Id</td>
		<td align="center" bgcolor="#f3d841">Title</td>
		<td align="center" bgcolor="#f3d841">Tagline</td>
		<td align="center" bgcolor="#f3d841">Author</td>
	</tr>
	<?php
	while($rows=mysql_fetch_array($result)) {
	?>
	<tr>
		<td align="center" bgcolor="#FFFFFF">
			<input type="checkbox" name="checkbox[]" />
			<input type="hidden" value="<?php echo $rows['id']; ?>" name="itemid[]" />
		</td>
		<td bgcolor="#FFFFFF">
			<?php
				echo $rows['id'];
			?>
		</td>
		<td bgcolor="#FFFFFF">
		<?php
			echo $rows['title'];
		?>
		</td>
		<td bgcolor="#FFFFFF">
		<?php
			echo $rows['tagline'];
		?>
		</td>
		<td bgcolor="#FFFFFF">
		<?php
			echo $rows['author'];
		?>
		</td>
	</tr>
	<?php

	}
	?>
	<tr>
		<td colspan="5" align="center" bgcolor="#FFFFFF">
			<input name="delete" type="submit" id="delete" value="Delete">
		</td>
	</tr>
</table>

</form>

<?php

} else {

if($_POST['delete'] == 'Delete'){
	for($i=0;$i<count($_POST['itemid']);$i++)
	{
		if(isset($_POST['checkbox'][$i]) && $_POST['checkbox'][$i]=='checked')
		{
			$del_id = $_POST['itemid'][$i];
			$sql = "DELETE FROM article WHERE id='$del_id'";
			$result = mysql_query($sql);
		}
	} 
	header("Location:delete.php");
}
}

mysql_close();
?>

Link to comment
Share on other sites

 

 

which part of the above link should i use? how does it work with my code?

 

 

The particular post of mine that the link points to

 

If the checkboxes have the id as the value

<?php
echo '<input type="checkbox" name="chk[]" value="{$news['id']}" />';
?>

 

then you can do all the deletions with a single query

 

<?php
$deletes = join(',', $_POST['chk'] );
$query = mysql_query("DELETE FROM site_news WHERE id IN ($deletes) ");

Link to comment
Share on other sites

Glad it worked for you.

 

Your code ignored the fact that only checked checkboxes are posted and so assumed that the i'th posted checkbox would be map to the i'th posted record id .

 

You can check for a checkbox being checked on the client (javascript) but not in PHP on the server.

Link to comment
Share on other sites

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.