The School of building a website of Fire Station has compiled some MYSQL skills and practical articles, which will be sent to you one after another. This article mainly introduces the PHP code of the instance of any data stored in mysql and the program extracted from it, as follows:
<? Php // File name: filedb. php stores the file in the database. /* Example of uploading a file to a MySQL database The data table structure is as follows: Create table files ( Id int (4) not null auto_increment, Filename varchar (64 ), Filesize int (16 ), Filetype varchar (64 ), Filecontent longblob, Remark text, Primary key (id) ); */ // Set system parameter variables and modify them as needed $ DB_SERVER = "127.0.0.1"; # database connection words $ DB_USER = "root"; # User Name $ DB_PASS = ""; # Password $ DB_NAME = "article"; # Database Name $ TABLE_NAME = "files"; # data table name $ HANDLER_SCRIPT = "mysql database display file. php"; # script file name for Data Processing $ PHP_SELF = $ _ SERVER ['php _ SELF ']; ?> <Html> <Head> <Title> store files in the database </title>
<Style> Caption {background-color: # E6F4FF; font-size: 14px} Td {background-color: # E6F4FF; font-size: 12px} Th {background-color: # FFCC00; font-size: 12px} Div {font-size: 12px} </Style> </Head> <Body> <Form enctype = 'multipart/form-data' method = 'post'> <Table cellspacing = 1 cellpadding = 5> <Caption> store files to the Database Manager </caption> <Input type = 'ddden 'name = 'max _ FILE_SIZE' value = 10489760> <Tr> <Td> upload a file: </td> <Td> <input type = 'file' name = 'userfile'> </td> </Tr> <Tr valign = top> <Td> file Description: </td> <Td> <textarea name = 'description' rows = '4' cols = '40'> </textarea> </td> </Tr> <Tr> <Td colspan = 2 align = center> <input type = 'submit 'name = 'submit' value = 'upload'> </Tr> </Table> </Form> <? Php If (isset ($ _ POST ["submit"]) // After the form is submitted, run the following code: { If (! Is_uploaded_file ($ _ FILES ['userfile'] ['tmp _ name']) // check whether the file is uploaded successfully. { Die ("File Upload Failed! "); } $ Link = mysql_connect ($ DB_SERVER, $ DB_USER, $ DB_PASS) // connect to the database Or die ("failed to connect to the database! "); Mysql_select_db ($ DB_NAME ); $ Filename = addslashes ($ _ FILES ['userfile'] ['name']); # To properly Save the FILES to the database, $ Filesize = addslashes ($ _ FILES ['userfile'] ['SIZE']); # All inserted data must be $ Filetype = addslashes ($ _ FILES ['userfile'] ['type']); # perform the addslashes operation // Read the uploaded file and perform preprocessing # $ Tmp_name = $ _ FILES ['userfile'] ['tmp _ name']; $ Fd = fopen ($ tmp_name, "rb "); $ Contents = fread ($ fd, filesize ($ tmp_name )); Fclose ($ fd ); $ Filecontent = addslashes ($ contents ); $ Tmp_name = $ _ FILES ['userfile'] ['tmp _ name']; $ Fd = fopen ($ tmp_name, "rb "); $ Contents = fread ($ fd, filesize ($ tmp_name )); Fclose ($ fd ); $ Filecontent = addslashes ($ contents ); $ Remark = addslashes ($ _ POST ['description']);
$ Query_string = "insert into $ TABLE_NAME VALUES ('', '$ filename',' $ filesize', '$ filetype',' $ filecontent', '$ remark ')"; $ Result = mysql_query ($ query_string) or die ("data insertion failed! "); # Insert data Echo "<div id = 'info'> saved successfully! <Br> \ n "; Echo "File name: {$ _ FILES ['userfile'] ['name']} <br> \ n "; Echo "file size: {$ _ FILES ['userfile'] ['SIZE']} bytes <br> \ n "; Echo "file type: {$ _ FILES ['userfile'] ['type']} </div> \ n "; Echo "<input type = \" submit \ "value = Refresh onclick = \" window. location = '$ PHP_SELF' \ "> "; } Echo" <Table border = 2 bordercolor = #055AA0 cellspacing = 0 width = 840 cellpadding = 0 style = \ "border-collapse: collapse; LEFT: 0px; WORD-WRAP: break-word; word-break: break-all \ "> <Tr> <Th width => ID </th> <Th> file name </th> <Th> file size </th> <Th> file type </th> <Th> description </th> <Th> connection </th> <Th> Delete </th> </Tr> \ n "; $ Link = mysql_connect ($ DB_SERVER, $ DB_USER, $ DB_PASS) // display existing database records Or die ("failed to connect to the database! "); Mysql_select_db ($ DB_NAME ); $ Query_string = "SELECT * FROM $ TABLE_NAME order by id"; # retrieve all records without pagination $ Result = mysql_query ($ query_string) or die ("query error! "); While ($ row = mysql_fetch_array ($ result )) { $ Remark = nl2br (htmlspecialchars ($ row ["remark"]); # preprocessing is required to display the description correctly. Echo "<tr> <Td> $ row [id] </td> <Td> $ row [filename] </td> <Td align = right> $ row [filesize] </td> <Td> $ row [filetype] </td> <Td> $ remark </td> <Td nowrap> <a href = '$ HANDLER_SCRIPT? Action = show & id = $ row [id] 'target = _ blank> click to View Details </td> <Td nowrap> <a href = '# 'onclick = 'window. open (\ "$ HANDLER_SCRIPT? Action = del & id = $ row [id] \ "); location. href = \" $ _ SERVER [PHP_SELF] \ "; '> click Delete </td> </Tr> \ n "; } Echo "</table> \ n ";
?> </Body> </Html>
|
- 2 pages in total:
- Previous Page
- 1
- 2
- Next Page