PHP adds and modifies data in the same form two

Source: Internet
Author: User
Tags array empty end insert modify

Well, I don't really care about the amount of traffic, but I have more visits today than I did last year.

I'm still a little surprised. As a technology of the cock silk. No, I don't think I'm a dick, I think programming is a tall mountain career. Although very tired, but really can make your daily life very fulfilling.

No matter whether you believe it or not,anyhow I believe it.

Today, in the last article, we are simply uploading and modifying the article.

If the attachment is involved. This is really a headache if you need to change the attachment at the time of the change.

I don't know how you do it. Let me share my method.

That's how I do it on the page.

I do the add in the background.

First we begin to receive data. such as title,content content. Determines whether the content is empty, and returns null directly.

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

If we fail, we will go straight back.

Okay, on the program.

if (is_post) {

$title = $_post[' title '];

$content = $_post[' content '];

if ($title && $content) {

if ($_files[' file '] [' name '])

Begin processing of the submitted attachments.

If successful, you can get the name of our attachment and upload the address and other information to see what you need

If it fails,

Returns an error.

}

$arr = Array (

' title ' => $title,

' Content ' => $content;

' filename ' =>///

' FilePath ' =>///

);

$flag = insert Operation ...

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

else $str = "Failed";

$this->success ("Add". $str,//skip to List routing.);

}

}//end of Post.

Here is the template content ....



Now, if we're going to modify that content.

I thought of a way.

In the page, if there are attachments, we display the name of the picture. If you need to modify it, we'll show you the file's submission box ...

I only wrote the key parts.

<script type= "Text/javascript" >

Here we use jquery to implement it.

function Show_file (o) {

$this = $ (o);

$this. Hide ();

$this. Next (). Show ();

}

</script>

It's done. So in the foreground is still more beautiful bar ...

The logic behind the scenes is a bit complicated.

First of all, you have to determine whether there are attachments, if not, you can not write to the database null value, if the database has, now the user did not modify the attachment, if the unified to the database to write content.

I'll talk about my ideas.

The first is to determine whether title,content is empty.

If it is not empty, determine if there are attachments, if there are attachments, we have a unified receive 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 '])

Begin processing of the submitted attachments.

If successful, you can get the name of our attachment and upload the address and other information to see what you need

If it fails,

Returns an error.

Success

$data [' filename '] =//filename

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

}

$arr = Array (

' title ' => $title,

' Content ' => $content

);

if (!$_post[' id ']) {

$flag = insert Operation ...

$type = "Add";

}else{

$flag = Modify the operation.

$type = "Modify";

}

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

else $str = "Failed";

$this->success ($type. $str,//skip to List routing.);

}

}//end of Post.

Here 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.