所以我在今年夏天开始使用该语言,我有问题,我不知道如何制作删除按钮,我看了很多页面,但我找不到如何使用pdo。
countryandcity.php
<?php
require 'pdo.php';
$connect=connect();
?><!DOCTYPE html>
<html>
<head>
<link href="style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div class="wrapper">
<table class="table" >
<thead>
<tr>
<th>Country</th>
<th>City</th>
<th>Image</th>
</tr>
<form action="deleteall.php" method="POST" >
<?php
$sql = connect()->prepare("SELECT * FROM countries ORDER BY country");
$sql->execute();
while($result = $sql->fetch(PDO::FETCH_ASSOC)) {
echo"<tr>";
echo"<td>".$result['country']."</td>";
echo"<td>".$result['city']."</td>";
if(!empty($result['image'])){
echo '<td><img src="images/'.$result['image'].'"/></td>';
}
else {
echo"<td>-</td>";
}
echo "<td><a href='edit.php?uid=".$result['country']."'>Edit</a></td>";
echo "<td><a href='deleteall.php?uid=".$result['country']."'>Delete</a></td>";
echo"</tr>";
}
?>
</form>
</thead>
</table>
</div>
</body>
deleteall.php
<?php
require 'pdo.php';
$connect=connect();
if(isset($_POST['delete_btn']))
?><!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form>
<div class="form-group">
<label>Do u want to delete?</label>
</div>
<button>
<input type="submit" value="YES" name="delete_btn">
</button>
<button>
<input type="submit" name="no" value="NO">
</button>
</form>
</body>
</html>
如果(isset)请帮助我使用sql查询和php代码,我需要帮助! 我只想删除数据库中的行。
我该如何解决这个问题? 抱歉我的英语不好。
谢谢!
答案 0 :(得分:0)
以下是对您的第一页的更正(<tbody>
中的内容而不是<thead>
,不需要<form>
,...) countryandcity.php :< / p>
<table>
<thead>
<tr>
<th>column name...</th>
</tr>
</thead>
<tbody>
<?php
$sql = connect()->prepare("SELECT * FROM countries ORDER BY country");
$sql->execute();
if($result = $sql->fetch(PDO::FETCH_ASSOC)) {
foreach($result as $i => $item) {
extract($item);
echo "<tr><td>$country</td>";
echo "<td>$city</td>";
if(!empty($image)) {
echo "<td><img src=\"images/$image\"/></td>";
}
else {
echo "<td>-</td>";
}
echo "<td><a href=\"edit.php?uid=$country\">Edit</a></td>";
echo "<td><a href=\"deleteall.php?uid=$country\">Delete</a></td></tr>";
}
}
?>
</tbody>
</table>
</div>
</body>
</html>
此处可以删除 deleteall.php 中的记录:
<?php
if(isset($_GET['delete_btn'])) {
$country = addslashes($_GET['delete_btn']);
$q = "DELETE FROM countries WHERE country = '$country'";
require 'pdo.php';
$sql = connect()->prepare($q);
$sql->execute();
}
?>
答案 1 :(得分:0)
希望它会对你有所帮助,你可以添加删除链接然后去这个页面它会删除
<a href="http://localhost/edit.php?uid=22" onclick="return confirm('Are you sure?')">Edit</a>
<a href="http://localhost/edit.php?uid=22" onclick="return confirm('Are you sure?')">Delete</a>
<?php
require 'pdo.php';
$connect = connect();
?>
<!DOCTYPE html>
<html>
<head>
<link href="style.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<div class="wrapper">
<table class="table">
<thead>
<tr>
<th>Country</th>
<th>City</th>
<th>Image</th>
</tr>
<form action="deleteall.php" method="POST">
<?php
$sql = connect()->prepare("SELECT * FROM countries ORDER BY country");
$sql->execute();
while ($result = $sql->fetch(PDO::FETCH_ASSOC)) {
if (!empty($result['image'])) {
$content_image = '<td><img src="images/' . $result['image'] . '"/></td>';
} else {
$content_image = '<td>-</td>';
}
?>
<tr>
<td> <?= $result['country'] ?></td>
<td><?= $result['city'] ?></td>
<?= $content_image ?>
<td><a href="edit.php?uid=<?= $result['country'] ?>">Edit</a></td>
<td><a href="deleteall.php?uid=<?= $result['country'] ?>" onclick="return confirm('Are you sure?')">Delete</a>
</td>
</tr>
<?php }
?>
</form>
</thead>
</table>
</div>
</body>
</html>
然后这是你的deleteall.php
<?php
$uid = trim($_GET['uid']);
if(isset($uid)) {
$sql = "DELETE FROM countries WHERE country = ?";
$q = connect()->prepare($sql);
$response = $q->execute(array($uid));
}
?>
答案 2 :(得分:0)
对于表单,我建议您使用一个充当该功能的按钮,这样您在页面加载时不会意外丢失所有数据,或者类似的任何内容。这只是三行代码。
<form action="deleteall.php" method="POST">
<input type="hidden" value="true" name="cameFromForm">
<button type="submit" name="confirmButton">Delete All</button>
</form>
单击该按钮时,它将触发deleteall.php的入口点,然后获取输入并从表中删除所有数据。
<?php
if(isset($_POST["cameFromForm"]) && $_POST["cameFromForm"] == "true") {
// This makes sure that the form actually sent it.
try {
$tableName = "Insert the name of your table here";
$mysqli = new mysqli(
'databaseHost',
'databaseUser',
'databasePassword',
'databaseName');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$stmt = $mysqli->prepare("DELETE FROM ?");
$stmt->bind_param('s', $tableName);
$stmt->execute();
$stmt->close();
$mysqli->close();
header('Location: http://afterDeltePage.com');
}
catch (Exception $e) {
print($e);
die();
}
}
else {
header('Location: http://notAuthorisedPage.com');
}
?>
完成此过程后,您应该将用户发送到带有UI的页面。尝试分离显示用户和流程所需的内容。此外,如果这是公开的,您需要确保任何人都无法通过网址访问deleteall。
为此,您需要将文件移出wwwroot(通常是public_html)。有关如何做到这一点的各种主题。
PHP Forms信息和PHP MySQLi信息。