**I can't update inserted data via GUI, Its working well on inserting records before I modify it, and when am trying to modify in order to update inserted data its not update as I expect to happen. I can display inserted data well by echo them to GUI

Below is the update form that Am trying to create

Please help me. Am just a beginner, thanks**

<?php

include ("db_con.php");

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

    $update_id = $post_id;

    //getting the text data from the fields
    $post_title = $_POST["post_title"];
    $category_id = $_POST["category_id"];
    $post_author = $_POST["post_author"];
    $post_keywords = $_POST["post_keywords"];
    $post_content = $_POST["post_content"];

    //getting the image form the field
    $post_image = $_FILES['post_image']['name'];
    $post_image_tmp = $_FILES['post_image']['tmp_name'];

    move_uploaded_file($post_image_tmp,"news_images/$post_image");

    $update = "update posts set
    post_title='$post_title', post_date=CURDATE(), category_id='$category_id', post_author='$post_author', post_keywords='$post_keywords', post_image='$post_image', post_content='$post_content' where post_id='$update_id'";

    //$query=mysqli_query($con, $sql);
    $run = mysqli_query($con, $update);

    if($run)
    {
        echo "<script>alert('Product has been updated')</script>";
    }
    else
    {
        echo "<script>alert('There is something wrong!')</script>";
    }

}

?>

<!doctype html>
<html>
<head>
<!-- starting of text editor -->
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
<script>tinymce.init({ selector:'textarea' });</script>
<!-- ending of text editor -->

<meta charset="utf-8">
<title>Insert Post Form</title>

</head>

<body>
<?php
include("db_con.php");

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

        $edit_id = $_GET['edit_post'];

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

        $run_query = mysqli_query($con, $select_post);

        while ($row_post=mysqli_fetch_array($run_query)){

        $post_id = $row_post['post_id'];
        $title = $row_post['post_title'];
        $post_cat = $row_post['category_id'];
        $author = $row_post['post_author'];
        $keywords = $row_post['post_keywords'];
        $image = $row_post['post_image'];
        $content = $row_post['post_content'];

        }
    }

?>

<div class='form'>
<form action="" method="post" enctype="multipart/form-data">

    <table width="800" align="center" border="2">   

    <tr>        
        <td colspan="6" align="center" bgcolor="#FF6600"><h1>Update Post:</h1></td> 
    </tr>

    <tr>        
        <td align="right" bgcolor="#FF6600"><strong>Post Title:</strong></td>
        <td><input type="text" name="post_title" size="60" value="<?php echo $title; ?>"/></td>     
    </tr>

    <tr>        
        <td align="right" bgcolor="#FF6600"><strong>Post Categories:</strong></td>
        <td>            
            <select name="category_id">             

                <?php       

         $get_cats = "select * from categories where cat_id='$post_cat'";        
         $run_cats = mysqli_query($con, $get_cats);

         while ($cats_row=mysqli_fetch_array($run_cats)){

             $cat_id=$cats_row['cat_id'];
             $cat_title=$cats_row['cat_title'];

             echo "<option value='$cat_id'>$cat_title</option>";         
         }      
             $get_more_cats = "select * from categories";

             $run_more_cats = mysqli_query($con, $get_more_cats);

             while($row_more_cats=mysqli_fetch_array($run_more_cats)){

             $cat_id=$row_more_cats['cat_id'];
             $cat_title=$row_more_cats['cat_title'];

             echo "<option value='$cat_id'>$cat_title</option>";     

             }

                ?>              
            </select>       
        </td>
    </tr>

    <tr>        
        <td align="right" bgcolor="#FF6600"><strong>Post Author:</strong></td>
        <td><input type="text" name="post_author" size="60" value="<?php echo $author; ?>" /></td>      
    </tr>

    <tr>        
        <td align="right" bgcolor="#FF6600"><strong>Post Keywords:</strong></td>
        <td><input type="text" name="post_keywords" size="60" value="<?php echo $keywords; ?>" /></td>          
    </tr>

    <tr>
        <td align="right" bgcolor="#FF6600"><strong>Post Image:</strong></td>
        <td><input type="file" name="post_image" size="50"/> <img src="news_images/<?php echo $image; ?>" width="60" height="60" /></td>
    </tr>

    <tr>
        <td align="right" bgcolor="#FF6600"><strong>Post Content:</strong></td>
        <td><textarea name="post_content" rows="15" cols="40"><?php echo $content; ?></textarea></td>
    </tr>

    <tr>
        <td colspan="6" align="center" bgcolor="#FF6600"><input type="submit" name="update" value="Update Now"></td>        
    </tr>

    </table>

</form>
</div>

</body>
</html>

Recommended Answers

All 2 Replies

Hi,

at line 7 you have:

$update_id = $post_id;

while $post_id is initialized at line 68:

$post_id = $row_post['post_id'];

Which in practice depends on $edit_id defined at line 60:

$edit_id = $_GET['edit_post'];

So, it seems that you open the page like this:

page.php?edit_post=123

All you have to do is to initialize $edit_id on top, at line 4, so that is available to the POST conditional statement and to the other code.

Do not use $_GET directly, filter the variable:

$edit_id = filter_input(INPUT_GET, 'edit_post', FILTER_VALIDATE_INT, ['options' => ['default' => NULL]]);

Then replace:

$update_id = $post_id;

With:

$update_id = $edit_id;

Or simply adjust the following code to use $edit_id. Use the filter functions also for the other input coming from POST and GET requests, and use prepared statements too:

**Thank you very much @cereal. You're my Hero. It works well now!

I follow your instruction to customize it like this: **

    <?php

    include ("db_con.php");
    $edit_id = filter_input(INPUT_GET, 'edit_post', FILTER_VALIDATE_INT, ['options' => ['default' => NULL]]);
    if(isset($_POST['update'])){

        $update_id = $edit_id;

        //getting the text data from the fields
        $post_title = $_POST["post_title"];
        $category_id = $_POST["category_id"];
        $post_author = $_POST["post_author"];
        $post_keywords = $_POST["post_keywords"];
        $post_content = $_POST["post_content"];

        //getting the image form the field
        $post_image = $_FILES['post_image']['name'];
        $post_image_tmp = $_FILES['post_image']['tmp_name'];

        move_uploaded_file($post_image_tmp,"news_images/$post_image");

        $update = "update posts set
        post_title='$post_title', post_date=CURDATE(), category_id='$category_id', post_author='$post_author', post_keywords='$post_keywords', post_image='$post_image', post_content='$post_content' where post_id='$update_id'";

        //$query=mysqli_query($con, $sql);
        $run = mysqli_query($con, $update);

        if($run)
        {
            echo "<script>alert('Product has been updated')</script>";
        }
        else
        {
            echo "<script>alert('There is something wrong!')</script>";
        }

    }

    ?>

    <!doctype html>
    <html>
    <head>
    <!-- starting of text editor -->
    <script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
    <script>tinymce.init({ selector:'textarea' });</script>
    <!-- ending of text editor -->

    <meta charset="utf-8">
    <title>Insert Post Form</title>

    </head>

    <body>
    <?php
    include("db_con.php");

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

            $edit_id = $_GET['edit_post'];

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

            $run_query = mysqli_query($con, $select_post);

            while ($row_post=mysqli_fetch_array($run_query)){

            $post_id = $row_post['post_id'];
            $title = $row_post['post_title'];
            $post_cat = $row_post['category_id'];
            $author = $row_post['post_author'];
            $keywords = $row_post['post_keywords'];
            $image = $row_post['post_image'];
            $content = $row_post['post_content'];

            }
        }

    ?>

    <div class='form'>
    <form action="" method="post" enctype="multipart/form-data">

        <table width="800" align="center" border="2">   

        <tr>        
            <td colspan="6" align="center" bgcolor="#FF6600"><h1>Update Post:</h1></td> 
        </tr>

        <tr>        
            <td align="right" bgcolor="#FF6600"><strong>Post Title:</strong></td>
            <td><input type="text" name="post_title" size="60" value="<?php echo $title; ?>"/></td>     
        </tr>

        <tr>        
            <td align="right" bgcolor="#FF6600"><strong>Post Categories:</strong></td>
            <td>            
                <select name="category_id">             

                    <?php       

             $get_cats = "select * from categories where cat_id='$post_cat'";        
             $run_cats = mysqli_query($con, $get_cats);

             while ($cats_row=mysqli_fetch_array($run_cats)){

                 $cat_id=$cats_row['cat_id'];
                 $cat_title=$cats_row['cat_title'];

                 echo "<option value='$cat_id'>$cat_title</option>";         
             }      
                 $get_more_cats = "select * from categories";

                 $run_more_cats = mysqli_query($con, $get_more_cats);

                 while($row_more_cats=mysqli_fetch_array($run_more_cats)){

                 $cat_id=$row_more_cats['cat_id'];
                 $cat_title=$row_more_cats['cat_title'];

                 echo "<option value='$cat_id'>$cat_title</option>";     

                 }

                    ?>              
                </select>       
            </td>
        </tr>

        <tr>        
            <td align="right" bgcolor="#FF6600"><strong>Post Author:</strong></td>
            <td><input type="text" name="post_author" size="60" value="<?php echo $author; ?>" /></td>      
        </tr>

        <tr>        
            <td align="right" bgcolor="#FF6600"><strong>Post Keywords:</strong></td>
            <td><input type="text" name="post_keywords" size="60" value="<?php echo $keywords; ?>" /></td>          
        </tr>

        <tr>
            <td align="right" bgcolor="#FF6600"><strong>Post Image:</strong></td>
            <td><input type="file" name="post_image" size="50"/> <img src="news_images/<?php echo $image; ?>" width="60" height="60" /></td>
        </tr>

        <tr>
            <td align="right" bgcolor="#FF6600"><strong>Post Content:</strong></td>
            <td><textarea name="post_content" rows="15" cols="40"><?php echo $content; ?></textarea></td>
        </tr>

        <tr>
            <td colspan="6" align="center" bgcolor="#FF6600"><input type="submit" name="update" value="Update Now"></td>        
        </tr>

        </table>

    </form>
    </div>

    </body>
    </html>

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.