Jump to content

Multiple row delete -- What do you see wrong with this solution


richardw

Recommended Posts

I see the code posted by desithugg  and I may look to use that. But my code is as follows:

 

<?PHP
if($_POST['submit']) {
foreach($_POST as $id) { // loop through checkboxes
mysql_query("DELETE FROM sidewalks  WHERE id='$id1'"); // table and record id
}
?>

<?php 
} else{
// display form
  ?>
<form name="action" id="action" method="post" action="<?=$_SERVER['PHP_SELF']?>">
     <?php
    $db = mysql_connect("localhost", "Uname", "PWD") or die("Could not connect");
    $provridb = mysql_select_db("dpw",$db);		
$sql = "SELECT * FROM sidewalks WHERE ward ='9' ORDER BY treet_1,street_nmbr ASC"; 
$result = mysql_query($sql);		

$num_results =  mysql_num_rows($result);
echo 'There are currently ', $num_results,' records for this ward', '<br><br>';
while ($row = mysql_fetch_assoc($result)) {
 echo  $row["street_nmbr"], " ",   $row["street"] ?><input type="checkbox" name="<?=$row["id"]?>" id="<?= $row["id"]?>" value="<?=  $row["id"] ?>" /> 
        <br>
  <?php   

} 
mysql_free_result($result);
?>	

 

Any suggestions as how to fix this code? Thanks  :)

<?PHP
if($_POST['submit']) {
foreach($_POST['delete_list'] as $id) { // loop through checkboxes
mysql_query("DELETE FROM sidewalks  WHERE id='$id1'"); // table and record id
}
?>

<?php 
} else{
// display form
  ?>
<form name="action" id="action" method="post" action="<?=$_SERVER['PHP_SELF']?>">
     <?php
    $db = mysql_connect("localhost", "Uname", "PWD") or die("Could not connect");
    $provridb = mysql_select_db("dpw",$db);		
$sql = "SELECT * FROM sidewalks WHERE ward ='9' ORDER BY treet_1,street_nmbr ASC"; 
$result = mysql_query($sql);		

$num_results =  mysql_num_rows($result);
echo 'There are currently ', $num_results,' records for this ward', '<br><br>';
while ($row = mysql_fetch_assoc($result)) {
 echo  $row["street_nmbr"], " ",   $row["street"] ?><input type="checkbox" name="delete_list[]" id="<?= $row["id"]?>" value="<?=  $row["id"] ?>" /> 
        <br>
  <?php   

} 
mysql_free_result($result);
?>	

name="delete_list[]"

you need the []  to pass arrays through forms

Not sure why, but it still won't delete... What am I missing?

 

I have been on this code for too many hours, so I may be missing the obvious. Thanks  :)

 

<?PHP
if($_POST['submit']) {
foreach($_POST['delete_list'] as $id1) { // loop through checkboxes
mysql_query("DELETE FROM sidewalks  WHERE id='$id1'"); // table and record id
}
?>

<?php 
} else {
// display form
  ?>
<form name="action" id="action" method="post" action="<?=$_SERVER['PHP_SELF']?>">

     <?php
    $db = mysql_connect("localhost", "Uname", "PWD") or die("Could not connect");
    $provridb = mysql_select_db("dpw",$db);
$sql = "SELECT * FROM sidewalks WHERE ward ='9' ORDER BY street_1,street_nmbr ASC"; 

$result = mysql_query($sql);	
$num_results =  mysql_num_rows($result);
echo 'There are currently ', $num_results,' records for this ward', '<br><br>';
while ($row = mysql_fetch_assoc($result)) {

 echo  $row["street_nmbr"], " ",   $row["street"] ?><input type="checkbox" name="delete_list[]" id="<?= $row["id"]?>" value="<?=  $row["id"] ?>" /> 
        <br>
     <?php
 }
mysql_free_result($result);
?>
<input type='submit' value='Delete Selected'>
</form>
                           
   <?php
} // end if
?>

 

Thanks again!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.