【问题标题】:PHP update data to mysql (ERROR with undefined variable)PHP将数据更新到mysql(未定义变量的错误)
【发布时间】:2017-02-24 18:13:49
【问题描述】:

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

<?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>";
		}
		
	}
	
}

?>

这是错误。它说未定义的变量:

【问题讨论】:

  • 嗯,这些变量在您使用它们时并没有设置。这可能意味着isset($_GET['edit']) 为假(未设置),或者$edit_id 没有指向数据库中记录的值。请注意,当您到达作为更新提交的页面时,您正在执行 POST,并且只有在 GET 的情况下才会设置变量
  • 如果这样做:if(isset($_GET['edit'])) { var_dump($_GET['edit']);exit; ..... 运行时返回的值是多少?该值是与数据库中某行的post_id 匹配的整数吗?
  • 试试这个$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.”'";
  • @CraigvanTonder 是的,它匹配。
  • @RahulSaxena 抱歉,我试过了,还是不行。

标签: php html mysql sql


【解决方案1】:

在我看来,在你第一次加载脚本时,一切都很好,因为你得到了一个页面。此时, isset($_GET['edit']) 将 true 。

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

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

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

对不起我的英语。

【讨论】:

  • 我明白了。那我怎么改呢?
  • 对不起,我该如何更改?
  • 在您的情况下,我认为您应该将 html 表单块移到 if(isset($_GET['edit'])) { ..... } 内部。此链接可能有用:@987654321 @
  • 是的,已经没有未定义的变量错误。感谢您的解决方案。当我点击更新时,它说帖子已更新。但是我的数据库根本没有改变。知道为什么吗?
  • 尝试打印你的 sql,在这种情况下是 $update_query 变量。并直接从您的数据库服务器运行它。您还可以调试为什么您的查询不受影响?使用 try catch,查看错误日志,var_dump($result), ....
猜你喜欢
  • 2013-02-25
  • 2013-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-24
  • 1970-01-01
  • 1970-01-01
  • 2021-07-21
相关资源
最近更新 更多