在php中定义数据时,数据更新无效且未定义变量错误
我已经创建了两个文件。一个名为index.php,另一个名为edit.php。我已经将index.php包含到edit.php中。php用于编辑帖子。帖子编辑很好,但问题是,每当我尝试更新时,它都会给出我的错误。注意,第10行的未定义变量edit是$edit\u id=$\u GET['edit'];但是我在index.php中声明了一个更新数据的查询。但每当我试图更新数据时。它不会更新,否则会被删除。 这是我的index.php文件。任何帮助都将不胜感激。感谢和问候在php中定义数据时,数据更新无效且未定义变量错误,php,mysql,Php,Mysql,我已经创建了两个文件。一个名为index.php,另一个名为edit.php。我已经将index.php包含到edit.php中。php用于编辑帖子。帖子编辑很好,但问题是,每当我尝试更新时,它都会给出我的错误。注意,第10行的未定义变量edit是$edit\u id=$\u GET['edit'];但是我在index.php中声明了一个更新数据的查询。但每当我试图更新数据时。它不会更新,否则会被删除。 这是我的index.php文件。任何帮助都将不胜感激。感谢和问候 <!DOCTYPE
<!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\u id=$\u 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'];
}
然后,在表单中,名称属性是以标题大小写的,但在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>
第二遍编辑未定义;您只定义编辑表单。哪个SQL不是;t执行?这只是index.php中定义的get,非常感谢@chris85实际上我是新的,并且遵循使用mysql的旧教程。我遇到了一些麻烦。你能澄清一些困惑吗?1.php区分大小写吗?php中的各种东西都区分大小写。。。我发现最好保持小写,如果你想在名称上有所区别,最好使用下划线。我很抱歉再次打扰你最后一个问题@chris85。正如你所看到的,我有内容,我已经在数据库中将其定义为文本。但每当我从网络上复制一些有comas、分号、冒号、引号等的东西时,它就不起作用了。数据不提交。我应该怎么做?最好将输入的数据与SQL分开。你应该研究准备好的陈述。
$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'];