-2

I am attempting to add a button to my html table that will allow a user to delete a specific row when clicked.

I have the following so far but do not understand why the corresponding database table row is not being deleted when I click the borrar (delete) button.

What part am I missing?

<?php do { ?>
  <table width="263" border="1">
    <tr>
      <td><?php echo $row_Recordset1['name']; ?></td>
      <td><form id="form1" name="form1" method="post" action="">
        <input type="submit" name="borrar" id="borrar" value="Borrar" />
      </form></td>
    </tr>
  </table>
  <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

<?php

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

  if ((isset($_POST['id'])) && ($_POST['id'] != "")) {
  $deleteSQL = sprintf("DELETE FROM carrito WHERE id=%s",
                       GetSQLValueString($_POST['id'], "int"));

  mysql_select_db($database_PY, $PY);
  $Result1 = mysql_query($deleteSQL, $PY) or die(mysql_error());

  $deleteGoTo = "http://fb.com";
  if (isset($_SERVER['QUERY_STRING'])) {
    $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
    $deleteGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $deleteGoTo));
}
}
?>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>
dlporter98
  • 1,590
  • 1
  • 12
  • 18
Naahu Nahub
  • 41
  • 3
  • 8
  • In what way does this attempt not succeed? – David Aug 07 '15 at 17:37
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 07 '15 at 17:37
  • possible duplicate of [how to delete certain row from mysql table?](http://stackoverflow.com/questions/18378190/how-to-delete-certain-row-from-mysql-table) – nomistic Aug 07 '15 at 17:41
  • I know it is deprecated,but I just need to solve this. – Naahu Nahub Aug 07 '15 at 17:51
  • You're not passing a row 'id' from you form. – dlporter98 Aug 07 '15 at 18:27

1 Answers1

0

You're not passing a row 'id' from you form.

<table width="263" border="1">
    <tr>
      <td><?php echo $row_Recordset1['name']; ?></td>
      <td><form id="form1" name="form1" method="post" action="">

        <!-- NEW LINE BELOW  -->
        <input type="hidden" name="id" value="<?php echo $row_Recordset1['id']; ?>" />

        <input type="submit" name="borrar" id="borrar" value="Borrar" />
      </form></td>
    </tr>
  </table>

Your form should look like the example above.

dlporter98
  • 1,590
  • 1
  • 12
  • 18