gpt4 book ai didi

PHP更新数据到mysql( undefined variable 的错误)

转载 作者:行者123 更新时间:2023-11-30 22:11:16 24 4
gpt4 key购买 nike

这是代码的一部分:它首先根据id检索信息。但是当我更改其中一个信息并单击更新时,它会给我 undefined variable 错误并且根本无法更新。

<?php

include("includes/connect.php");

if(isset($_GET['edit'])) {

$edit_id = $_GET['edit'];

$edit_query = "select * from posts where post_id = '$edit_id' ";

$run_edit = mysqli_query($con,$edit_query);

while ($edit_row=mysqli_fetch_array($run_edit)) {


$post_id = $edit_row['post_id'];
$post_title = $edit_row['post_title'];
$post_author = $edit_row['post_author'];
$post_keywords = $edit_row['post_keywords'];
$post_image = $edit_row['post_image'];
$post_content = $edit_row['post_content'];


}
}
?>



<form method="post" action="edit.php?edit_form=<?php echo $post_id; ?>" enctype="multipart/form-data">

<table width="600" bgcolor="orange" align="center" border="10">

<tr>
<td align="center" bgcolor="yellow" colspan="6">
<h1>Edit The Post Here</h1>
</td>
</tr>


<tr>

<td align="right">Post Title:</td>
<td><input type="text" name="title" size="30" value="<?php echo $post_title; ?>"></td>

</tr>

<tr>

<td align="right">Post Author:</td>
<td><input type="text" name="author" size="30" value="<?php echo $post_author; ?>"></td>

</tr>

<tr>

<td align="right">Post Keywords:</td>
<td><input type="text" name="keywords" size="30" value="<?php echo $post_keywords; ?>"></td>

</tr>

<tr>

<td align="right">Post Image:</td>
<td>
<input type="file" name="image">
<img src="../images/<?php echo $post_image; ?>"width="100" height="100"></td>

</tr>

<tr>

<td align="right">Post Content:</td>
<td><textarea name="content" cols="30" rows="15"><?php echo $post_content; ?></textarea></td>

</tr>

<tr>

<td align="center" colspan="6"><input type="submit" name="update" id="update" value="Update Now"></td>

</tr>

</table>



</form>




</body>
</html>


<?php

if(isset($_POST['update'])) {

$update_id = $_GET['edit_form'];
$post_title1 = $_POST['title'];
$post_date1 = date('m-d-y');
$post_author1 = $_POST['author'];
$post_keywords1 = $_POST['keywords'];
$post_content1 = $_POST['content'];
$post_image1 = $_FILES['image']['name'];
$image_tmp = $_FILES['image']['tmp_name'];

if($post_title1 == '' or $post_author1=='' or $post_keywords1=='' or $post_content1=='' or $post_image1=='') {

echo "<script>alert('Any of the fields is empty')</script>";
exit();

}
else {

move_uploaded_file($image_tmp,"../images/$post_image1");

$update_query = "update posts set post_title='$post_title1', post_date='$post_date1', post_author='$post_author1',post_image='$post_image1',post_keywords='$post_keywords1',post_content='$post_content1' where post_id='$update_id'";

if(mysqli_query($con,$update_query)) {

echo "<script>alert('Post has been updated')</script>";

echo "<script>window.open('view_posts.php','_self')</script>";
}

}

}

?>

这里是错误。它说 undefined variable :

enter image description here

最佳答案

在我看来,第一次加载脚本时,一切正常,因为您获得了一个页面。此时,isset($_GET['edit']) 将为 true

点击更新后(我猜你提交了页面本身)。此时,isset($_GET['edit']) 将 false 因为您正在执行 POST。所以,里面的 block 代码

if(isset($_GET['edit'])) {
.....
}

将被跳过。这些变量将是未定义的。

对不起我的英语。

关于PHP更新数据到mysql( undefined variable 的错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40057965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com