First you need to connect to the MySQL database via PHP:
#连接数据库
The following is the simplest code for PHP to connect to the MySQL database:
The code is as follows |
Copy Code |
$link =mysql_connect ("localhost", "root", "password"); if (! $link) echo "Connect error"; else echo "Connect OK"; ?>
|
where mysql_connect () connection function, localhost represents the address of the database server, root is the MySQL database user name, password is the MySQL database password. Change to your own when you use it.
To make it easier to use later, normalize the connection code:
The code is as follows |
Copy Code |
$link _host= ' localhost '; $link _user= ' root '; $link _pass= ' password ';
$link =mysql_connect ($link _host, $link _user, $link _pass);
if ($link) { echo "Connect ok!"; } Else { echo "Connect fail!"; } ?>
|
Use three variables to read the server address, user name and password, convenient for later form reading and assignment.
#建立数据库代码
The code is as follows |
Copy Code |
Include ("conn.php");
$link _db= ' Link_system '; Set the name of the database to be established, must not be the same as the existing database name
if ($link) { echo "Connect ok! "; if (mysql_query ("CREATE DATABASE". $link _db, $link)) { echo "Database created! "; } Else { echo "Database Create fail!"; } } Else { echo "Connect error!"; } ?>
|
Once the Link_system database has been established, tables need to be established.
#建立数据库表格
To set up the table to be built for link_table, here is the table name that needs to be established, used to store different data, can be set according to your own needs.
ID of the LINK_ID data
Link_name Friend chain Name
Link_url friend chain URL
Link_detail Introduction
Link_contact Contact information
Whether the link_show is displayed
Link_order Sorting order
Link_sort classification
Because we have categories in the list of friends, so we need to set up a classification table link_sorts, my vision is the location of the friend chain display, such as the home page or channel pages, internal pages and so on.
sort_id Data ID
Sort_name category Name
The complete PHP code for creating the table is as follows:
The code is as follows |
Copy Code |
Select the Operational database mysql_select_db ($link _db, $link); Create a table $link _table = "CREATE TABLE link_table ( link_id int unsigned primary key not NULL auto_increment, Link_name varchar () NOT NULL, Link_url varchar () NOT NULL, Link_detail varchar (+) NOT NULL, Link_contact varchar (+) NOT NULL, link_show int unsigned NOT NULL, link_order int unsigned NOT NULL, link_sort int unsigned NOT NULL )"; $sort _table = "CREATE TABLE sort_table ( sort_id int unsigned primary key not NULL auto_increment, Sort_name varchar () NOT NULL )"; Perform a build table operation if (!mysql_query ($link _table, $link)) { echo "Create link_table error:". Mysql_error (). " "; } else { echo "Link_table created!". " "; } if (!mysql_query ($sort _table, $link)) { echo "Create sort_table error:". Mysql_error (). " "; } else { echo "Sort_table created!". " "; } Close database connection after execution Mysql_close ($link); ?> |
First create a table to fill in the data that needs to be written to the MySQL database:
#写入数据库
The code is as follows |
Copy Code |
insert.php
|
The others are entered with a text box and are displayed using a check box, which is selected by default.
Execute the Write Program page
The code is as follows |
Copy Code |
insert_ok.php Include ("conn.php"); Read the data from the form on the previous page $link _name=$_post[site_name]; $link _url=$_post[site_url]; $link _contact=$_post[site_contact]; $link _detail=$_post[site_detail]; $link _order=$_post[site_order]; $link _sort=$_post[site_sort]; $link _show=$_post[site_show]; if (! $link _show== "1") $link _show= "0"; check box is selected and assigned to 0 if not checked mysql_select_db ("Link_system", $link); Select Database Link_system if ($_post) { $sql = "INSERT into link_table (link_name,link_url,link_contact,link_detail,link_order,link_sort,link_show) VALUES (' $link _name ', ' $link _url ', ' $link _contact ', ' $link _detail ', ' $link _order ', ' $link _sort ', ' $link _show '); if (!mysql_query ($sql, $link)) { echo "Add Data failed:". Mysql_error (); } Else { echo "Add data successfully!" "; Echo $_post[site_name]. " ". $_post[site_url]." ". $_post[site_contact]." ". $_post[site_detail]." ". $_post[site_order]." ". $_post[site_sort]." ". $_post[site_show]; } } ?>
|
If the execution succeeds, add the friend chain data to complete, as for the classification is not added temporarily, and then added to the later classification. The next step is the implementation of displaying the data, editing the data, and deleting the data.
http://www.bkjia.com/PHPjc/629011.html www.bkjia.com true http://www.bkjia.com/PHPjc/629011.html techarticle first, you need to connect to MySQL database via PHP: #连接数据库 The following is the simplest PHP connection to MySQL database code: Code as follows copy code? PHP $link =mysql_connect (Localho ...