Organising my Syntax

Hi all, I’ve created a quick script to delete items from peoples orders. I’ve just added in at the top an if statement to prevent deletion if there is only one record in the database. Before I added the if statement, once ‘Submit’ was pressed the page would reload with the updated page (whichever items the user had deleted were no longer displayed).

I think my problem is now with $rowNums. It was originally in my second syntax but needed to move it to the top for the if statement to function. Any ideas to what could be causing this would be appreciated!

<?php
$rowNums = mysql_query("SELECT * FROM order_products WHERE `receipt_id` LIKE '$ref'");

if(isset($_POST['submit'])){

	if($ready = mysql_num_rows($rowNums)==1){
	echo "You cannot delete any further rows!";
	}
	else {
	//Get all checked boxes and run through array...
	foreach($_POST['delete'] as $value){
	
	$deleteId = mysql_real_escape_string($value);
	
	$qid = mysql_query("DELETE FROM order_products WHERE id='".$deleteId."'");
	
	}
	}

}//EOF isset
?>

<div class="headerTitles">Delete Items in Order</div>
<form action="<?php echo $_SERVER['PHP_SELF']."?section=deleteitems&".$urlCarry ?>" method="post">

<div style="float: left; width: 100%">
<table style="width: 100%" cellpadding="5" cellspacing="0">
<tr>
<td><b>Delete</b></td>
	<td><b>Qty</b></td>
	<td><b>Product Description</b></td>
			<td><b>Condition</b></td>
		<td><b>VAT</b></td>
	<td><b>Item Price</b></td>
<td style="text-align: right;"><b>Subtotal</b></td>
</tr>

<?php
$i = 0;
while($rows = mysql_fetch_array($rowNums)){
echo row_color($cnt++,"even-row","odd-row");
echo "<td><input type=\\"checkbox\\" name=\\"delete[]\\" value=\\"$rows[id]\\"></td>\
";
echo "<td>$rows[product_quant]</td>\
";
echo "<td>$rows[product_name]</td>\
";
echo "<td>$rows[product_cond]</td>\
";
echo "<td>£$rows[vat]</td>\
";
echo "<td>£$rows[product_price]</td>\
";
echo "<td style=\\"text-align: right;\\">£$rows[sub_total]</td>\
";
echo "</tr>\
";
}
?>
</table>
<input type="submit" value="Delete" name="submit" id="subBtn" />
</form>
</div>

What about…


<?php
if('POST' === $_SERVER['REQUEST_METHOD']){
  
  $hasItemsToDelete = false === empty($_POST['delete']) && is_array($_POST['delete']);
  
  if($hasItemsToDelete){
    
    $idCollection = array_map(
      'intval',
      $_POST['delete']
    );
    
    mysql_query(sprintf(
      'DELETE FROM order_products WHERE id IN (%s);',
      implode(',', $idCollection)
    ));
    
  }
}

function getInvoiceItems($ref){
  $records = array();
  $result = mysql_query(sprintf(
    "SELECT * FROM order_products WHERE receipt_id = '%s';",
    mysql_real_escape_string($ref)
  ));
  if(false === is_resource($result)){
    return $records;
  }
  while($record = mysql_fetch_assoc($result)){
    array_push($records, $record);
  }
  return $records;
}
?>
<table style="width: 100%" cellpadding="5" cellspacing="0">
  <tr>
    <td><b>Delete</b></td>
    <td><b>Qty</b></td>
    <td><b>Product Description</b></td>
    <td><b>Condition</b></td>
    <td><b>VAT</b></td>
    <td><b>Item Price</b></td>
    <td style="text-align: right;"><b>Subtotal</b></td>
  </tr>
  <?php foreach(getInvoiceItems('AnthonyOrder01') as $item): ?>
    <tr>
      <td><input type="checkbox" name="delete[]" value="<?php echo $item['id']; ?>"></td>
      <td><?php echo $item['product_quant']; ?></td>
      <td><?php echo $item['product_name']; ?></td>
      <td><?php echo $item['product_cond']; ?></td>
      <td>&pound;<?php echo $item['vat']; ?></td>
      <td>&pound;<?php echo $item['product_price']; ?></td>
      <td style="text-align: right;">&pound;<?php echo $item['sub_total']; ?></td>
    </tr>
  <?php endforeach; ?>
</table>

Try building on top of that. :slight_smile:

Have you checked that the query is exectuting ok? If the query is failing them mysql_num_rows() won’t be getting a valid result set to work with

Hey dude, yeah the query is working as it’s displaying ‘You cannot delete any further rows!’ when a user is trying to delete the last row. My problem is that when you press submit, it does indeed delete the item but doesn’t show until a refresh.

I want it to delete the record (as it is) and then to run through the rest of the script and drag up the results from the database. My problem seems to stem from having $rowNums at the top. I’m trying hard to figure out the error of my ways…! :slight_smile:

Try this Kriss. :slight_smile:


<?php
if('POST' === $_SERVER['REQUEST_METHOD']){
  $hasItemsToDelete = false === empty($_POST['delete']) && is_array($_POST['delete']);
  if($hasItemsToDelete){
    $idCollection = array_map(
      'intval',
      $_POST['delete']
    );
    mysql_query(sprintf(
      'DELETE FROM order_products WHERE id IN (%s);',
      implode(',', $idCollection)
    ));
  }
  header('http://www.example.org/this-script.php', true, 301);
  exit;
}

function getInvoiceItems($ref){
  $records = array();
  $result = mysql_query(sprintf(
    "SELECT * FROM order_products WHERE receipt_id = '%s';",
    mysql_real_escape_string($ref)
  ));
  if(false === is_resource($result)){
    return $records;
  }
  while($record = mysql_fetch_assoc($result)){
    array_push($records, $record);
  }
  return $records;
}

$items = getInvoiceItems('Anthony-Order-01');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>
      SitePoint - Demo
    </title>
  </head>
  <body>
    <?php if(0 === count($items)): ?>
      <p>
        You have no items available for deletion.
      </p>
    <?php else: ?>
      <form action="this-script.php" method="post">
        <p>
          You can delete <?php echo count($items); ?> items.
        </p>
        <table style="width: 100%" cellpadding="5" cellspacing="0">
          <tr>
            <td><b>Delete</b></td>
            <td><b>Qty</b></td>
            <td><b>Product Description</b></td>
            <td><b>Condition</b></td>
            <td><b>VAT</b></td>
            <td><b>Item Price</b></td>
            <td style="text-align: right;"><b>Subtotal</b></td>
          </tr>
          <?php foreach($items as $item): ?>
            <tr>
              <td><input type="checkbox" name="delete[]" value="<?php echo $item['id']; ?>"></td>
              <td><?php echo $item['product_quant']; ?></td>
              <td><?php echo $item['product_name']; ?></td>
              <td><?php echo $item['product_cond']; ?></td>
              <td>&pound;<?php echo $item['vat']; ?></td>
              <td>&pound;<?php echo $item['product_price']; ?></td>
              <td style="text-align: right;">&pound;<?php echo $item['sub_total']; ?>
              </td>
            </tr>
          <?php endforeach; ?>
        </table>
        <input type="submit" value="Delete Selected" />
      </form>
    <?php endif; ?>
  </body>
</html>

You’ll need to alter some parts.

Hey bud, sorry didn’t see your original message! Brilliant, I’ll have a look through it and alter as needed. Will let you know how I get on! Cheers Anthony! :slight_smile:

Hey dude, just a quick update, all working perfectly. Thank you for your help :slight_smile: I’ve changed from using a foreach to your method of using the array_map - works really well. Cheers again bud! :smiley: