Jump to content

[SOLVED] Checkbox delete?


Cory94bailly

Recommended Posts

I am trying to add 2 extra columns at the beginning of my table so admins can edit/delete one by one...

 

 

My code:

 

<head>
<title>Recently Wanted</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>

<br />

<div id="menu">
<? include('menu.html'); ?>
</div>
<div id="content">
<?php
require('config.php');
date_default_timezone_set('America/New_York');
?>
<!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=utf-8" />
</head>
<body>
<br />
<center>
<h1>Current Date: <? echo date('m-d-Y'); ?></h1>
<h1>Current time: <? echo date("g:i a"); ?></h1>
<table border="1" cellpadding="15">
<?php
   mysql_connect("$path", "$username", "$password") or die(mysql_error());
   mysql_select_db("$database") or die(mysql_error());

$query = "SELECT * FROM recentwanted";

$result = mysql_query($query) or die(mysql_error());


while($row = mysql_fetch_array($result)){
        echo "<tr>";
        echo "<td>";
        echo $row['date'];
      echo "</td>";
      echo "<td>";
        echo $row['time'];
      echo "</td>";
        echo "<td>";
      echo $row['name'];
      echo "</td>";
      echo "<td>";
      echo $row['game'];
      echo "</td>";
      echo "<td>";
      echo $row['aimbot'];
      echo "</td>";
        echo "<td>";
      echo $row['gui'];
      echo "</td>";
      echo "<td>";
      echo $row['killspam'];
      echo "</td>";
      echo "<td>";
      echo $row['ip'];
      echo "</td>";
      echo "<td>";
      echo $row['hostname'];
      echo "</td>";
      echo "</tr>";
}
?>
</table>
</div>

 

 

 

 

Here's another script of mine:

 

<head>
<title>Edit/Delete News</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>

<br />

<div id="menu">
<? include('menu.html'); ?>
</div>
<div id="content">
<?php

require 'config.php';

// Make a MySQL Connection
mysql_connect($path, $username, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());

$query1 = "SELECT * FROM news ORDER BY sticky, id ASC";
$result1 = mysql_query($query1) or die(mysql_error());

function printNews($result)
{
    if ( mysql_num_rows($result) < 1 )
    {
        echo "<h1>No news!</h1>";
    }
    else
    {
        echo "<table cellspacing=\"2\" cellpadding=\"5\">\n";

        while($news = mysql_fetch_assoc($result))
        {
         $sticky = ($news['sticky'] == 'y') ? "<font color='red'><u>Announcement:</u></font> " : '';
            echo <<<HTML
  <tr>
    <th align="left">{$sticky}<b>{$news['title']}</b></th>
    <td rowspan="2" valign="bottom" align="left">
      <a href="news_edit.php?id={$news['id']}"><img border="0" src="../images/edit.png" alt="Edit" /></a><br />
      <a href="news_delete.php?id={$news['id']}"><img border="0" src="../images/delete.png" alt="Edit" /></a>
    </td>
  </tr>
  <tr>
    <td>{$news['news']}</td>
  </tr>
HTML;
        }

        echo "\n</table><br />";
    }
}

printNews($result1);

?>

 

I want something like that but to fit into my other script..

 

Also, I don't even know how to add checkboxes so that if you select a checkbox for say 5 of them, it will only delete those 5, not all...

 

Thanks all!

 

(Any help is appreciated...)

Link to comment
Share on other sites

Well should be easy since only check boxes that are actually checked get passed. Easiest thing to do is create an array of the id's of the rows you want to delete.

 

while($row = mysql_fetch_array($result)){
        echo "<tr>";
        echo "<td>";
        echo "<input type=\"checkbox\" name=\"del[]\" value=\"{$row['id']}\" />";
        echo "</td>";
        echo "<td>";
        echo $row['date'];
        echo "</td>";
        echo "<td>";
        echo $row['time'];
        echo "</td>";
        echo "<td>";
        echo $row['name'];
        echo "</td>";
        echo "<td>";
        echo $row['game'];
        echo "</td>";
        echo "<td>";
        echo $row['aimbot'];
        echo "</td>";
        echo "<td>";
        echo $row['gui'];
        echo "</td>";
        echo "<td>";
        echo $row['killspam'];
        echo "</td>";
        echo "<td>";
        echo $row['ip'];
        echo "</td>";
        echo "<td>";
        echo $row['hostname'];
        echo "</td>";
        echo "</tr>";
}

 

and on the page that processes the delete

foreach($_POST['del'] as $id){
$sql = "DELETE FROM `recentwanted` WHERE `id` = '$id' LIMIT 1";
mysql_query($sql) or die(mysql_error());
}

 

Just a quick help

 

Ray

Link to comment
Share on other sites

I fixed that up a bit and I get this:

 

Warning: Invalid argument supplied for foreach() in /home/content/m/a/r/markbailly/html/fcs/admin/recentwanted.php on line 3

 

 

Script:

 

<?php
if(isset($_POST['deleteselected'])) { 
foreach($_POST['del'] as $id){
$sql = "DELETE FROM `recentwanted` WHERE `id` = '$id' LIMIT 1";
mysql_query($sql) or die(mysql_error());
}
}
?>

<head>
<title>Recently Wanted</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>

<br />

<div id="menu">
<? include('menu.html'); ?>
</div>
<div id="content">
<?php
require('config.php');
date_default_timezone_set('America/New_York');
?>
<!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=utf-8" />
</head>
<body>
<br />
<center>
<h1>Current Date: <? echo date('m-d-Y'); ?></h1>
<h1>Current time: <? echo date("g:i a"); ?></h1>
<table border="1" cellpadding="15">
<?php
   mysql_connect("$path", "$username", "$password") or die(mysql_error());
   mysql_select_db("$database") or die(mysql_error());

$query = "SELECT * FROM recentwanted";

$result = mysql_query($query) or die(mysql_error());


while($row = mysql_fetch_array($result)){
        echo "<tr>";
        echo "<td>";
        echo "<input type='checkbox' name='del[]' value='{$row['id']}' />";
        echo "</td>";
        echo "<td>";
        echo $row['date'];
      echo "</td>";
      echo "<td>";
        echo $row['time'];
      echo "</td>";
        echo "<td>";
      echo $row['name'];
      echo "</td>";
      echo "<td>";
      echo $row['game'];
      echo "</td>";
      echo "<td>";
      echo $row['aimbot'];
      echo "</td>";
        echo "<td>";
      echo $row['gui'];
      echo "</td>";
      echo "<td>";
      echo $row['killspam'];
      echo "</td>";
      echo "<td>";
      echo $row['ip'];
      echo "</td>";
      echo "<td>";
      echo $row['hostname'];
      echo "</td>";
      echo "</tr>";
}
?>
</table>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
      <input type="submit" name="deleteselected" value="Delete Selected">
   </form>
</div>

Link to comment
Share on other sites

Code now:

 

<?php
if ( isset($_POST['del']) && is_array($_POST['del']) ) {

    foreach ( $_POST['del'] as $id ) {
    $sql = "DELETE FROM `recentwanted` WHERE `id` = '$id' LIMIT 1";
    mysql_query($sql) or die(mysql_error());
    }

}

?>

<head>
<title>Recently Wanted</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>

<br />

<div id="menu">
<? include('menu.html'); ?>
</div>
<div id="content">
<?php
require('config.php');
date_default_timezone_set('America/New_York');
?>
<!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=utf-8" />
</head>
<body>
<br />
<center>
<h1>Current Date: <? echo date('m-d-Y'); ?></h1>
<h1>Current time: <? echo date("g:i a"); ?></h1>
<table border="1" cellpadding="15">
<?php
   mysql_connect("$path", "$username", "$password") or die(mysql_error());
   mysql_select_db("$database") or die(mysql_error());

$query = "SELECT * FROM recentwanted";

$result = mysql_query($query) or die(mysql_error());


while($row = mysql_fetch_array($result)){
        echo "<tr>";
        echo "<td>";
        echo "<input type='checkbox' name='del[]' value='{$row['id']}' />";
        echo "</td>";
        echo "<td>";
        echo $row['date'];
      echo "</td>";
      echo "<td>";
        echo $row['time'];
      echo "</td>";
        echo "<td>";
      echo $row['name'];
      echo "</td>";
      echo "<td>";
      echo $row['game'];
      echo "</td>";
      echo "<td>";
      echo $row['aimbot'];
      echo "</td>";
        echo "<td>";
      echo $row['gui'];
      echo "</td>";
      echo "<td>";
      echo $row['killspam'];
      echo "</td>";
      echo "<td>";
      echo $row['ip'];
      echo "</td>";
      echo "<td>";
      echo $row['hostname'];
      echo "</td>";
      echo "</tr>";
}
?>
</table>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
      <input type="submit" name="deleteselected" value="Delete Selected">
   </form>
</div>

 

 

 

I click submit and it just does nothing.

Link to comment
Share on other sites

Well, do you have any boxes checked?  Also, you have no output, so obviously it's not going to say anything.

 

Yep, I tried 1 box checked then 2...

 

I know, but it never deletes them..

 

 

 

echo $sql and see what it contains.  I'm guessing $id is empty

 

I did that and it just showed the button.. lol

Link to comment
Share on other sites

Here's some code that you may find useful:

 

<table align="center" border="0">


<?php

$query = mysql_query("SELECT * FROM table WHERE id='$row[id]'");
while($r = mysql_fetch_array($query)){
echo "<tr><td>";
echo $r['email']." </td><td><input type='checkbox' name='select[]' value='".$r['id']."'></td></tr>"; 

//just print a title and a text box. note the [], this creates an array of all selected checkboxes.

}


?>	


<tr>
<td><input type="submit" name="submit" value="Delete Selected Email Addresses"></td>
</tr>
</table>


$selected = implode(",", $_POST['select']); //this would make the array into something like this 1,3,7,8 etc...

if($selected == ''){

echo "Nothing selected!";

}else{

mysql_query("DELETE FROM table WHERE id in ($selected)"); //deletes all from table if id is in variable

echo "Deleted records!";

}

Link to comment
Share on other sites

 

Ok but I'm not really that good with php...

 

It's sad, I can't even tear that tiny bit apart.

 

I'm no 'guru' either, but I made it work quite easily. Make a new page, call it 'test.php' and use the code I supplied. Have a play with it....you can do it :-)

Link to comment
Share on other sites

I'm no 'guru' either, but I made it work quite easily. Make a new page, call it 'test.php' and use the code I supplied. Have a play with it....you can do it :-)

 

I'm working with it...

 

<?php
require('config.php');
date_default_timezone_set('America/New_York');
   mysql_connect("$path", "$username", "$password") or die(mysql_error());
   mysql_select_db("$database") or die(mysql_error());
if ( isset($_POST['del']) && is_array($_POST['del']) ) {

//    foreach ( $_POST['del'] as $id ) {
//    $sql = "DELETE FROM `recentwanted` WHERE `id` = '$id' LIMIT 1";
//    mysql_query($sql) or die(mysql_error());
//   }

//}
$selected = implode(",", $_POST['select']); //this would make the array into something like this 1,3,7,8 etc...

if($selected == ''){

echo "Nothing selected!";

}else{

mysql_query("DELETE FROM recentwanted WHERE id in ($selected)");

echo "Deleted records!";

}
}
?>

<head>
<title>Recently Wanted</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>

<br />

<div id="menu">
<? include('menu.html'); ?>
</div>
<div id="content">
<!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=utf-8" />
</head>
<body>
<br />
<center>
<h1>Current Date: <? echo date('m-d-Y'); ?></h1>
<h1>Current time: <? echo date("g:i a"); ?></h1>
<table border="1" cellpadding="15">
<?php
$query = "SELECT * FROM recentwanted";

$result = mysql_query($query) or die(mysql_error());


while($row = mysql_fetch_array($result)){
        echo "<tr>";
        echo "<td>";
        echo "<input type='checkbox' name='selected[]' value='$row[id]' />";
        echo "</td>";
        echo "<td>";
        echo $row['date'];
      echo "</td>";
      echo "<td>";
        echo $row['time'];
      echo "</td>";
        echo "<td>";
      echo $row['name'];
      echo "</td>";
      echo "<td>";
      echo $row['game'];
      echo "</td>";
      echo "<td>";
      echo $row['aimbot'];
      echo "</td>";
        echo "<td>";
      echo $row['gui'];
      echo "</td>";
      echo "<td>";
      echo $row['killspam'];
      echo "</td>";
      echo "<td>";
      echo $row['ip'];
      echo "</td>";
      echo "<td>";
      echo $row['hostname'];
      echo "</td>";
      echo "</tr>";
}
?>
</table>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
      <input type="submit" name="deleteselected" value="Delete Selected">
   </form>
</div>

 

^^Code...

 

 

I check a box and click submit and nothing changes.

Link to comment
Share on other sites

<?php
require('config.php');
date_default_timezone_set('America/New_York');
   mysql_connect("$path", "$username", "$password") or die(mysql_error());
   mysql_select_db("$database") or die(mysql_error());
if ( isset($_POST['del']) && is_array($_POST['del']) ) {

//    foreach ( $_POST['del'] as $id ) {
//    $sql = "DELETE FROM `recentwanted` WHERE `id` = '$id' LIMIT 1";
//    mysql_query($sql) or die(mysql_error());
//   }

//}
$selected = implode(",", $_POST['select']); //this would make the array into something like this 1,3,7,8 etc...

if($selected == ''){

echo "Nothing selected!";

}else{

mysql_query("DELETE FROM recentwanted WHERE id in ($selected)");

echo "Deleted records!";

}
}
?>

<head>
<title>Recently Wanted</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>

<br />

<div id="menu">
<? include('menu.html'); ?>
</div>
<div id="content">
<!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=utf-8" />
</head>
<body>
<br />
<center>
<h1>Current Date: <? echo date('m-d-Y'); ?></h1>
<h1>Current time: <? echo date("g:i a"); ?></h1>
<table border="1" cellpadding="15">
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<?php
$query = "SELECT * FROM recentwanted";

$result = mysql_query($query) or die(mysql_error());


while($row = mysql_fetch_array($result)){
        echo "<tr>";
        echo "<td>";
        echo "<input type='checkbox' name='selected[]' value='$row[id]' />";
        echo "</td>";
        echo "<td>";
        echo $row['date'];
      echo "</td>";
      echo "<td>";
        echo $row['time'];
      echo "</td>";
        echo "<td>";
      echo $row['name'];
      echo "</td>";
      echo "<td>";
      echo $row['game'];
      echo "</td>";
      echo "<td>";
      echo $row['aimbot'];
      echo "</td>";
        echo "<td>";
      echo $row['gui'];
      echo "</td>";
      echo "<td>";
      echo $row['killspam'];
      echo "</td>";
      echo "<td>";
      echo $row['ip'];
      echo "</td>";
      echo "<td>";
      echo $row['hostname'];
      echo "</td>";
      echo "</tr>";
}
?>
</table>
      <input type="submit" name="deleteselected" value="Delete Selected">
   </form>
</div>

 

Still does nothing.

Link to comment
Share on other sites

change 1st part of your script to

<?php
require('config.php');
date_default_timezone_set('America/New_York');
   mysql_connect("$path", "$username", "$password") or die(mysql_error());
   mysql_select_db("$database") or die(mysql_error());
if ( isset($_POST['selected']) && is_array($_POST['selected']) ) {// name in  your chkbox is selected! not del or select 

//    foreach ( $_POST['del'] as $id ) {
//    $sql = "DELETE FROM `recentwanted` WHERE `id` = '$id' LIMIT 1";
//    mysql_query($sql) or die(mysql_error());
//   }

//}
$selected = implode(",", $_POST['selected']); // <-- change this line too

if($selected == ''){

echo "Nothing selected!";

}else{

mysql_query("DELETE FROM recentwanted WHERE id in ($selected)");

echo "Deleted records!";

}
}
?>

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.