PHP Still give me old data after form submit

Tugce Aksoz :

I have form for updating some data on my category table. After i post my new data via form, page refresh but it still show old data to me.

public function updateCategory($status, $main_category, $title, $bg_color, $meta_title, $meta_description, $meta_keywords, $id)
{
    $sql = "UPDATE categories SET status = ?, main_category = ? , title = ?, bg_color = ?, meta_title = ?, meta_description = ?, meta_keywords = ? WHERE id = ?";
    $stmt = $this->conn->prepare($sql);
    try {
            $stmt->execute([$status, $main_category, $title, $bg_color, $meta_title, $meta_description, $meta_keywords, $id]);
            return true;
    } catch (Exception $e) {
        echo $e->getMessage();
    return false;
    }
}

Form post area

 $category = new Categories();
 $id = $_GET['id'];
 $query = $category->getCategoryDetails($id);
 $result = false;
 if (isset($_POST['categorySave'])) {

    $result = $category->updateCategory($_POST['status'],$_POST['main_category'],$_POST['title'],$_POST['bg_color'],$_POST['meta_title'],$_POST['meta_description'], $_POST['meta_keywords'], $query->id);

}
?>
<form action="<?=$_SERVER['REQUEST_URI'];?>" method="post" enctype="multipart/form-data">
<?php if ($result) { ?>
    <div class="alert alert-success" role="alert">
        <?=UPDATESUCCESS?>
    </div>
<?php } ?>
<label for="title">Başlık</label>
<input type="text" class="form-control" id="title" name="title" placeholder="Başlık" value="<?=$query->title?>">
        ...

Its update data without problem. As an example if i change title 'Title New', it updates on my database. But after form submit, it stay as the same. 'Title Old'

Omar Abbas :

move update code above the get code.

$result = false;
if (isset($_POST['categorySave'])) {
    $result = $category->updateCategory($_POST['status'],$_POST['main_category'],$_POST['title'],$_POST['bg_color'],$_POST['meta_title'],$_POST['meta_description'], $_POST['meta_keywords'], $query->id);
}

$category = new Categories();
$id = $_GET['id'];
$query = $category->getCategoryDetails($id);

?>
<form action="<?=$_SERVER['REQUEST_URI'];?>" method="post" enctype="multipart/form-data">
<?php if ($result) { ?>
    <div class="alert alert-success" role="alert">
        <?=UPDATESUCCESS?>
    </div>
<?php } ?>
<label for="title">Başlık</label>
<input type="text" class="form-control" id="title" name="title" placeholder="Başlık" value="<?=$query->title?>">
        ...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=360204&siteId=1