Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shahr's avatar
Level 10

How to update & edit in php mvc?

I want to edit in php mvc.

edit.php

<form action="CategoryController/edit/<?= $CategoryData->id ?>" method="post">
    <div class="form-group">
        <label for="name">name</label>
        <input type="text" id="name" name="name" value="<?= $CategoryData->name ?>" class="form-control">
    </div>
    <div class="form-group">
        <label for="parent_id">parent id</label>
        <select id="parent_id" name="parent_id" class="form-control">
            <?php $categories = $data[0] ?>
            <?php foreach ($categories as $category) { ?>
                <?php
                if ($category->id == $CategoryData->parent_id) {
                    $selected = 'selected';
                } else {
                    $selected = '';
                }
                ?>
                <option value="<?= $category->id ?>" <?= $selected ?>><?= $category->name ?></option>
            <?php } ?>
        </select>
    </div>
    <div class="form-group">
        <button name="update" type="submit" class="btn-save">save</button>
    </div>
</form>

CategoryController.php

function edit($category_id = 0, $edit = null)
{
    if (isset($_POST['update'])) {
        $name = $_POST['name'];
        $parent_id = $_POST['parent_id'];
        $this->model->update($name, $parent_id, $edit, $category_id);
    }

    $categories = $this->model->getCategories();
    $CategoryData = $this->model->CategoryData($category_id);
    $data = [$categories, $category_id,  $CategoryData];
    $this->view('admin/categories/edit', $data);
}

model Category.php

function update ($name, $parent_id, $id)
{
    $sql = "UPDATE categories SET name = ?, parent_id = ? WHERE id = ?";
    $stmt = self::$connection->prepare($sql);
    $stmt->bindValue(1, $name);
    $stmt->bindValue(2, $parent_id);
    $stmt->bindValue(3, $id);
    $stmt->execute();
}
0 likes
7 replies
shahr's avatar
Level 10

My problem is did not saving and I did not any an error.

Tray2's avatar

Does the record you are trying to update exist in the database?

Snapey's avatar

I remember code like this. It was about 2012 and there was no global pandemic. Ah, sweet innocent days...

jlrdw's avatar

I would highly urge you to take Jefferies free laravel from scratch video series. it will only help you and teach you crud ( create read update delete) in laravel.

If you want to continue using $_POST, at least use htmlEntities, and or strip_tags.

shahr's avatar
Level 10

database

I have a table of categories

Please or to participate in this conversation.