Step by step to teach you to build a website with Php+mysql No.4 article editing, image upload

Source: Internet
Author: User
Tags parent directory

This blog we will focus on the future of the article edit page story.php, because this page to tell the truth code is quite a lot of, also involves the upload of pictures.

Intuitive experience from the page:


Add new and edit are open story.php pages, so we should be able to think ahead of time, this page will first detect what kind of request.

first, let's take care of the simpler logout.php page.


This page is actually very simple, mainly a few functions

The unset function is actually to set some specific variables to null;

The Session_destroy function destroys the current session and, of course, the data in the current session is destroyed as well;

The next step is to execute the header function, which is writer.php, and of course it executes into this block of code:


menu and public site are very simple, one is to return to the Admin Folder index page, and the other is to return to the parent directory, the default is index.php, is the main page of our entire site.


Next we'll see the plays.

story.php

Because this part of the code is relatively long, we will first put out the code, the overall explanation, and then on the individual details of the in-depth explanation.

<?php# Script User to Create or Edit a storyinclude_once (' include_fns.php '), if (Isset ($_request[' story ')) {$story = ge T_story_record ($_request[' story ');}? ><form action = "story_submit.php" method = "POST" enctype= "multipart/form-data" ><input type = "hidden" name= "Story" value = "<?php echoes $_request[' story ');? > "><input type =" hidden "name =" Destination "value =" <?php echo $_server[' Http_referer '];?> "><tab Le><tr><td>headline</td></tr><tr><td><input size= "Name=" Headline " Value = "<?php echo $story [' headline '];? > "></td></tr><tr><td>page</td></tr><tr><td><?phpif (  Isset ($_request[' story ')) {# code ... $query = "Select P.code, p.description from pages p, writer_permissions WP, stories s where P.code = wp.page and Wp.writer = s.writer and S.id = ". $_request[' Story ');} else{$query = "Select P.code, p.description from pages p, writer_permissions WP where P.code = wp.page and Wp.writer = ' {$_session[' auth_user ']} ';} echo query_select (' page ', $query, $story [' page ']);? ></td></tr><tr><td>story text (can contain HTML tags) </td></tr><tr> <td><textarea cols = "rows=" 7 "name=" Story_text "wrap=" virtual "><?php echo $story [' story_text '];? ></textarea></td></tr><tr><td>or Upload HTML file</td></tr><tr ><td><input type = "File" name = "html" size= "></td></tr><tr><td>picture" </td></tr><tr><td><input type= "file" name= "Picture" size= "></td></tr" ><?phpif ($story [picture]) {$size = GetImageSize ('.. /'. $story [' picture ']; $width = $size [0]; $height = $size [1];? ><tr><td> "width=" <?php echo $width;? > "height=" <?php echo $height;? > "&GT;&LT;/TD&GT;&LT;/TR&GT;&LT;?PHP}?&GT;&LT;TR&GT;&LT;TD algin= "center" ><input type= "Submit" value= "Submit" ></td></tr></table > </form>
because the code is long, so it's not the whole thing, and then we'll go through the code:


Line 5th

As we mentioned before, whether you click Add New or edit, the story.php page is displayed, so here is whether the add new or edit is determined by the variable of the story in the request. Of course, it is easy to think that if there is no argument story, that is add new, if there is a story parameter, it must be an edit.

This can also be seen from the writer.php code.

Line 6th

We use the get_story_record function to get the details of the current story, including ID, author, subject content, creation time, modification time, release time, picture content, etc...

Then the entire code is constructed with a form content form


See the form submit the request page is story_submit.php, the request is post, about enctype, we look at StackOverflow above the answer of God:


Because this page of ours may pass the file up, so enctype if multipart/form-data.

Next, the page entered two parameters for the hidden attribute, one is story, and the other is destination. Story does not have to explain, if is new, the story is empty, and destination is to the story_submit.php page can directly return to the previous page of the current page (in fact, this is not the previous page, but the Address of the page (if any) which referred the user agent to the current page. This was set by the user agent.), that is, writer.php page, in fact, here _server[' Http_referer '] inside is actually writer.php page.

For more information on _server, see:

http://php.net/manual/en/reserved.variables.server.php


16-23 rows

is the headline form line, this part is relatively simple


25-48 rows

is a drop-down selection for the page category

Here is still based on the argument whether there is no story to judge, if there is, according to the ID number of the current story to find which category, and display it, if not, then see the current user has the right to publish those kinds of articles, such as the current user I logged in, only two permissions:


Of course, this permission is stored in the writer_permissions table ,

Then after the SELECT statement is written, we will display the drop-down box via the query_select function.


50-60 rows

This section is used to show the main body of the story, similar to headline.


62-80 rows

is used to upload the part of the file, the first part is to upload HTML file, the 2nd part is to upload pictures


81-96 rows

This part of the PHP code is used to display images that have been stored on the server, if it is possible to get the contents of the picture field from the story table


Well, the entire code goes through, let's look at a few specific functions:


Get_story_record function

This function exists in db_fns.php, you know it is stored in the root directory, right?



query_select function

It's still in the db_fns.php .


This part is combined into the final HTML format to be displayed, and we can look at the following source code:

<select name = ' page ' ><option value= "" selected>--Choose--</option><option value= ' News ' >[  NEWS] The Top news Stories from Around the world</option><option value= ' Sport ' >[sport] Sports latest-all the Winners and Losers</option></select>

Well, that's the end of the section, and our next chapter looks at how our new input is uploaded to the server.

Step by step to teach you to build a website with Php+mysql No.4 article editing, image upload

Related Article

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.