0

I have the following code which retrieves items from my DB and displays them in a table. The file is manage-products.php.

while($row = mysql_fetch_row($result)){
       echo '</tr>';
       echo ' <td class="product"><a href="manage-products-2.php">'.$row[1].'</a></td>';
       echo'<td class="quantity">'.$row[5].'</td>';
       echo '<td class="item_price">'.$row[4].'</td>';
       echo '<td class="item_total">'.$row[6].'</td>';
       echo '<td class="item_unsold"><a href = "manage-products.php?prod = '.$row[0].'" style="color:red">Delete</a></td>';//to delete an item
       echo '</tr>';
}

I have the following code (which should be executed) when the DELETE link is clicked (also in manage-products.php)

$prodid = $_GET['prod'];

if($prodid != ""){
  echo '<script type="text/javascript"> 
  var r = window.confirm("Are you sure you want to delete this product") ;
  if(r == true){
    $ . post ( "manage-products.php" ,  { result : r });
  }
  </script>';
  $delete = $_POST['result'];
  if($delete == true){
    $SQL1 = "DELETE FROM tbl_product WHERE id = '$prodid'";
    $result1 = mysql_query($SQL1);
  }
}

When i click delete it says undefined index:prod. Please where is my error. Thanks

5

1 Answer 1

0

Do the confirm before reload, and delete the product during reload if it's happens, you'll be more performant.

Like this on the delete link :

echo '<td class="item_unsold"><a href = "manage-products.php?prod='.$row[0].'" style="color:red" onclick="return confirm('Are you sure you want to delete this product ?')">Delete</a></td>';//to delete an item

then when reload :

$prodid = $_GET['prod'];

if($prodid != "" && is_numeric($prodid)){
    $SQL1 = "DELETE FROM tbl_product WHERE id = '$prodid'";
    $result1 = mysql_query($SQL1);
  }
}

Do not forget to sanitize variable (check numeric type) before delete ;)

EDIT :

Replace manage-products.php?prod = '.$row[0] by manage-products.php?prod='.$row[0]

4
  • Thanks but it still does nothing as before. It just prints undefined index: prod.
    – dhani
    May 15, 2013 at 16:34
  • print_r($row) and see what's contained in the 0 index
    – Pouki
    May 15, 2013 at 16:45
  • remove space in the called url, I've edited my post... Retry and it should works
    – Pouki
    May 15, 2013 at 16:47
  • i did print_r($row) contains the ids, so does print_r($_GET). I also removed the space but nothing changed.
    – dhani
    May 15, 2013 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.