问题
I'm making a system to display pages in the system in a table and one of the columns is the action column giving people the option to delete the page. I did this:
listpages.php
<?php
$sql = "SELECT * FROM Pages";
$result = $conn->query($sql);
if ($result) {
while($row = $result->fetch_assoc()) {
$pages[] = array_map('htmlspecialchars', $row);
}
}
// PHP is finished now, here's the HTML
?>
<form method="post" action="utils/delpage.php">
<table>
<thead>
<tr>
<th>Page Name</th>
<th>Page Type</th>
<th>Registered on</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach($pages as $page):?>
<tr>
<input type="hidden" name="targetid" value="<?= $page['id'] ?>"></input>
<td><?= $page['pagename'] ?></td>
<td><?= $page['pagetype'] ?></td>
<td><?= $page['regdate'] ?></td>
<td><input type="submit" value="delete" class="red-text material-icons tooltipped" data-tooltip="Delete"><td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</form>
delpage.php
<?php
include ('../../php/db.php');
$id = $_POST['targetid'];
$target = htmlentities($id);
$stmt = $conn->prepare("DELETE FROM Pages WHERE id = (?)");
$stmt->bind_param("i", $page);
$page = $target;
$stmt->execute();
?>
This partially works: I get the trash icon I set to delete records. The problem is, when I click it, it will always delete the last record instead of the record it was said to delete. This is a problem, an not how it was really supposed to work. I don't see why it's doing this as the id is being "displayed" the same way the other information is. I searched SO for an answer but nothing worked and I didn't see many options but creating a question. Any help?
Thanks in advance
回答1:
If this is one big form then you will have multiple fields all named targetid. That's a problem and the browser is sending the value of the last field. Try small forms like below. Each form could have it's own name by numerating them.
Remove the form from above an place it in the loop.
<?php foreach($pages as $page):
?>
<tr>
<form method="post" action="utils/delpage.php">
<input type="hidden" name="targetid" value="<?= $page['id'] ?>"></input>
<td><?= $page['pagename'] ?></td>
<td><?= $page['pagetype'] ?></td>
<td><?= $page['regdate'] ?></td>
<td><input type="submit" value="delete" class="red-text material-icons tooltipped" data-tooltip="Delete"><td>
</tr>
</form>
<?php endforeach ?>
Here is a simple example of inputs with the same name and the value that is received by PHP:
<form method="post" action="#">
<input name="a" value="1">
<input name="a" value="2">
<button type="submit" value="delete">Always prints second.</button>
</form>
<form method="post" action="#">
<input name="a" value="1">
<button type="submit" value="delete">Returns 1</button>
</form>
<form method="post" action="#">
<input name="a" value="2">
<button type="submit" value="delete">Returns 2</button>
</form>
<?php
if (isset($_POST['a'])) {
echo $_POST['a'];
} else {
echo 'page loaded';
}
回答2:
There is mistake in your delpage.php
, you are passing wrong variable to sql query id parameter.
Look into this for bindParam.
Try this:
delpage.php
<?php
include ('../../php/db.php');
$id = $_POST['targetid'];
$target = htmlentities($id);
$stmt = $conn->prepare("DELETE FROM Pages WHERE id = :id");
$stmt->bindParam(":id", $target);
$page = $target;
$stmt->execute();
?>
来源:https://stackoverflow.com/questions/43645270/php-form-to-delete-records-in-mysqli-when-displaying-then-in-a-table