PHP scripting Database features in detail (medium) _php tutorial

Source: Internet
Author: User
Using PHP to save files to a database
A database is the center of data organization and storage. It is also possible to process a variety of data, including programs, files, reports, and even audio and video data. As a result of the browser, individual users can only fill in a small portion of their CV. Therefore, here we demonstrate the user's resume upload function. Other types of data can be manipulated by mimicking this example.

The first is the information collection page. Let the user select the file to upload. The HTML code for this page is as follows:

!--begin of Post.htm--〉

〈p〉〈/p〉

〈form method= "POST" action= "insert.php" enctype= "Multipart/form-data"

〈p〉〈b〉 Personal Resume Submission 〈/b〉〈/p〉

〈p〉 Name: 〈br〉

〈input type= "text" name= "name" size= "20" 〉〈/p〉

〈p〉 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= "commit" 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 that inserts records into the database:

〈?

Begin of File insert.php

if ($ResuFile! = "None")

Determine if the user has selected a file

{

$Size = FileSize ($ResuFile);

Determine file size

$mFileData = Addslashes (Fread (fopen ($ResuFile, "R"), $Size));

Read the file and process the content

Unlink ($ResuFile);

Delete Upload temp file

}

$LinkID = @mysql_connect ("localhost", "root", "") or Die ("Cannot connect to the database server!) The database server may not be started, or the user name password is incorrect! ");

$DBID = @mysql_select_db ("Resumedb", $LinkID) or Die ("error selecting database, possibly the database you specified does not exist!") ");

$query = "INSERT into Resume (name,intro,resufile) VALUES (' $Name ', ' $Intro ', ' $mFileData ')";

$result = @mysql_query ("$query", $LinkID); Execute query, insert file to database

if (! $result)

echo "Data insertion failed! ";

Else

echo "File upload succeeded!";

@mysql_close ($LinkID);

End of File insert.php

?〉

With the basics above, it's easy to write a program that reads data from a database. Note that the method that the file sends to the customer. The server must send a header message to the browser stating that the data will be sent as a Word document. If the user's computer is installed with MSWord, the browser will automatically invoke word to display the document.

We can set up 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 may not be started, or the user name password is incorrect! ");

$DBID = @mysql_select_db ("Resumedb", $LinkID) or Die ("error selecting database, possibly the database you specified does not exist!") ");

$query = "INSERT into Resume (name,intro,resufile) VALUES (' $Name ', ' $Intro ', ' $mFileData ')";

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

Execute query, insert file to database

$query = "Select Id,name,intro from Resume";

Generating SQL statements

$result = mysql_query ($query, $LinkID); execution, the result set is saved to the variable $result

$num = mysql_num_rows ($result); Gets the number of record rows returned by the query

if ($num = = 0)

{

echo "did not find any records";

Exit ();

}

while ($row =mysql_fetch_array ($result))//Take 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 document 〈/a〉〈br〉 ";

}

End of File show.php

?〉

To access the file show.php, the user sees a list of personal summary information. Click "View Word document" To see a detailed resume for the corresponding member.

The Word document is displayed with the following file:

〈?

Begin of File download.php

$LinkID = @mysql_connect ("localhost", "root", "") or Die ("Cannot connect to the database server!) The database server may not be started, or the user name password is incorrect! ");

$DBID = @mysql_select_db ("Resumedb", $LinkID) or Die ("error selecting database, possibly the database you specified does not exist!") ");

$query = "Select Resufile from Resume where id= $ID";

$ID the variable passed for the call

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

Execute query to read file contents from database

if (mysql_num_rows ($result) 〈1)

{

echo "did not find the corresponding file! ";

Exit ();

}

$row = Mysql_fetch_array ($result);

$mFileData = $row ["Resufile"];

Read the contents of your CV (data in Word file format)

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

Send header message stating that the data to be sent is a Word document

Echo $mFileData;

Send document data

End of File download.php

?〉

At this point, we have achieved a personal resume submission, database storage, information browsing and other functions, the basic completion of the "Talent Information exchange" framework function.

It is important to note that file upload and database storage through PHP is a prominent technical problem. Many of the Web sites on PHP are constantly having this kind of problem. These operations are more dependent on the platform and environment settings. Different platform configurations can lead to failure of the operation. The operating platform and compilation parameters of the above program are appended to this article for reference.

http://www.bkjia.com/PHPjc/316147.html www.bkjia.com true http://www.bkjia.com/PHPjc/316147.html techarticle using PHP to save files to database database is the center of data organization and storage. It is also possible to process a variety of data, including programs, files, reports, and even audio, 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.