我创建了两个文件。一个是index.php,另一个是edit.php。我已经将index.php包含在edit.php中。edit.php用于编辑帖子。帖子编辑得很好,但问题是,每当我试图更新时,都会出现错误。"注意,第10行上的未定义变量edit"这是$edit_id = $_GET['edit'];
行,但我在index.php中声明了<td><a href="edit.php?edit=<?php echo $id; ?>">Edit</a></td>
,我还声明了一个更新数据的查询。但每当我试图更新数据时。它不会更新其他已删除的内容。这是我的index.php文件。如有任何帮助,我们将不胜感激。谢谢和问候,
<!DOCTYPE html>
<?php
session_start();
if(!isset($_SESSION['user_name'])) {
header("location: login.php");
} else {
?>
<html>
<head>
<link rel="stylesheet" href="admin_style.css">
<title>Admin Panel</title>
</head>
<body>
<header>
<h1><a href="index.php"> Welcome to Admin Panel</a> </h1>
</header>
<h3 align="center">This is Admin Area</h3>
<aside>
<h3>Welcome <?php echo $_SESSION['user_name']; ?></h3>
<h2>Manage Content</h2>
<p><a href="index.php?view=view">View Posts</a></p>
<p><a href="index.php?insert=insert">Insert Posts</a></p>
<p><a href="logout.php">Logout</a></p>
</aside>
<?php
if(isset($_GET['insert'])){
include("Post.php");
}
?>
<?php if(isset($_GET['view'])) { ?>
<table width="1000" align="center" border="1">
<tr>
<td align="center" colspan="9"><h1>View all Posts</h1></td>
</tr>
<tr align="center">
<th>Post No</th>
<th>Post Title</th>
<th>Post Date</th>
<th>Post Author</th>
<th>Post Image</th>
<th>Post Content</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
include("connect.php");
if(isset($_GET['view'])) {
$query = "SELECT * FROM posts order by 1 DESC";
$run = mysqli_query($con, $query);
$i=1;
while($row=mysqli_fetch_array($run)) {
$id = $row['Post_id'];
$title = $row['Post_title'];
$date = $row['Post_date'];
$author = $row['Post_author'];
$image = $row['Post_image'];
$content = substr($row['Post_content'],0,50);
?>
<tr align="center">
<td><?php echo $i++; ?></td>
<td><?php echo $title; ?></td>
<td><?php echo $date; ?></td>
<td><?php echo $author; ?></td>
<td><img src="../images/<?php echo $image; ?>" width="50" height="50" /> </td>
<td><?php echo $content; ?></td>
<td><a href="edit.php?edit=<?php echo $id; ?>">Edit</a></td>
<td><a href="delete.php?del=<?php echo $id; ?>">Delete</a></td>
</tr>
<?php
}
}
}
?>
</table>
</body>
</html>
<?php } ?>
edit.php代码在这里:
<!DOCTYPE html>
<html>
<body>
<?php
include("index.php");
include("connect.php");
$edit_id = $_GET['edit'];
$query = "SELECT * FROM posts where Post_id = '$edit_id'";
$run = mysqli_query($con, $query);
while($row=mysqli_fetch_array($run)) {
$edit_id1 = $row['Post_id'];
$title = $row['Post_title'];
$date = $row['Post_date'];
$author = $row['Post_author'];
$image = $row['Post_image'];
$content = $row['Post_content'];
?>
<form method="post" action="edit.php?edit_form=<?php echo $edit_id1;?>" enctype="multipart/form-data">
Post Title:     <input type="text" name="Title" size="50" value="<?php echo $title; ?>" required /> <br />
Post Author: <input type="text" name="Author" size="50" value="<?php echo $author; ?>" required /> <br />
Post Image: <input type="file" name="Image" /><img src="../images/<?php echo $image; ?>" width="60" height="60"/> <br />
Post Content: <textarea name="Content" cols="70" rows="20" >
<?php echo $content; ?>
</textarea> <br />
<input type="submit" name="update" value="update" /> <br />
</form>
</body>
</html>
<?php
if(isset($_POST['update'])) {
$update_id = $_GET['edit_form'];
$post_title = $_POST['title'];
$post_date = date('y-m-d');
$post_author = $_POST['author'];
$post_content = $_POST['content'];
$post_image = $_FILES['image']['name'];
$post_image_type = $_FILES['image']['type'];
$post_image_size = $_FILES['image']['size'];
$post_image_tmp = $_FILES['image']['tmp_name'];
move_uploaded_file($post_image_tmp,"../images/$post_image");
$update_query = "update posts set Post_title='$post_title',Post_date='$post_date',Post_author='$post_author',Post_image='$post_image',Post_content='$post_content' where Post_id='$update_id' ";
if(mysqli_query($con,$update_query)) {
echo "<script>alert('Post has been updated')</script>";
}
}
?>
<?php } ?>
问题与您的表单有关。当提交此表单时,您上面的$edit_id = $_GET['edit'];
未定义。
你应该把它更新到
if(!empty($_GET['edit'])) {
//next trust your users; http://php.net/manual/en/mysqli.real-escape-string.php
$edit_id = mysqli_real_escape_string($con, $_GET['edit']);
$query = "SELECT * FROM posts where Post_id = '$edit_id'";
$run = mysqli_query($con, $query);
while($row=mysqli_fetch_array($run)) {
$edit_id1 = $row['Post_id'];
$title = $row['Post_title'];
$date = $row['Post_date'];
$author = $row['Post_author'];
$image = $row['Post_image'];
$content = $row['Post_content'];
}
然后,在您的表单中,name属性是按标题大小写的,但在PHP中,它们的大小写较低。你需要选择一个命名约定并坚持下去。所以要么。。。。。。
<form method="post" action="edit.php?edit_form=<?php echo $edit_id1;?>" enctype="multipart/form-data">
Post Title:     <input type="text" name="title" size="50" value="<?php echo $title; ?>" required /> <br />
Post Author: <input type="text" name="author" size="50" value="<?php echo $author; ?>" required /> <br />
Post Image: <input type="file" name="image" /><img src="../images/<?php echo $image; ?>" width="60" height="60"/> <br />
Post Content: <textarea name="content" cols="70" rows="20" >
<?php echo $content; ?>
</textarea> <br />
<input type="submit" name="update" value="update" /> <br />
</form>
或
$update_id = $_GET['edit_form'];
$post_title = $_POST['Title'];
$post_date = date('y-m-d');
$post_author = $_POST['Author'];
$post_content = $_POST['Content'];
$post_image = $_FILES['Image']['name'];
$post_image_type = $_FILES['Image']['type'];
$post_image_size = $_FILES['Image']['size'];
$post_image_tmp = $_FILES['Image']['tmp_name'];