我有删除文章的问题。我有2个文件(article.php,delete.php)。当我在article.php中需要文件delete.php时,它不是删除文章,而是当我自己打开它时..只是delete.php,它有效。我不知道为什么会这样。有人可以帮帮我吗?谢谢。下面的文件。
这是article.php:
<?php
session_start();
include_once('../connect.php');
include_once('admin.php');
include_once('../includes/article.php');
if(isset($_SESSION["user_id"])){
$query=$pdo->prepare("SELECT * FROM users WHERE id =
".$_SESSION["user_id"]);
$query->execute();
$row=$query->fetch(PDO::FETCH_ASSOC);
if($row['privileges']==1){
?>
<html>
<head>
<title>CMS Tutorial</title>
<link rel="stylesheet" href="assets/style.css" />
</head>
<body>
<br/>
<div class="options">
<img src="../images/add.png" width="20px" height="20px" style="float:left;"><form method="post"><input class="article" type="submit" name="add" value="Pridať článok">
</form>
<img src="../images/delete.png" width="20px" height="20px" style="float:left;"><form method="post"><input class="article" type="submit" name="delete" value="Odstrániť článok"></form>
<img src="../images/edit.png" width="20px" height="20px" style="float:left;"><form method="post"><input class="article" type="submit" name="edit" value="Upraviť článok"></form>
</div>
<?php
if(isset($_POST['add'])){
require_once('add.php');
}else{
if(isset($_POST['delete'])){
require_once('delete.php');
}else{
if(isset($_POST['edit'])){
require_once('edit.php');
}}}?>
</body>
</html>
<?php
} else{
header('Location: ../index.php');
}
} else{
header('Location: ../index.php');
}
?>
这是delete.php:
<?php
session_start();
include_once('../connect.php');
include_once('../includes/article.php');
$article= new Article;
if(isset($_SESSION['user_id'])){
if(isset($_POST['id'])){
$id=$_POST['id'];
$query=$pdo->prepare('DELETE FROM articles WHERE article_id=?');
$query->bindValue(1,$id);
$query->execute();
header('Location: ../index.php');
}
$articles=$article->fetch_all();
?>
<html>
<head>
<link rel="stylesheet" href="assets/style.css" />
</head>
<body>
<div class="container">
<br/>
<h4>Zvoľte článok, ktorý chcete odstrániť:</h4>
<form action="" method="post" class="addarticle">
<select name="id">
<?php foreach($articles as $article){?>
<option value="<?php echo $article['article_id']; ?>"><?php echo
$article['article_title']; ?></option>
<?php } ?>
</select>
<input type="submit" value="Odstrániť článok">
</form>
</div>
</body>
</html>
<?php
} else{
header('Location: ../index.php');
}
?>
答案 0 :(得分:0)
您应该确保正好调用session_start
一次:
if(session_id() == '') {
session_start();
}
现在你的问题。当您点击删除按钮时,系统会提交form
,并且发送的唯一数据是 delete =Odstrániťčlánok,此处不会发送id
。您需要像这样更改表单:
<img src="../images/delete.png" width="20px" height="20px" style="float:left;"><form method="post"><input class="article" type="submit" name="delete" value="Odstrániť článok"><input type="hidden" name="id" value="<?php /*echo id of the article to be removed*/ ?>"></form>
如果您这样做,点击删除按钮后,id
也将被发送到服务器。您的删除现在执行没有问题,但您提供给它的id
有问题。