What is the problem?
Apr 25, 2020
7
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();
}
Please or to participate in this conversation.
