Change option value which have already been selected

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.

i just found out that is getting update in database but as we have assign parent id = $_GET[‘parent_id’]

<?php if($rows->id == $parent_id) {?> echo selected<?php }  ?>

it’s showing old parent id now how can i change/update to new enter parent id in select->option after form have been submit with new update data…

Not sure if this is the cause of the incorrect operation or not, but the post method form processing code is not indented properly and has an extra }, so that the else logic (for the case of editing a child record), is actually for the if(isset($_POST[‘update’])) condition, not the if($parent_id == “” || empty($parent_id )) condition.

You also have some apparent php comments outside of php mode, i.e. in the html document, and the syntax for the selected attribute has an error, where the echo statement is actually in the html document.

Lastly, if editing the parent of a child page, i.e. moving a child from one parent to another, is not possible, why are you using this select/option menu at all? You should just pass the parent_id value, regardless of if it is empty, e.g. a parent page, or has a value, e.g. a child page, through a hidden form field.

Edit; I see, after formatting the sql statement, that this is allowing the parent to be changed for a child page. The cause of the incorrect operation is as I stated, the logic is wrong and cannot be seen due to the bad indentation.

1 Like

while refactoring there may some misplace of closing, code is very long just i have just refactor the code, please ignore closing tags

have solved, just pass your parent id from database and compare with your form parent id, if both id matches then echo ‘selected’, that’s all, was so simple

Yes, I see that now.

However, now that I understand what the code is trying to do, there’s an issue if there can be multiple child pages per parent page. The current code will retrieve the last/only child page data for editing. If your test data only contains one child for any parent id, it will work, but won’t with more than one child page.

You would need to fetch the child data into an array of rows, even if there is only one child page, then loop over those row(s) and output an edit form for each child page. This should be the only change needed to accomplish this, since the hidden id field will contain the correct id for the child record being edited.