PHP script database Function Details 2

Source: Internet
Author: User
(Author: Wang Kaibo) using PHP to save files to the database is the center of 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 SyntaxHighlighter. all ();

(Author: Wang Kaibo) using PHP to save files to the database is the center of 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 introduction: <br> <textarea rows = "2" name = "Intro" cols = "20"> </textarea> </p> <p> resume file: <br> <input type = "file" name = "ResuFile"> </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") // Determine the selected file {$ 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 that 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 is 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. We can set a hyperlink to download this 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 that 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 statement. Save the result set to the variable $ result $ num = mysql_num_rows ($ result ); // Obtain the number of records returned by the query if ($ num = 0) {echo "no records found"; exit () ;}while ($ row = mysql_fetch_array ($ result )) // Obtain the number of the next row in the result set Data to the array $ row {echo $ row ["ID"]. "". $ row ["Name"]. "". $ row ["Intro"]. ""; echo "<a href =" download. php? ID = ". $ row [" ID "]." "> View the word document </a> <br>";} // end of file show. php?> Access the file show. php, which displays a list of personal 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 that the specified database does not exist! "); $ Query =" select ResuFile from Resume where ID = $ ID "; // $ ID indicates the variable passed by the call $ result = @ mysql_query (" $ query ", $ LinkID); // execute the query and 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"); // send header information, description: the data to be sent is the word file echo $ mFileData; // send the document data // end of file download. php?> At this point, 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.

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.