Php adds and modifies data in the same form

Source: Internet
Author: User

I don't know how it is implemented. Let's share my methods.

On the page, I implemented it in this way.

I am adding data in the background.

First, we start to receive data. For example, the content of title and content. Determine whether the content is empty. if the content is empty, it is returned directly.

If there is an attachment, we need to determine whether there is an attachment. If there is an attachment, we first move it to the project's uploads folder through the program,

If it fails, we will return it directly.

Okay, go to the program.

If (IS_POST ){

$ Title = $ _ POST ['title'];

$ Content = $ _ POST ['content'];

If ($ title & $ content ){

If ($ _ FILES ['file'] ['name'])

// Start processing the submitted attachments.

If the attachment is successfully uploaded, you can obtain the attachment name, the uploaded address, and other information.

If it fails

An error is returned.

}

$ Arr = array (

'Title' => $ title,

'Content' => $ content;

'Filename' => ///

'Filepath' => ///

);

$ Flag = insert operation ..

If ($ flag) $ str = "successful ";

Else $ str = "failed ";

$ This-> success ("add". $ str, // jump to list route .);

}

} // End of post.

// The following is the template content ....

If we want to modify the content.

I thought of a method ..

On the page, if there is an attachment, We will display the image name. If you need to modify it, we will display the file submission box ..

I only wrote the key part ..

I got it done... it looks pretty on the front desk...

The background logic is a bit complicated ..

First, you must determine whether there are attachments. If there is no attachment, you cannot write null values into the database. If there is already one in the database, the user has not modified the attachment, if you write content to the database in a unified manner. will overwrite ..

Let me talk about my ideas.

First, judge whether the title and content are empty.

If it is not null, check 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 attachments.

If the attachment is successfully uploaded, you can obtain the attachment name, the uploaded address, and other information.

If it fails

An error is returned.

Successful

$ Data ['filename'] = // file name

$ Data ['filepath'] = // file path.

}

$ Arr = array (

'Title' => $ title,

'Content' => $ content

);

If (! $ _ POST ['id']) {

$ Flag = insert operation ..

$ Type = "add ";

} Else {

$ Flag = perform the modification operation.

$ Type = "modify ";

}

If ($ flag) $ str = "successful ";

Else $ str = "failed ";

$ This-> success ($ type. $ str, // jump to the list route .);

}

} // End of post.

// The following is the template content ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.