PHP Study Notes (2)-Database Operations

Source: Internet
Author: User

I have created two pages. text.html is a single page of a static table. input data in the text.html form. Then dB. php obtains the data in the form and inserts the data into the database for output to display on the page. Next, let's look at the code and write the code in the comments directly.

Text.html

 

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <br/> <HTML xmlns = "http://www.w3.org/1999/xhtml"> <br/> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 "/> <br/> <title> single table page </title> <br/> </pead> </P> <p> <body> </P> <p> <form method = post action = "DB. PHP "> <br/> <p> product ID: <input type = "text" name = "proid" width = "100px"/> </P> <br/> <p> product name: <input type = "text" name = "proname" width = "100px"/> </P> <br/> <p> product description: <input type = "text" name = "prodes" width = "100px"/> </P> <br/> <input type = "Submit" value = "Submit "/> </P> <p> </body> <br/> </ptml>

 

DB. php

 

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <br/> <HTML xmlns = "http://www.w3.org/1999/xhtml"> <br/> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 "/> <br/> <title> database connection </title> <br/> </pead> <br/> <body> <br/> <? Php <br/> error_reporting (0); // to avoid warning notice: Undefined index: proid in ...... <Br/> $ dbserver = "localhost"; // define the database server <br/> $ dbuser = "sa "; // define the database user name <br/> $ dbpass = "123"; // define the database access password </P> <p> $ conn = mysql_connect ($ dbserver, $ dbuser, $ dbpass); </P> <p> // The code below to test whether the database is successfully connected <br/>/* If (! $ Conn) <br/> die ('could not connect :'. mysql_error (); <br/> else echo '<p> connection successful </P>'; */</P> <p> mysql_select_db ("sa ", $ conn); <br/> mysql_query ("set names utf8"); // ensure that the data displayed on the page is encoded in the same way as that of the database, otherwise, the data output and input may be question marks </P> <p> mysql_query ("insert into product (pro_id, pro_name, pro_description) <br/> values ('$ _ post [proid]', '$ _ post [proname]', '$ _ post [prodes]') "); // insert data. The input three parameters are text. parameters entered in the form in PHP. Here, let's look at the extended $ _ post, $ _ get, $ _ requst differences </P> <p> $ SQL = "select * from product"; // database query <br/> $ result = mysql_query ($ SQL ); </P> <p> echo "<Table border = 1> // you can write HTML code in Echo, <br/> <tr> <br/> <TD> NO. </TD> <br/> <TD> name </TD> <br/> <TD> details </TD> <br/> </tr> "; </P> <p> while ($ ROW = mysql_fetch_array ($ result )) {// output data <br/> echo "<tr> <br/> <TD> $ row [pro_id] </TD> <br/> <TD> $ row [pro_name] </TD> <br/> <TD> $ row [pro_description] </TD> <br/> </tr> "; <br/>}< br/> echo "</table>"; </P> <p> mysql_close ($ conn); // close the connection, some people on the Internet say they don't need to close it. php can be closed by myself. I decided to close it first. After all, it's better for beginners to do this, develop good habits <br/> </body> <br/> </ptml>

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.