PDA

View Full Version : Problem of deleting multiple news at same time


xiaodao
11-26-2004, 11:03 AM
my thinking is use checkboxes, i click the checkboxes beside the news then click submit to delet those news checked

my script below, but did not work

<?
include 'config.php';
$sql=mysql_query("SELECT * FROM k_news ORDER BY newsid DESC");
echo "<form name='del' action='delnews.php' method=POST>";
echo "<table width=400 cellpadding=0 cellspacing=0>";
while($row=mysql_fetch_array($sql)) {
echo "<tr><Td>".$row['newstitle']."</td><td>".$row['date']."</td><td><input type='checkbox' name='delid[]' value=".$row['newsid']."</td></tr>";
}
echo "<tr><td colspan=3><input type=submit value=delet name=action></td></tr>";
echo "</table></form>";
if($action==delet) {
require_once 'config.php';
$newsid[]=$delid[];
for($n=0;$n<count($newsid),$n++)
{
$id=$newsid[$n];
$sql=mysql_query(DELET FROM k_news where newsid=$id) or die(mysql_error());
}
if (!$sql) {
echo"Cannot delet the news";
}
else
{ echo"News successfully deleted";
}
}
?>


error happens, says $newsid[]=$delid[]; is wrong, how to solve?

raf
11-26-2004, 12:53 PM
replace
$newsid[]=$delid[];
for($n=0;$n<count($newsid),$n++)
{
$id=$newsid[$n];
$sql=mysql_query(DELET FROM k_news where newsid=$id) or die(mysql_error());
...
}

by

$sql=mysql_query('DELET FROM k_news where newsid In ('. implode(', ', $_POST['delid']) .')') or die(mysql_error());

xiaodao
11-26-2004, 03:55 PM
replace
$newsid[]=$delid[];
for($n=0;$n<count($newsid),$n++)
{
$id=$newsid[$n];
$sql=mysql_query(DELET FROM k_news where newsid=$id) or die(mysql_error());
...
}

by

$sql=mysql_query('DELET FROM k_news where newsid In ('. implode(', ', $_POST['delid']) .')') or die(mysql_error());

Please explain

raf
11-26-2004, 04:13 PM
just replace your code by the one i supplied.

how it works?
- inside the form, you have " name='delid[]' " for the checkboxes, which ells me this is an array.
- so when the form is posted, you'll have an array $_POST['delid'] that contains all checked boxes their values as elements (because there value is set like " value=".$row['newsid'] " --> you're missing some quotes there for the value)
If you wanna check this, then put
print_r($_POST['delid']);
in your code when you process the form.
- with implode(', ', $_POST['delid']) we then turn the array into a commaseperated list
- inside our where-clause, we use the In() comparison. Inside the parentheses, you need to put a commadelimited list of values. This will then delete all records where the newsid is found inside the list
- so, use the implode function to generate the commaseperated list when we build the query.

of course, you should do some extra checks beforerunnin the query --> like checking if there are checkboxes check like

if (count($_POST['delid']) >= 1){ //at least one bow is checked

etc

xiaodao
11-26-2004, 04:39 PM
great, thanks

xiaodao
11-26-2004, 05:03 PM
appear this problem


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 'DELET FROM k_news where newsid In (3</td)' at line 1

Brandoe85
11-26-2004, 10:00 PM
I think you spelt DELETE wrong, you have DELET.

marek_mar
11-26-2004, 10:13 PM
Ok xiaodo that's the second time you have misspelled DELETE.
You have to write DELETE a 100 times for tommorow!

xiaodao
11-27-2004, 01:10 AM
yes sir, i should really careful in writing