1

This is the part of the code: It first retrieves the information according to the id. but When i change one of the information and clicked update, it gives me undefined variable error and couldnt update at all.

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

?>

Here is the Error. It says undefined variable:

enter image description here

u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • 1
    Well, these variables aren't set at the time you use them. That probably means that either `isset($_GET['edit'])` is false (not set), or that `$edit_id` doesn't have a value that points to a record in the db. Notice that when you're arriving on the page when it's submitted as update, you're doing a POST, and the variables will only be set in case of a GET – fvu Oct 15 '16 at 10:45
  • If you do: `if(isset($_GET['edit'])) { var_dump($_GET['edit']);exit; .....` What is the value that you get back when running it? Is that value an integer that matches a `post_id` of a row in your database? – Craig van Tonder Oct 15 '16 at 10:52
  • try this `$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.”'";` – Rahul Saxena Oct 15 '16 at 10:59
  • @CraigvanTonder Yes, it matches. – Yap Jun Hong Oct 16 '16 at 02:13
  • @RahulSaxena Sorry, i tried and still doesn't work. – Yap Jun Hong Oct 16 '16 at 02:14

1 Answers1

0

In my opinion, at the first you load script, everything is ok because you GET a page. At this time, isset($_GET['edit']) would true .

After you clicked update (I guess you submit page itself). At this point, isset($_GET['edit']) would false because you're doing POST. So, block code inside

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

will be skipped. And those variables would be undefined.

Sorry for my english.

Sam
  • 1
  • 2
  • i see. so how do i change it? – Yap Jun Hong Oct 16 '16 at 01:29
  • Sorry, so how do i change it? – Yap Jun Hong Oct 16 '16 at 02:15
  • In your case, I think you should move html form block into inside if(isset($_GET['edit'])) { ..... } .This link maybe useful: http://www.yourwebskills.com/phpformssingle.php – Sam Oct 16 '16 at 03:33
  • yeah there is no undefined variable error already.Thanks for the solution. When i Clicked update, it says the the post is updated. But my database didnt change at all. Any idea why? – Yap Jun Hong Oct 16 '16 at 06:46
  • Try print your sql, in this case is $update_query variable. And run it directly from your database server. You also could debug why your query is not affected? use try catch, view error log, var_dump($result), .... – Sam Oct 16 '16 at 09:21