Php generates a complete html static page instance

Source: Internet
Author: User
Tags php database

Addform. Php file

The code is as follows: Copy code


<Form action = "add. php" method = "post">
News title:
<Input type = "text" name = "title"/> <br>
News: <br>
<Textarea name = "content" rows = "10" cols = "50">

</Textarea> <br>

<Input type = "submit" name = "submit" value = "submit"/>

</Form>

Add. Php file

The code is as follows: Copy code

<? Php
Require_once ("mysql_inc.php"); // reference conn. php to connect to the database

$ Title = $ _ POST ['title'];
$ Content = $ _ POST ['content']; // Obtain the form variable


// Create a text document with the following values automatically counted
$ Countfile = "count.txt ";
If (! File_exists ($ countfile ))
{
Fopen ($ countfile, "w"); // If this file does not exist,
}
$ Fp = fopen ($ countfile, "r ");
$ Num = fgets ($ fp, 20 );
$ Num = $ num + 1; // The value is automatically added each time.
Fclose ($ fp );
$ Fp = fopen ($ countfile, "w ");
Fwrite ($ fp, $ num); // update its value
Fclose ($ fp );


// Obtain the HTML path by using the value automatically counted above $ path
$ Houzui = ". html ";
$ Path = $ num. $ houzui;
// The result is automatic growth, such as 1.html, 2.html, 3.html .......... 1 is automatically added when a news item is added.

// Use the following SQL statement to add data to the table news
$ SQL = "insert into news (id, title, content, path) values ('','". $ title. "','". $ content. "','". $ path. "')";
$ Query = mysql_query ($ SQL );

// Replace the data obtained from the form with the {title} and {content} tags in the template.
$ Fp = fopen ("mode.html", "r"); // read-only open template
$ Str = fread ($ fp, filesize ("mode.html"); // read the template content
$ Str = str_replace ("{title}", $ title, $ str );
$ Str = str_replace ("{content}", $ content, $ str); // replace content
Fclose ($ fp );

$ Handle = fopen ($ path, "w"); // The path to open the news in writing mode.
Fwrite ($ handle, $ str); // write the replaced content into the generated HTML file.
Fclose ($ handle );


// Close the work:
Echo "<a href = $ path target = _ blank> view the news just added </a> ";


Mysql_inc.php database connection file

The code is as follows: Copy code

<? Php
Class mysql {


Private $ host; // ��
Private $ name; // & ucirc; ��
Private $ pass; // ��
Private $ database; //
Private $ ut; // �� 뷽 ʽ

 

Function _ construct ($ host, $ name, $ pass, $ database, $ ut ){
$ This-> host = $ host;
$ This-> name = $ name;
$ This-> pass = $ pass;
$ This-> database = $ database;
$ This-> ut = $ ut;
$ This-> connect ();

     }


Function connect (){
$ Link = mysql_connect ($ this-> host, $ this-> name, $ this-> pass) or die ($ this-> error ());
Mysql_select_db ($ this-> database, $ link) or die ("& ucirc; �� ݿ ⣺". $ this-> database );
Mysql_query ("set names '$ this-> ut '");
     }

Function query ($ SQL, $ type = ''){
If (! ($ Query = mysql_query ($ SQL) $ this-> show ('say: ', $ SQL );
Return $ query;
 }

Function show ($ message = '', $ SQL = ''){
If (! $ SQL) echo $ message;
Else echo $ message. '<br>'. $ SQL;
 }

Function affected_rows (){
Return mysql_affected_rows ();
 }

Function result ($ query, $ row ){
Return mysql_result ($ query, $ row );
 }

Function num_rows ($ query ){
Return @ mysql_num_rows ($ query );
 }

Function num_fields ($ query ){
Return mysql_num_fields ($ query );
 }

Function free_result ($ query ){
Return mysql_free_result ($ query );
 }

Function insert_id (){
Return mysql_insert_id ();
 }

Function fetch_row ($ query ){
Return mysql_fetch_row ($ query );
 }

Function version (){
Return mysql_get_server_info ();
 }

Function close (){
Return mysql_close ();
 }

 

 

Function htmtocode ($ content ){
$ Content = str_replace ("n", "<br>", str_replace ("", "& nbsp", $ content ));
Return $ content;
     }
   }

$ Db = new mysql ("localhost", "root", "", "database", "utf8 ");

 

 


?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.