im trying to create CRUD having parent and child page.
In index page when form it is submitted its send to update page with all data that user have entered. if page is have parent id then it will carry parent id with variable $parent_id else parent id will be blank :
header(“Location:./update.php?update=$lastenterid&parent_id=$parent_id”);
where $lasterid = current page created id and $parent id = parent page id
Now here is entire code for update page.
Displaying data enter in index page with help of lastenter id and parent id in update page
//header("Location:./update.php?update=$lastenterid&parent_id=$parent_id");
if(isset($_GET['update'])){
$update_id = intval($_GET['update']);
$parent_id = $_GET['parent_id'];
if($parent_id == "" || empty($parent_id )){//if parent_id is empty is will display parent data
$data = $conn->query("SELECT * FROM parent_page WHERE id = $update_id ");
while($rows = $data->fetch(PDO::FETCH_OBJ) ):
$id = $rows->id;
$title = $rows->page_title;
$content = $rows->page_content;
endwhile;
}else{//but if it has updat=$lastenterid as well as parent_id=$parent_id it will display children data with listing parent page title in select->option
$data = $conn->query("SELECT * FROM children_page WHERE parent_id = $parent_id ");
while($rows = $data->fetch(PDO::FETCH_OBJ) ):
$id = $rows->child_id;
$title = $rows->child_title;
$content = $rows->child_content;
endwhile;
}
From above code display data in form of update page.
<form action="" method="post">
<label for="">page id</label>
<input type="hidden" name="id" value="<?php echo $id;?>">
<label for="">Page Title</label>
<input type="text" name="title" class="form-control" value="<?php echo $title?>">
<label for="">page content</label>
<textarea class="form-control" name="content" id="" cols="30" rows="10"><?php echo $content;?></textarea>
<input type="submit" value="Update" name="update" class="btn btn-primary">
<label for="">Parent Page id</label>
<select name="parent_id" id="">
<option value="">no parent</option>
<?php
$data = $conn->query("SELECT * FROM parent_page ORDER BY id desc");
while($rows = $data->fetch(PDO::FETCH_OBJ) ):
?>
//now here we have display parent id from database but if parent id generated from database is equal to parent id which we have got from parent_id=$parent_id
header("Location:./update.php?update=$lastenterid&parent_id=$parent_id");
then it will select the value by default that user have entered in index page form
<option value="<?php echo $rows->id; ?>" <?php if($rows->id == $parent_id) {?> echo selected<?php } ?>>
<?php echo $rows->page_title;?>
</option>
<?php endwhile;?>
</form>
Now after getting value in form for update page we are updating data in database with code:
if(isset($_POST['update'])){
$id = $_POST['id'];
$title = $_POST['title'];
$content = $_POST['content'];
$parent_id = $_POST['parent_id'];//id which we get from select->option in form below
if($parent_id == "" || empty($parent_id )){//parent id empty it will update parent table
$update = $conn->prepare("UPDATE parent_page SET page_title = :title, page_content = :content WHERE id = :id ");
$update->execute([
":id"=>$id,
":title"=>$title,
":content"=>$content
]);
}
}else{//but if parent id is selected in form then in will it update in children table which we got from header("Location:./update.php?update=$lastenterid&parent_id=$parent_id"); index page
$update_child = $conn->prepare("UPDATE children_page SET parent_id = :parent_id, child_title = :title, child_content = :content WHERE child_id = :id ");
$update_child->execute([
":id"=>$id,
":parent_id"=>$parent_id,
":title"=>$title,
":content"=>$content
]);
}
Every data in fill is updated successfully except in selection->option:
<select name="parent_id" id="">
<option value="">no parent</option>
<?php
$data = $conn->query("SELECT * FROM parent_page ORDER BY id desc");
while($rows = $data->fetch(PDO::FETCH_OBJ) ):
?>
//now here we have display parent id from database but if parent id generated from database is equal to parent id which we have got from parent_id=$parent_id
header("Location:./update.php?update=$lastenterid&parent_id=$parent_id");
then it will select the value by default that user have entered in index page form
<option value="<?php echo $rows->id; ?>" <?php if($rows->id == $parent_id) {?> echo selected<?php } ?>>
<?php echo $rows->page_title;?>
</option>
<?php endwhile;?>
</select>
when i select new parent title and update, it’s not updating, it’s showing same old parent title data.
parent page = demo1 is not getting update, all others data are updated.
may due code below as we have mention to get selected from the parent_id that we got from $_GET[’ ']
<option value="<?php echo $rows->id; ?>" <?php if($rows->id == $parent_id) {?> echo selected<?php } ?>>
<?php echo $rows->page_title;?>
</option>
we can see here:
<?php if($rows->id == $parent_id) {?> echo selected<?php } ?>
i have define the selected option for parent title with id, i want to update this value when user select new value from dropdwon.