Home > Backend Development > PHP Tutorial > php add and modify in the same form_PHP tutorial

php add and modify in the same form_PHP tutorial

WBOY
Release: 2016-07-13 10:29:17
Original
886 people have browsed it

Everyone may encounter this situation when writing a website, which is to write a form. This form is used to add an article. We receive the data in the background and then store it in the database. Now we have Question. When you want to modify this article, how do you deal with it?

My method is to make an article in the same form. The following example is the code I wrote using the Thinkphp framework. If you don’t understand Thinkphp, it doesn’t matter. I just want to talk about my solution.

If the front desk is like this.

Description, indicating submission to the add method of the article controller to perform the receiving operation.

It’s such a simple form,

We receive data directly in the background.

public function add{

if(IS_POST){

//Here you can actually add a name="submit" attribute to the button. Then use if(isset($_POST['submit']) to determine post submission.

$title = $_POST['title'];

$content = $_POST['content'];

if($title&&$content){

//Insert data.

$flag = model->add(posted data.);

if($flag) $str = "Success";

else $sttr="Failed";

}else{

$str = "Failed, the title or content cannot be empty!";

$this->error($str,U('article/index'));

exit;

}

//Return the operation result.

$this->success("Add".$str,U('article/index')); //Jump to the article list page

exit;

}

$this->display();//If it is not submitted, display our template.

}

The operation succeeds and fails, and jumps back to the add page. Note here that I added an exit structure in some places. Mainly because if it is not added, the following template file will be displayed. Because I did not use that if (IS_POST) Add else.

//Okay, if you want to make changes. We connect through the url, point it to the add method of the article controller, and pass the id., which is the above method.

In the form, we need to modify it like this.


I added a hidden field to the form. If there is a variable for that, add that id

Background processing page.

public function add{

if(IS_POST){

$title = $_POST['title'];

$content = $_POST['content'];

if($title&&$content){

//Insert data.

//Here I first create an array and put the data into

$data = array(

'title' => $title,

'cntent'=> $content

);

if($id = $_POST['id']){

//Indicates that there is an id...

$flag = model modification data;

}else{

$flag = model adds data;

$type = "Add";

}

if($flag) $str = "Success";

else $sttr="Failed";

}else{

$str = "Failed, the title or content cannot be empty!";

$this->error($str,U('article/index'));

exit;

}

//Return the operation result.

$this->success($type.$str,U('article/index')); //Jump to the article list page

exit;

}

//We must also pay attention to the template output here.

if($id = $_GET['id']){

$this->ret = The model queries data through $id and puts it into the template.

}

$this->display();//If it is not submitted, display our template.

}


Okay, the logic is a little confusing... Let’s expand a bit, what if there are attachments? How to deal with it. In fact, the principle is similar. But you have to judge whether there are attachments submitted. Use if($_FILES[' file']['name']), because if you only write if($_FILES['file']), you cannot judge that there is indeed an attachment submitted. Because if there is no attachment, the array is not empty, but Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ). This is of course true in the if judgment statement.

So we have to add the name to judge...

Okay, I’ve said a lot of rubbish, thank you for reading it.

May it help to you .

Best Wishes.



www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/777990.htmlTechArticleEveryone may encounter this situation when writing a website, which is to write a form. This form is used to add For an article, we receive the data in the background and then store it in the database...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template