Step by step, we will teach you how to use PHP + MySql to build website No. 4 Article editing and image uploading,

Source: Internet
Author: User

Step by step, we will teach you how to use PHP + MySql to build website No. 4 Article editing and image uploading,

This blog will focus on the article editing page in the future.Story. phpBecause this page has a lot of code to tell the truth and involves uploading images.

Intuitive experience from the page:


Add newAndEditAll are open story. php pages, so we should be able to think ahead of time that this page will first detect which request is used.

First, let's get a simple solution. Logout. phpPage


This page is actually very simple. It mainly involves several functions.

UnsetThe function is actually to leave some specific variables empty;

Session_destroyThe function destroys the current session. Of course, the data in the current session is also destroyed;

Then executeHeaderThe function, that is, writer. php, will be executed in this code block:


Menu and Public SiteThey are all very simple. One is to return to the index page under the admin folder, and the other is to return to the parent directory. The default is index. php, which is the homepage of our entire website.


Next, let's take a look at the highlights,

Story. php

Because this part of the code is relatively long, we should first paste the code, first explain it as a whole, and then explain the details in depth.

<?php# Script User to Create or Edit a Storyinclude_once('include_fns.php');if (isset($_REQUEST['story'])) {$story = get_story_record($_REQUEST['story']);}?><form action = "story_submit.php" method = "POST" enctype="multipart/form-data"><input type = "hidden" name="story" value = "<?php echo $_REQUEST['story'];?>"><input type = "hidden" name = "destination"value = "<?php echo $_SERVER['HTTP_REFERER']; ?>"><table><tr><td>Headline</td></tr><tr><td><input size="80" 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 = "80" 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="40"></td></tr><tr><td>Picture</td></tr><tr><td><input type="file" name= "picture" size="40"></td></tr><?phpif ($story[picture]) {$size = getimagesize('../'.$story['picture']);$width = $size[0];$height = $size[1];?><tr><td></td></tr><?php}?><tr><td algin="center"><input type="submit" value="Submit"></td></tr></table> </form>
Because the code is long, it will not be complete. Then we will go through the code again:


5th rows

As we have mentioned before, no matter whether you click add new or edit, story is displayed. php page, so here is to decide whether to add new or edit based on whether the story variable in the request exists. Of course, we can easily think that if there is no story parameter, It is add new. If there is a story parameter, it must be edit.

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

6th rows

We useGet_story_recordFunction to obtain detailed information about the current story, including id, author, subject content, creation time, modification time, release time, and image content...

The following code constructs a form content form.


The request page for the form submit isStory_submit.phpThe request method is POST.EnctypeLet's take a look at the answers provided by stackoverflow:


Because we may upload files to this page, enctype isMultipart/form-data.

Next, we enter two parameters with the property "hidden", story and destination. Story does not need to be explained. If it is created, the story is empty. destination aims to directly return to the previous page of the current page on the story_submit.php page, this is not The previous page, but the address of the page (if any) which referred the user agent to The current page. this is set by the user agent .), that is, writer. the php page is actually a writer in _ SERVER ['HTTP _ referer. php page.

For more information about _ SERVER, see:

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


Lines 16-23

Is the form line of headline, which is relatively simple


25-48 rows

Is a drop-down selection item of the page category.

It is still determined based on the existence of story in the parameter. If yes, the ID of the current story is used to locate the type of story and display it. If no, the current user has the permission to post those articles. For example, the current user I log on to has only two permissions:


Of course, this permission is stored inWriter_permissions table,

After the select statement is written, we useQuery_selectFunction to display the drop-down list


50-60 rows

This part is used to display the main part of the story, similar to headline.


62-80 rows

Is used to upload files. The first part is to upload html files, and the second part is to upload images.


Lines 1-96

This part of php code is used to display images that have been stored on the server. Of course, the premise is that you can obtain the picture field content from the story table.


Now, the entire code is finished. Let's look at the specific functions used:


Get_story_record Function

This function exists inDb_fns.phpYou know it is stored in the root directory, right?



Query_select Function

Still inDb_fns.phpMedium


This part combines the HTML content to be displayed. You can view the 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, this part of content ends here. Let's take a look at how the newly entered content is uploaded to the server in the next chapter.

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.