Domanda

Im fetching data from database table when i m trying to delete a particular record no matter what i click last record get deleted what m i doing wrong, this is code m using after submit/click del button

// DELETE

if(isset($_POST['del']))
{
require'conn.php';
$delete_id = $_POST['del_id'];
print_r($_POST);
die;

$del_stmt = "DELETE FROM signup WHERE ID =$delete_id";
mysqli_query($conn,$del_stmt);
mysqli_execute($del_stmt);
$row=mysqli_affected_rows($conn);
if($row==1)
{
echo "<h1>".' sucess ! record was deleted' ."</h1>";
}
else
{
echo "<h1>".' record was not deleted '."</h1>";
}

mysqli_close($conn);
}
include'fetchtable.php';

and this is my table structure and del button code

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">

<?php
echo "<table border='1' cellpadding='2' cellspacing='2'";
echo "<tr><td>ID</td><td>First Name</td><td>Last Name</td><td>Gender</td><td>Email</td><td>Password</td><td>Delete</td><td>Edit</td>";
while (mysqli_stmt_fetch($stmt))
{
echo"<tr>";
echo "<td>".$id."</td>";
echo "<td>". "$fn" ."</td>";
echo "<td>". "$ln" ."</td>";
echo "<td>". "$gen"."</td>";
echo "<td>". "$email"."</td>";
echo "<td>". "$pass" ."</td>";
echo '<td> <input type="hidden" name="del_id" value="'.$id.'" />'. '<input type="submit" name="del" value="delete" /> ';
echo '<td> <input type="hidden" name="edit_id" value="'.$id.'" />'.' <input type="submit" name="edit" value="edit" /> ';
echo"</tr>";
}
?>
</form>
È stato utile?

Soluzione

As Abhik Chakraborty says, you need a form for each row, or an other logic;

one solution is to put the <form ...>...</form> within the loop:

this is - AFAIK - not correct, in does not conform to HTML std. and works only with some browsers, bcause <table> and <form> ar mixed in wrong order, I use it only as egsample to show you problem

<?php
echo "<table border='1' cellpadding='2' cellspacing='2'";
echo "<tr><td>ID</td><td>First Name</td><td>Last Name</td><td>Gender</td><td>Email</td><td>Password</td><td>Delete</td><td>Edit</td>";
while (mysqli_stmt_fetch($stmt))
{
echo "<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">"
echo"<tr>";
echo "<td>".$id."</td>";
echo "<td>". "$fn" ."</td>";
echo "<td>". "$ln" ."</td>";
echo "<td>". "$gen"."</td>";
echo "<td>". "$email"."</td>";
echo "<td>". "$pass" ."</td>";
echo '<td> <input type="hidden" name="del_id" value="'.$id.'" />'. '<input type="submit" name="del" value="delete" /> ';
echo '<td> <input type="hidden" name="edit_id" value="'.$id.'" />'.' <input type="submit" name="edit" value="edit" /> ';
echo"</tr>";
echo "</form>"

I would preferre to have only one hidden field in the form und to set the value of that field with onClick event of the submit button.

only the last lines again:

...
echo '<td> <input type="submit" name="del" value="delete" onclick="form.row_id.value='$the id$';"/>'
echo '<td> <input type="submit" name="edit" value="edit" onclick="form.row_id.value='$the id$';"/>'
echo"</tr>";
}
?>
<input ID='row_id' type="hidden" name="del_id" value="no set till now" />'
</form>

Altri suggerimenti

You need to have multiple for each action and so you need to have the code as

    while (mysqli_stmt_fetch($stmt)){
    echo '<form action="'.htmlspecialchars($_SERVER["PHP_SELF"]).'" method="post">' ;
    echo "<tr>";
    echo "<td>".$id."</td>";
    echo "<td>". "$fn" ."</td>";
    echo "<td>". "$ln" ."</td>";
    echo "<td>". "$gen"."</td>";
    echo "<td>". "$email"."</td>";
    echo "<td>". "$pass" ."</td>";
    echo '<td> <input type="hidden" name="del_id" value="'.$id.'" />'. '<input type="submit" name="del" value="delete" /> ';
    echo '<td> <input type="hidden" name="edit_id" value="'.$id.'" />'.' <input type="submit" name="edit" value="edit" /> ';
    echo "</tr>";
    echo "</form>";

}

Also your code is not safe, you need to use mysqli_real_escape_string() for your POST data. or prepared statement

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top