I want to delete mysql rows by clicking on a button in a form. The problem is that I do it in a php while cicyle and the html action attribute does not navigate into the other php page, where the code should delete the row. Here's my code:
if ($result->num_rows > 0) {
echo "<table><tr><th>Vezetéknév</th><th>Keresztnév</th><th>Nemzetiség</th>
<th>Szülőváros</th><th>Találmányok száma</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["vezeteknev"]. "</td><td>" . $row["keresztnev"].
"</td><td>" . $row["nemzetiseg"] ."</td><td>"
. $row["szulovaros"]. "</td><td>". $row['talalmanyok']. "
</td> <td><form action='deleterow.php' method='post'>
<input type='submit' name='delete' value='".$row['id']."'/>
</form></td></tr>";
}
The other page (deleterow.php):
header('location:index.php');
include('DBConnection');
if(isset($_POST['delete'])){
$user = $_POST['delete'];
$delet_query = mysqli_query($conn,"DELETE FROM feltalalo WHERE id = '$user' ") or die(mysql_error());
}
Change your code as like below:
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["vezeteknev"]. "</td><td>" . $row["keresztnev"].
"</td><td>" . $row["nemzetiseg"] ."</td><td>"
. $row["szulovaros"]. "</td><td>". $row['talalmanyok']. "
</td> <td><a href='deleterow.php?delete=".$row['id']."'>Delete</a></td></tr>";
}
deleterow.php
if(isset($_GET['delete'])){
$user = $_GET['delete'];
$delet_query = mysqli_query($conn,"DELETE FROM feltalalo WHERE id = '$user' ") or die(mysql_error());
}
I believe what you want to achieve is to be able to delete an entry using a link, but have it register as a post request.
To achieve this, you need to generate a form just before your link like so
<form name="item_deleter_{YOUR_ID}" style="display:none;" method="post" action="{URL_FOR_DELETING}">
<input type="hidden" name="delete" value="{YOUR_ID}">
</form>
Then after this you will create the delete link to have an onclick which submits the above form using javascript like so
<a href="#" onclick="document.item_deleter_{YOUR_ID}.submit(); event.returnValue = false; return false;">Delete</a>
Cheers
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.