im trying to create page as wordpress where when user click on add new page, blank form in show with publish button and when user fill data and click on publish button, form in page remains with fill data create by user but button publish button is change to update button same as in wordpress now when i change form field data and click on update button, page get refresh and blank form is displayed but form data are updated in database successfully and update are not show in form instead, blank form is shown…. how can i keep data in form after clicking update button…
im trying acheieve as wordpress create page:
wordpress pages style
code for inserting value in databas and defining varibale $last_id for
storing last ID generate for updating value
global $last_id;
if(isset($_POST['create_page'])){
$post_title = $_POST['title'];
$post_parent = $_POST['parent_page'];
$post_author = $_POST['author'];
$post_status = $_POST['status'];
$post_date = date('d-m-y');
$post_image = $_FILES['page_image']['name'];
$post_image_tmp = $_FILES['page_image']['tmp_name'];
$post_tags = $_POST['page_tags'];
$post_content = $_POST['page_content'];
move_uploaded_file($post_image_tmp, "../images/$post_image" );
$post_parent = $_POST['parent_page'];
$query = "INSERT INTO pages(`parent_id`, `page_title`, `author`, `post_date`, `page_image`, `page_content`, `page_tags`, `post_status`) VALUES ('{$post_parent}','{$post_title}','{$post_author}','now()','{$post_image}','{$post_content}','{$post_tags}','{$post_status}')";
//$create_page = mysqli_query($connection,$query);
//storing last id generated in variale $last_id
if (mysqli_query($connection,$query)) {
$last_id = mysqli_insert_id($connection);
}
}
Now we got last enter id with $last_id, with the help $last id check for empty ID, if ID is empty then generate Empty form else get from with ID reference value in Form
so,if Id is empty generating empty Form
if(empty($last_id)){
?>
<div class="form-group">
<label for="">page title</label>
<input type="text" class="form-control" name="title" >
</div>
<div class="form-group">
<label for="">parent page</label>
<select name="parent_page" id="">
<option value="">no parent</option>
<?php
$query = "SELECT * FROM parent_page";
$parent_page = mysqli_query($connection,$query);
while($rows = mysqli_fetch_assoc($parent_page) ){
$page_id = $rows['page_id'];
$page_title = $rows['page_title'];
?>
<option value="<?php echo $page_id; ?>"><?php echo $page_title; ?></option>
<?php }?>
</select>
</div>
<div class="form-group">
<label for="">author</label>
<input type="text" class="form-control" name="author" >
</div>
<div class="form-group">
<label for="">page status</label>
<input type="text" class="form-control" name="status" >
</div>
<div class="form-group">
<label for="">page image</label>
<input type="file" class="form-control" name="page_image">
</div>
<div class="form-group">
<label for="">page tags</label>
<input type="text" class="form-control" name="page_tags" >
</div>
<div class="form-group">
<label for="">page content</label>
<textarea name="page_content" class="form-control" cols="30" rows="10"> </textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" name="create_page" value="publish">
</div>
But If $last_id is not empty then get value in form with id reference
<?php
}else{
//echo $id2 = $last_id;
$data['page_title'] = $data['page_title'] = $data['post_status'] = $data['page_tags'] = $data['page_content'] = '';
$sql = "select * from pages where child_id='".$id2."'";
$result = mysqli_query($connection,$sql);
if(!$result){
die('QUERY Failed' . mysqli_error($connection));
}
if ($result->num_rows > 0){
while($data=mysqli_fetch_assoc($result)) {
?>
<form action="" method="post" enctype="multipart/form-data">
<?php echo "<h1> im main id $last_id</h1>"; ?>
<div class="form-group">
<label for="">page id 12</label>
<input type="text" class="form-control" name="id" value="<?php echo $last_id;?>">
</div>
<div class="form-group">
<label for="">page title 12</label>
<input type="text" class="form-control" name="title" value="<?php echo $data['page_title'];?>">
</div>
<div class="form-group">
<label for="">parent page 12</label>
<select name="parent_page" id="">
<option value="">no parent</option>
<?php
$query = "SELECT * FROM parent_page";
$parent_page = mysqli_query($connection,$query);
while($rows = mysqli_fetch_assoc($parent_page) ){
$page_id = $rows['page_id'];
$page_title = $rows['page_title'];
?>
<option value="<?php echo $page_id; ?>"><?php echo $page_title; ?></option>
<?php }?>
</select>
</div>
<div class="form-group">
<label for="">author 12</label>
<input type="text" class="form-control" name="author" value="<?php echo $data['author'];?>">
</div>
<div class="form-group">
<label for="">page status 12</label>
<input type="text" class="form-control" name="status" value="<?php echo $data['post_status'];?>">
</div>
<div class="form-group">
<label for="">page tags 12</label>
<input type="text" class="form-control" name="page_tags" value="<?php echo $data['page_tags'];?>">
</div>
<div class="form-group">
<label for="">page content 12</label>
<textarea name="page_content" class="form-control" cols="30" rows="10"> <?php echo $data['page_content'];?></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" name="update" value="update">
</div>
</form>
<?php
} } }
Now we have got form with field value right after publishing pages and changing Publish button to Update button which will help us to update our query
Now again with help of $last_id will we be updating our form data:
if(isset($_POST['update'])){
$post_id = $_POST['id'];
$post_title = $_POST['title'];
$post_parent = $_POST['parent_page'];
$post_author = $_POST['author'];
$post_status = $_POST['status'];
$post_date = date('d-m-y');
$post_tags = $_POST['page_tags'];
$post_content = $_POST['page_content'];
$post_parent = $_POST['parent_page'];
$query = "UPDATE pages SET page_title = '{$post_title}', author = '{$post_author}', page_content = '{$post_content}', page_tags = '{$post_tags}', post_status = '{$post_status}' WHERE child_id = '{$post_id}' ";
$create_page = mysqli_query($connection,$query);
if(!$create_page){
die('QUERY Failed' . mysqli_error($connection));
}
}
now when click on update button update query is run successfully but after page get refresh i get blank form instead of update data in form but data is succesully update in database but im unable to display update data back in form like in wordpress.
My Goal is to creat new page same as wordpress and displaying data on same form as well as updating data on same form and displaying back updated data back in same form without page refresh