Jump to content

[SOLVED] deleting the checked records


Vidya_tr

Recommended Posts

I have a page that displays the name of some files wih a checkbox against it.The list is created and displayed dynamically when each time a record is added to the database.There is a DELETE button also. The files has to be selected using checkbox and when the delete button is clicked the selected records should be deleted from the database and its name should be cleared from the list.

 

The post action of delete button calls delete_file.php where the code for the purpose has to be written.

 

How can I write the code to identify the checked checkboxes and do the action?

 

The code for the page creating the list is given below..

 

<form name="f1" method="post" action="delete_file.php">
<input name="delete" type="button" value="Delete video"  />
<div  align="left" >
<?php 


$sql="SELECT * FROM video_links"; 

$result = mysql_query($sql);


while($row = mysql_fetch_array($result))
{

echo "<br>";


?>
<input type="checkbox" name="chk[]"  id="delcheckbox"  value="<?php echo $row['id']; ?>"/> <a href="#"><?php echo $row['title'] ?></a>

<?php 


}


echo "<br>";
?>
</div>
</form>

 

googled a lot. but not able to find a proper solution .Please help....

Link to comment
Share on other sites

thank you...

I was able to make the concept clear through your tutorial.

But for me,still some problem exits.

My code to delete the records is as follows..

<?php
include('config.php');
if(count($_POST) > 0)
{
$delcheck=$_POST['deletechk'];

$delcheck= implode(",", $delcheck);
//echo $delcheck;
mysql_query("DELETE from video_links where link_id IN($delcheck)") or trigger_error(mysql_error(),E_USER_ERROR);  
mysql_query("DELETE from video where videoID IN($delcheck)") or trigger_error(mysql_error(),E_USER_ERROR);  
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input name="delete" type="button" value="Delete Video" />
<div  align="left" >
<?php 


$sql="SELECT * FROM video_links"; 

$result = mysql_query($sql);

if($result==NULL)
{echo "<br>No videos uploaded";
exit();}


while($row = mysql_fetch_array($result))
{

	echo "<br>";

	echo '<input type="checkbox" name="deletechk[]"  value="'.$row['link_id'].' " /> <a href="#"> '.$row['videoTitle'].'</a>';

}

//echo "</table>";
echo "<br>";
?>
</div>

</form>
</body>
</html>

 

But with this code it is not even identifying whether the DELETE button is clicked or not..

The $_POST of DELETE button is not working...

Please help me to find where I went wrong in this code...

Link to comment
Share on other sites

My problem is that, the if loop of the code

if(count($_POST) > 0)
{...
}

is not working..

I am not able to find what goes  wrong when the delete button is clicked...

action POST is not taking place even after selecting the checkbox and clicking the delete button

Please help me... :'(

Link to comment
Share on other sites

Try this, all I've changed is:

* Changed your button to a SUBMIT button

* Indented the code properly

* Changed the check to isset() instead of using count

 

<?php
include('config.php');
if (isset($_POST['subdelete']) {
  $delcheck=$_POST['deletechk'];

  $delcheck= implode(",", $delcheck);
  //echo $delcheck;
  mysql_query("DELETE from video_links where link_id IN($delcheck)") or trigger_error(mysql_error(),E_USER_ERROR);  
  mysql_query("DELETE from video where videoID IN($delcheck)") or trigger_error(mysql_error(),E_USER_ERROR);  
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input name="subdel" type="submit" value="Delete Video" />
<div  align="left" >
<?php 


$sql="SELECT * FROM video_links"; 

$result = mysql_query($sql);

if($result==NULL) {
  echo "<br>No videos uploaded";
  exit();
}


while($row = mysql_fetch_array($result)) {
    echo "<br>";
    echo '<input type="checkbox" name="deletechk[]"  value="'.$row['link_id'].' " /> <a href="#"> '.$row['videoTitle'].'</a>';
}

//echo "</table>";
echo "<br>";
?>
</div>

</form>
</body>
</html>

Link to comment
Share on other sites

One more small doubt......

I am not able to delete more than one record at a time

It shows the follwing warning and error...

 

Warning: implode() [function.implode]: Invalid arguments passed in C:\wamp\www\DEVTPN\videolist.php on line 22

 

Fatal error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 in C:\wamp\www\DEVTPN\videolist.php on line 30

Link to comment
Share on other sites

if (isset($_POST['subdelete']) {
  foreach ($_POST['deletechk'] as $d) {
    mysql_query("DELETE FROM video_links WHERE link_id=".$d." LIMIT 1") or trigger_error(mysql_error(),E_USER_ERROR);
  }
}

 

That will loop through the selected items deleting them one by one.

Link to comment
Share on other sites

Only thing I'd recommend is to prevent against MySQL injection.

 

If your ID field is a numerical value (which I suspect it is) then use this:

if (isset($_POST['subdelete']) {
  foreach ($_POST['deletechk'] as $d) {
    mysql_query("DELETE FROM video_links WHERE link_id=".intval($d)." LIMIT 1") or trigger_error(mysql_error(),E_USER_ERROR);
  }
}

That will return 0 if anything other than a number is sent. You can perform other checks but that's a start.

Link to comment
Share on other sites

Only thing I'd recommend is to prevent against MySQL injection.

 

If your ID field is a numerical value (which I suspect it is) then use this:

if (isset($_POST['subdelete']) {
  foreach ($_POST['deletechk'] as $d) {
    mysql_query("DELETE FROM video_links WHERE link_id=".intval($d)." LIMIT 1") or trigger_error(mysql_error(),E_USER_ERROR);
  }
}

That will return 0 if anything other than a number is sent. You can perform other checks but that's a start.

 

One of the points of my tutorial was to avoid making multiple queries for something like this. You only need the one if you use an IN clause. You can validate using the array_map function prior to executing the query.

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.