PHP script database function explanation (medium) _ PHP Tutorial

Source: Internet
Author: User
PHP script database features (medium ). Saving files to a database using PHP is the center of data organization and storage. The data to be processed may also be various types of data, including programs, files, reports, and even audio and video data. use PHP to save the files to the database.
Databases are the centers for data organization and storage. Various types of data may be processed, including programs, files, reports, and even audio and video data. Individual users can enter only a small part of their resumes in the browser. Therefore, we will demonstrate the upload feature of your resume. Other types of data can be imitated in this example.

The first is the information collection page. Select the file to upload. The html code on this page is as follows:

<〈! -- Begin of post.htm --> --〉

<P> </p> 〉

<Form method = "POST" action = "insert. php" ENCTYPE = "multipart/form-data"> "〉

<P> <B> submit your resume </B> </p> 〉

<P> Name: <br> 〉

<Input type = "text" name = "Name" size = "20"> </p> 〉

<P> Personal Profile: <br> 〉

<Textarea rows = "2" name = "Intro" cols = "20"> </textarea> </p> 〉

<P> resume File: <br> 〉

<Input type = "file" name = "ResuFile"> </p> 〉

<P> <input type = "submit" value = "submit" name = "B1"> </p> 〉

</Form> 〉

<〈! -End of post.htm --> --〉

Note that the ENCTYPE keyword must not be saved; otherwise, the file cannot be uploaded correctly.

Here, we re-design the code for inserting records into the database:

<〈?

// Begin of file insert. php

If ($ ResuFile! = "None ")

// Confirm that the file is selected

{

$ Size = filesize ($ ResuFile );

// Determine the file size

$ MFileData = addslashes (fread (fopen ($ ResuFile, "r"), $ Size ));

// Read the file and process the content

Unlink ($ ResuFile );

// Delete the uploaded temporary file

}

$ LinkID = @ mysql_connect ("localhost", "root", "") or die ("cannot connect to the database server! The database server is not started, or the user name and password are incorrect! ");

$ DBID = @ mysql_select_db ("ResumeDB", $ LinkID) or die ("An error occurred while selecting the database. it may be because the specified database does not exist! ");

$ Query = "insert into Resume (Name, Intro, ResuFile) values ('$ name',' $ intro', '$ mfiledata ')";

$ Result = @ mysql_query ("$ query", $ LinkID); // execute the query and insert the file to the database.

If (! $ Result)

Echo "data insertion failed! ";

Else

Echo "file uploaded successfully! ";

@ Mysql_close ($ LinkID );

// End of file insert. php

? > 〉

With the above Foundation, it should be easy to write a program that reads data from the database. Note that the file is sent to the customer. The server must send a header to the browser, indicating that the data to be sent is a word document. If MSWord is installed on your computer, the browser automatically calls word to display documents.

You can set a hyperlink to download the Word file:

<〈?

// Begin of file show. php

$ LinkID = @ mysql_connect ("localhost", "root", "") or die ("cannot connect to the database server! The database server is not started, or the user name and password are incorrect! ");

$ DBID = @ mysql_select_db ("ResumeDB", $ LinkID) or die ("An error occurred while selecting the database. it may be because the specified database does not exist! ");

$ Query = "insert into Resume (Name, Intro, ResuFile) values ('$ name',' $ intro', '$ mfiledata ')";

$ Result = @ mysql_query ("$ query", $ LinkID );

// Execute the query and insert the file to the database

$ Query = "select ID, Name, Intro from Resume ";

// Generate an SQL statement

$ Result = mysql_query ($ query, $ LinkID); // run the command. The result set is saved to the variable $ result.

$ Num = mysql_num_rows ($ result); // gets the number of records returned by the query.

If ($ num = 0)

{

Echo "no records found ";

Exit ();

}

While ($ row = mysql_fetch_array ($ result) // fetch the next row of data from the result set to the array $ row

{

Echo $ row ["ID"]. "". $ row ["Name"]. "". $ row ["Intro"]. "";

Echo "<a href =" download. php? ID = ". $ row [" ID "]." "> View word documents </a> <br> 〉";

}

// End of file show. php

? > 〉

Access the file show. php and you will see a list of personal profile information. Click "view Word document" to view the detailed resume of the corresponding member.

The following file is used to display word documents:

<〈?

// Begin of file download. php

$ LinkID = @ mysql_connect ("localhost", "root", "") or die ("cannot connect to the database server! The database server is not started, or the user name and password are incorrect! ");

$ DBID = @ mysql_select_db ("ResumeDB", $ LinkID) or die ("An error occurred while selecting the database. it may be because the specified database does not exist! ");

$ Query = "select ResuFile from Resume where ID = $ ID ";

// $ ID is the variable passed by the call

$ Result = @ mysql_query ("$ query", $ LinkID );

// Execute the query to read the file content from the database

If (mysql_num_rows ($ result) <1)

{

Echo "no corresponding file found! ";

Exit ();

}

$ Row = mysql_fetch_array ($ result );

$ MFileData = $ row ["ResuFile"];

// Read the resume content (Word file format data)

Header ("Content-type: application/msword ");

// Sending header, indicating that the data to be sent is a word document

Echo $ mFileData;

// Send document data

// End of file download. php

? > 〉

So far, we have implemented the resume submission, database storage, information browsing and other functions, and basically completed the "talent information exchange" framework function.

It should be noted that uploading files and storing databases through PHP is a prominent technical problem. Many websites related to PHP are constantly encountering such problems. These operations depend heavily on the platform and environment settings. Different platform configurations may cause operation failures. The running platform and compilation parameters of the above program are provided for your reference.

The tranquility database is the center of data organization and storage. The data to be processed may also be various types of data, including programs, files, reports, and even audio and video data...

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.