I don’t know how everyone achieves it. Let me share my method below.
This is how I implemented it on the page.
I am doing the adding operation in the background.
First we start to receive data, such as title and content. Determine whether the content is empty, and return directly if it is empty.
If there is, we need to determine whether there is an attachment. If there is an attachment, we first move it to the uploads folder of the project through the program,
If it fails, we will return directly.
Okay, let’s start the program.
if(IS_POST){
$title = $_POST['title'];
$content = $_POST['content'];
if($title&&$content){
if($_FILES['file']['name'])
//Start processing the submitted attachment.
If successful, you can get the name of our attachment, upload address and other information to see what you need
If it fails
Return error.
}
$arr = array(
'title' => $title,
'content' => $content;
'filename' => ///
'filepath' => ///
);
$flag = Perform insertion operation..
if($flag) $str = "Success";
else $str = "Failure";
$this->success("Add".$str,//Jump to the list route.);
}
}//end of post.
//The following is the template content....
Now if we want to modify the content.
I thought of a way...
On the page, if there is an attachment, we display the name of the image. If it needs to be modified, we display the file submission box.
I will only write the key parts...
Done... It will look more beautiful at the front desk...
The backend logic is a bit complicated..
First you have to determine whether there is an attachment. If not, you cannot write a null value into the database. If there is an attachment in the database, but the user has not modified the attachment, if you write the contents uniformly into the database, it will be overwritten. .
Let me tell you my thoughts.
First, determine whether title and content are empty.
If it is not empty, determine whether there is an attachment. If there is an attachment, we will perform a unified receiving operation.
if(IS_POST){
$title = $_POST['title'];
$content = $_POST['content'];
if($title&&$content){
//Here we first define an array..
$data = array();
if($_FILES['file']['name'])
//Start processing the submitted attachment.
If successful, you can get the name of our attachment, upload address and other information to see what you need
If it fails
Return error.
Success
$data['filename'] = //File name
$data['filepath'] = //File path.
}
$arr = array(
'title' => $title,
'content' => $content
);
if(!$_POST['id']){
$flag = Perform insertion operation..
$type = "Add";
}else{
$flag = Perform modification operation.
$type = "Modify";
}
if($flag) $str = "Success";
else $str = "Failure";
$this->success($type.$str,//Jump to the list route.);
}
}//end of post.
//The following is the template content....