• 0

PHP Check Box delete Script


Question

I am trying to delete my articles with selected checkboxes but I am having a little issue. When I click the delete button nothing happens, I am not sure where to append the variable, can someone help?

<?php

//include database

require_once('db_config.php');

$tbl_name="articles"; // Table name

$sql="SELECT * FROM $tbl_name";

$result=mysql_query($sql);

$count=mysql_num_rows($result);

//Provide the user with the number of articles in the database

//space string

$counttxt = "There are";

echo $counttxt;

print_r($count); $print = print 'articles in this database';

echo $print;

?>

<table width="400" border="0" cellspacing="1" cellpadding="0">

<tr>

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

<table width="900" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">

<tr>

<td bgcolor="#FFFFFF"> </td>

<td colspan="4" bgcolor="#FFFFFF"><strong>Select the articles you would like delete.</strong> </td>

</tr>

<tr>

<td align="center" bgcolor="#FFFFFF">#</td>

<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>

<td align="center" bgcolor="#FFFFFF"><strong>Title</strong></td>

<td align="center" bgcolor="#FFFFFF"><strong>Content</strong></td>

<td align="center" bgcolor="#FFFFFF"><strong>Frontpage</strong></td>

</tr>

<?php

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

?>

<tr>

<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['articleID']; ?>"></td>

<td bgcolor="#FFFFFF"><? echo $rows['articleID']; ?></td>

<td bgcolor="#FFFFFF"><? echo $rows['title']; ?></td>

<td bgcolor="#FFFFFF"><? echo $rows['content']; ?></td>

<td bgcolor="#FFFFFF"><? echo $rows['frontpage']; ?></td>

</tr>

<?php

}

?>

<tr>

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

</tr>

<?

//set the delete variable

$delete = (isset($_POST['checkbox']));

//use isset to check if the variable has been set or not

if ($delete){

$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";

$result = mysql_query($sql);

echo 'You are deleting this article';

print_r($result);

// Check if delete button active, start this

for($i=0;$i<$count;$i++){

$del_id = $checkbox[$i];

//print out the checkbox

}

// if successful redirect to delete_multiple.php

if($result){

echo 'You have deleted the article(s)';

//echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";

}

}

//mysql_close();

?>

</table>

</form>

</td>

</tr>

</table>

articles_list.phpFetching info...

Link to comment
https://www.neowin.net/forum/topic/1065264-php-check-box-delete-script/
Share on other sites

13 answers to this question

Recommended Posts

  • 0

off the top of my head you might need to change input name="checkbox[]" to input name="checkbox".

You probably also need to put the entire table inside 'form' tags which state where the submit button will post to.

I mean you probably need to state where the form will post to so something like <form name="form1" method="post" action="<?php $_SERVER['PHP_SELF'] ?>">

EDIT:

also change this: $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";

to this: $sql = "DELETE FROM $tbl_name WHERE id='$delete'";

EDIT2:

Your also forgot an apostrophe on line 50.

This one works fine: http://pastebin.com/smsde5Z8

You will need to sanitize the table checkbox inputs with mysql_escape_string or something otherwise people could put anything in there and submit it - and kindly drop all your data for you!

See here: http://stackoverflow.com/questions/129677/whats-the-best-method-for-sanitizing-user-input-with-php

  • 0

Sorry to disturb you but I have one more quesiton I've set the variable to the input name but it only deletes the record with an ID of 1 all the time.

<?php

//include database

require_once('db_config.php');

$tbl_name="articles"; // Table name

$sql="SELECT * FROM $tbl_name ORDER BY articleID";

$result=mysql_query($sql);

$count=mysql_num_rows($result);

//Provide the user with the number of articles in the database

echo "There are ".$count." articles in this database";

?>

<table width="400" border="0" cellspacing="1" cellpadding="0">

<tr>

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

<table width="900" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">

<tr>

<td bgcolor="#FFFFFF"> </td>

<td colspan="4" bgcolor="#FFFFFF"><strong>Select the articles you would like delete.</strong> </td>

</tr>

<tr>

<td align="center" bgcolor="#FFFFFF">#</td>

<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>

<td align="center" bgcolor="#FFFFFF"><strong>Title</strong></td>

<td align="center" bgcolor="#FFFFFF"><strong>Content</strong></td>

<td align="center" bgcolor="#FFFFFF"><strong>Frontpage</strong></td>

</tr>

<?php

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

?>

<tr>

<td align="center" bgcolor="#FFFFFF"><input name="checkbox" type="checkbox" id="checkbox" value="<? echo $rows['articleID']; ?>"></td>

<td bgcolor="#FFFFFF"><? echo $rows['articleID']; ?></td>

<td bgcolor="#FFFFFF"><? echo $rows['title']; ?></td>

<td bgcolor="#FFFFFF"><? echo $rows['content']; ?></td>

<td bgcolor="#FFFFFF"><? echo $rows['frontpage']; ?></td>

</tr>

<?php

}

?>

<tr>

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

</tr>

<?

//set the delete variable

$del_id = (isset($_POST['checkbox']));

//use isset to check if the variable has been set or not

if ($del_id){

$sql = "DELETE FROM $tbl_name WHERE articleID='".$del_id."'";

echo $sql;

$result = mysql_query($sql);

//$art_del = "You are deleting this article";

//echo ".$art_del.";

print_r($result);

// Check if delete button active, start this

for($i=0;$i<$count;$i++){

$del_id = $checkbox[$i];

//print out the checkbox

}

// if successful redirect to delete_multiple.php

if($result){

//echo 'You have deleted the article(s)';

//echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";

}

}

//mysql_close();

?>

</table>

</form>

</td>

</tr>

</table>

  • 0

The reason it is only deleteing one, is because you arn't looping through your SQL to delete the article ID's for each id. The difference of checkbox[] and checkbox as the above people have mentioned to be changed. You were right with sending it as "name=checkbox[]". Because then php interrupts the post data as a array. And thats what you would want. By using "name=checkbox" with multiple id's only one id is being sent back.

  • 0
  On 20/03/2012 at 19:35, mallenium115 said:

Thank you so once I revert back to checkbox[] it should work normally, how about the logic and structure of the form is everything alright so far?

Once you revert checkbox back to checkbox[], php will see the post data with the article ids as a $_POST[checkbox] array. Then you need to modify your DELETE sql to do a FOREACH $_POST[checkbox] to delete each ID and execute each SQL. There are cleaner ways to use a DELETE sql with only 1 sql. But as a learning experience, get with what you need working first. Then optimize later.

I'd modify you're code but i'm not fond of diving in. Id rather re-write it all or explain it to you how to re-write it lol.

  • 0
  On 20/03/2012 at 19:52, mallenium115 said:

I'd prefer the explaination so I can learn more. Thank you for the help I truly appreciate the support. I'm new at complex php scripting, basically creating an CMS for my internship. can you give me an example of the sql to work from?


<?php
// Check if Post Data was Sent, else just echo out a Form to the Viewer. If post was sent $_POST is valid and don't echo out a form.
if (!$_POST) {
echo "<form method=\"post\">";
echo "<input type=\"checkbox\" name=\"checkbox[]\" value=\"001\">Value 1<br/>";
echo "<input type=\"checkbox\" name=\"checkbox[]\" value=\"002\">Value 2<br/>";
echo "<input type=\"checkbox\" name=\"checkbox[]\" value=\"003\">Value 3<br/>";
echo "<input type=\"submit\" value=\"Submit\">";
}
else {
// See if checkbox[] Data is in POST Data and has Values, if so Continue.
if (count($_POST[checkbox]) != 0) {
// Lets loop through each checkbox[] value and preform a DELETE on it.
foreach ($_POST[checkbox] as $key => $value) {
// Here we check our field if its valid, this is a simple charecter type check. We Verify its a digit.
if (ctype_digit($value)) {
$sql = "DELETE FROM $tbl_name WHERE articleID=$value";
// mysql_query($sql);
// Print out that a query was executed for the assoicated articleID.
echo "articleID=$value" . ": Executed Query.<br/>";
}
else {
// We should kill the script, invalid non digit data was sent in the form.
die("Invalid Data Sent.");
}
}
echo "Finished Deleteing Articles from Form.";
}
}
?>
[/CODE]

I commented out the mysql_query part just because I don't have a database to test against and what not. But that gives you a idea of what i'm talking about. Feel free to PM some messenger (aim etc) details I don't mind answering a q for someone once and awhile :).

Also missing from this is db_config and $tbl_name. Heh.

  • 0

Okay so in my case what would I set my checkbox value to for the POST?

if (!$_POST) {

echo "<form method=\"post\">";

echo "<input type=\"checkbox\" name=\"checkbox[]\" value=\"001\">Value 1<br/>";

}

<tr>

<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['articleID']; ?>"></td>

<td bgcolor="#FFFFFF"><? echo $rows['articleID']; ?></td>

<td bgcolor="#FFFFFF"><? echo $rows['title']; ?></td>

<td bgcolor="#FFFFFF"><? echo $rows['content']; ?></td>

<td bgcolor="#FFFFFF"><? echo $rows['frontpage']; ?></td>

</tr>

<?php

}

?>

<tr>

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

</tr>

<?

?>

This topic is now closed to further replies.
  • Posts

    • Amazon Deals: Samsung Q990F Q900F Q800F 2025 Dolby Atmos soundbars with wireless subwoofers by Sayan Sen While separate AV receivers with hi-fi speakers are generally the preferred way to listed to music and watch movies/shows by audiophiles, the more general folks often prefer soundbars instead as they offer a capable all in one solution that is still plenty good. Currently Nakamichi is running discounts on multiple products from its Dragaon lineup as well as its Shockwafe model. If you are looking for more options to choose from Samsung has its Q-series products at lowest prices (purchase links towards the end of article). Q990F The flagship Q990F is an 11.1.4 system and the single subwoofer unit on it houses two opposite-facing 8-inch subwoofer drivers. Thus, together they move around the same amount of air as a single 12-inch subwoofer unit. In addition to increasing the bass by +3 dB, dual opposing drivers are also said to help reduce vibrations of the subwoofer cabinet by cancelling out the resonance. Samsung also says that the bass is "AI-optimized" but we are not sure if it actually helps or if it's just a buzz term here. Aside from the bass, dialogue in movies is the second most important thing, and Samsung claims great vocal clarity from its front speakers thanks to AVA (Active Voice Amplifier) Pro feature that is said to detect noise disturbances and amplify dialogue to make it more audible over such surrounding noises. Q900F Feature wise the Q900F is similar to the 990F model except it has fewer channels and it is a 7.1.2 setup. Finally the Q800F is a 5.1.2 system and it has a passive radiator instead of the additonal subwoofer driver unit. Get the Samsung Q series soundbars at the links below: Samsung Q990F 11.1.4ch Wireless Dolby Atmos, Q-Symphony, Game Mode Pro, Adaptive Sound (HW-Q990F, 2025): $1497.99 (Shipped and Sold by Amazon US) Samsung HW-Q900F 7.1.2 ch Wireless Dolby Atmos, Q-Symphony: $997.99 (Shipped and Sold by Amazon US) Samsung Q800F 5.1.2ch Q Series Soundbar + Subwoofer, Wireless Dolby Atmos, Q-Symphony, Game Mode Pro, Smart Integration (HW-Q800F, 2025): $697.99 (Shipped and Sold by Amazon US) This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • They are shifting into AI now. Don't you see?
  • Recent Achievements

    • One Month Later
      Helen Shafer earned a badge
      One Month Later
    • One Month Later
      ambani880 earned a badge
      One Month Later
    • Week One Done
      ambani880 earned a badge
      Week One Done
    • First Post
      artistro08 earned a badge
      First Post
    • First Post
      paul29 earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      498
    2. 2
      ATLien_0
      223
    3. 3
      Michael Scrip
      196
    4. 4
      Xenon
      160
    5. 5
      +FloatingFatMan
      138
  • Tell a friend

    Love Neowin? Tell a friend!