Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A delete button for each field

Tags:

php

I have a table of items from my database and for each item I want a delete button.

Currently it IS working, but the button shows the ID number, and I want it to just say "delete".

I still want it to delete based on the joke.ID

$result = mysqli_query($con, "SELECT joke.id, joketext, jokedate, name, email FROM joke INNER JOIN author ON authorid = author.id");

echo "<form action='delete1.php' method='post'>
<table border='1'>
   <tr>
      <th>Joke</th>
      <th>Date</th>
      <th>Name</th>
      <th>Email</th>
      <th>Delete</th>
   </tr>";

   while( $row = mysqli_fetch_array( $result ) ) {
      echo "<tr>";
      echo "<td>" . $row['joketext'] . "</td>";
      echo "<td>" . $row['jokedate'] . "</td>";
      echo "<td>" . $row['name'] . "</td>";
      echo "<td>" . $row['email'] . "</td>";
      echo "<td><input type='submit' name='deleteItem' value='".$row['id']."' /></td>";
      echo "</tr>";
   }
   echo "</table>";
   echo "</form></br>";

   mysqli_close($con);
?>

this is the delete1.php

    if ( isset( $_POST['deleteItem'] ) and is_numeric( $_POST['deleteItem'] ) ) {
        // here comes your delete query: use $_POST['deleteItem'] as your id
        mysqli_query($con,"DELETE FROM joke WHERE id='$_POST[deleteItem]'");
    }
    mysqli_close( $con );
    header('Location: joke1.php');
    exit();
?>

I would just like the button to say "delete"

like image 374
Kimmy Benton Avatar asked Aug 28 '13 03:08

Kimmy Benton


Video Answer


1 Answers

Go other way round. Make a hidden input element as follows:

echo "<input type='hidden' name='deleteItem' value='".$row['id']."'>";

And change the existing one to

echo "<td><input type='submit' value='Delete' /></td>";

That's it.

EDIT1: You'll have to keep a different form for each Joke.

while($row = mysqli_fetch_array($result))
{

    echo "<tr>";
    echo "<td>" . $row['joketext'] . "</td>";
    echo "<td>" . $row['jokedate'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['email'] . "</td>";
    echo "<td>";
    echo "<form action='delete1.php' method='post'>
    echo "<input type='hidden' name='deleteItem' value='".$row['id']."'>";
    echo "<input type='submit' value='Delete' />";
    echo "</form>";
    echo "</td>";
    echo "</tr>";
}
like image 50
kushpf Avatar answered Oct 17 '22 19:10

kushpf