-3
我目前正在尝试将数据上传到一个数据库,我已经成功完成了这项工作。然后我将其显示在编辑页上。php,并可以选择删除已输入数据库的任何数据。问题是,每次我从数据库中删除数据行时,我的页面都会刷新,然后相同的数据会重新输入到数据库中。我如何防止这种情况发生?
这是我的编辑页。php,允许插入、查看和删除数据。
<?php
session_start();
$connect = mysqli_connect("localhost", "root", "", "skilltask2");
if ($_SESSION["loggedin"] == false) {
header(\'Location: skillsTask2.php\');
}
if(isset($_POST[\'content\']) || isset($_POST[\'header\']) || isset($_POST[\'image\'])){
$displayText = $_POST[\'content\'];
$displayHeader = $_POST[\'header\'];
$dir="images/";
$file=$dir.basename($_FILES["image"]["name"]);
$query = "INSERT INTO `content` (`image`) VALUES (\'$file\')";
if(strlen($displayText)>0){
$query = "INSERT INTO `content` (`text`, `header`, `image`) VALUES (\'$displayText\', \'$displayHeader\', \'$file\')";
}
if (mysqli_query($connect, $query)) {
echo \'<script>alert("Title, Text and Image has been uploaded to database")</script>\';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Page</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="editPage.css">
<script>
function deleteContent(id,header) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "processDelete.php?id="+id, true);
xhr.send();
alert("You have deleted "+header);
location.reload();
}
</script>
</head>
<body>
<br>
<br>
<div class="container" style="width:25vw;">
<h3 align="center">Update and Insert Headers, Text and Images</h3>
<br>
<form method="post" enctype="multipart/form-data">
<h3>Title</h3>
<input type="text" name="header" required />
<br>
<br>
<h3>Text</h3>
<input type="text" name="content" required />
<br>
<br>
<h3>Image</h3>
<input type="file" name="image" required />
<br>
<br>
<br>
<input type="submit" name="all" value="Upload All" />
</form>
<br>
<br>
<?php
echo "<a href=\\"skillsTask2.php\\">Logout</a>";
?>
</div>
<div id="rightSide">
<?php
$conn = mysqli_connect("localhost", "root", "", "skilltask2");
$query = "SELECT * FROM content ORDER BY `id` DESC";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result)) {
$theimage = $row[\'image\'];
$theheader = $row[\'header\'];
$thetext = $row[\'text\'];
$id = $row[\'id\'];
echo "<div id=\'card\'>
<img src=\\"".$theimage."\\" >
<div id=\'cardHeader\'>
<b>$theheader</b><br>$thetext
</div>
<button onclick=deleteContent($id,\'$theheader\')>Delete</button>
</div>
";
}
?>
</div>
</body>
</html>
这是我的processDelete。php页面,从数据库中删除代码行。
<?php
session_start();
$connect = mysqli_connect("localhost", "root", "", "skilltask2");
$id=$_GET[\'id\'];
$query = "DELETE FROM content WHERE id=$id";
$result = mysqli_query($connect,$query) or die ( mysqli_error());
?>
希望你能帮忙。再次感谢。