Delete button for each table row

前端 未结 3 512
小蘑菇
小蘑菇 2021-01-13 21:18

I manage to succesfully read and display data from my database with the following code: http://pastebin.com/rjZfBWZX I also generate a delete button for each row of the tabl

相关标签:
3条回答
  • 2021-01-13 21:35

    Let's try and do it with _GET instead of _POST.

    In your main code you need to change line 39 (the input) from:

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

    to:

        echo  "<td><a href='obrisi.php?id=$id'>Delete</a></td>";
    

    In your obrisi.php change line 3 (the id setup) from:

        $id = $_POST['id'];
    

    to:

        $id = $_GET['id'];
    

    And finally as a nice addition, redirect back to the main page by adding the following line at the end of the obrisi.php file before the closing of the php tag.

        header('location:index.php');
    

    Where index.php the name of the main page you have.

    0 讨论(0)
  • 2021-01-13 21:36
    echo  "<td>" . " <input type='submit' id= '$id' . ' value='Delete' >" .  "</td>";
    

    some simple errors here. this would output (with $id = 1):

    <td><input type='submit' id= '1' . ' value='Delete' ></td>
    

    this line should be corrected to

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

    this is also going wrong.

    echo "<form action="obrisi.php" method="post">";
    

    should be like:

    echo '<form action="obrisi.php" method="post">';
    

    But the main problem is that there is no field id given in the post. The id of a html element is not sent on submit. it is basically to identify that element in the HTML structure.

    And when using a submit button you will have to limit the scope of the form to that row and use a hidden input field, or use a link like thanpa suggests

    to clarify: if you want to do it with a post (but i would sugget using the $_GET)

    while ($row = mysqli_fetch_array($result) )
    {
    
    $id = $row['id'];
    
    echo "<tr>";
    echo '<form action="obrisi.php" method="post">';
    echo "<td>" . $row['Ime'] . "</td>";
    echo "<td>" . $row['Prezime'] . "</td>";
    echo "<td>" . $row['Grad'] . "</td>";
    echo "<td>" . $row['Drzava'] . "</td>";
    echo "<td>" . $row['Obavijesti'] . "</td>";
    echo "<td>" . $row['Tekst'] . "</td>";
    echo "<td>"
    echo  '<td><input type="hidden" name="id" value="' . $id . '"/><input type="submit" value="Delete" ></td>';
    echo "</form>
    echo "</tr>";
    }
    

    and remove the echo's for the form from start and end of script.

    0 讨论(0)
  • 2021-01-13 21:50

    as an additional note here, if this is going to be at some point being used in a live system you need to be checking $id in obrisi.php that it is actually an ID and not something nasty and unexpected like more sql, look up sql injection.

    0 讨论(0)
提交回复
热议问题