I was reading through the forum and still cant get this code to work. I have a saved cart for different users. Each cart linked to different table. with list of items in the cart for retreval. But i cant get the admin function to work to delete saved carts. here is the code

<?

$db_name       = "";
$db_host       = "";
$db_port       = "";
$db_user       = "";
$db_password   = "";

mysql_connect($db_host, $db_user, $db_password)or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select db"); 

$sql="SELECT * FROM saved_carts";
$result=mysql_query($sql);

$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</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>Cart ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>User ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Ammount</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><form method="post" ><input name="cart_id[]" type="checkbox" id="checkbox[]" value=cart_id."<? echo $row['id']; ?>">
</td>
<td bgcolor="#FFFFFF"><? echo $rows['cart_id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['cart_name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['user_id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['cart_total']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="submit" type="submit" id="delete" value="Delete"></td>
</tr>
<?
// Check if delete button active, start this 
if (isset($_POST['Submit'])) {
  $i = 1;
  $j = $rows+1;
  while ($i < $j) {
    $value = $_POST['cart_id'.$i];
    if ($value !== '') {
      $sql = "DELETE FROM `saved_carts` where cart_id=$i";
      $query = mysql_query($sql);
    }
  }
}
echo $j
if($submit){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";
}

?>
</table>
</form>
</td>
</tr>
</table>

Any help will be appreciated. I did not included the saved_items table as i cant get deletion work even from one table.

Surely

$j = $rows+1;

should be

$j = $count+1;

Surely

$j = $rows+1;

should be

$j = $count+1;

It stil doesn't work. Just refreshes the page and nothing else

Are you sure the query is good? try replacing $query = mysql_query($sql); with $query = mysql_query($sql)or die(mysql_error());

i tried that as well

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.