PHP connects to MySQL database and adds record to database _ PHP Tutorial

Source: Internet
Author: User
PHP connects to the MySQL database and adds records to the database. First, you need to use PHP to connect to the MySQL database: # below is the simplest PHP code to connect to the MySQL database: the code is as follows: Copy the code? Php $ linkmysql_connect (localho first needs to connect to the MySQL database through PHP:

# Connecting to a database

The following is the simplest PHP code for connecting to the MySQL database:

The code is as follows:


$ Link = mysql_connect ("localhost", "root", "password ");
If (! $ Link) echo "connect error ";
Else echo "connect OK ";
?>

Mysql_connect () connection function. localhost indicates the address of the database server. root indicates the MySql database username and password indicates the password of the MySql database. You can change it to your own.

To facilitate future use, standardize the connection code:

The code is as follows:
$ 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, so that you can read and assign values to the form later.

# Create database code

The code is as follows:
Include ("conn. php ");

$ Link_db = 'link _ system ';
// Set the name of the database to be created. it must not be the same as the name of an existing database.

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! ";
}
?>

After the link_system database is created, you also need to create a table.

# Create a database table
// Set the table to be created as link_table. The following table names need to be created to store different data and can be set as needed.
Id of the link_id data
Link_name user link name
Link_url URL
Link_detail introduction
Link_contact contact
Whether link_show is displayed
Link_order
Link_sort category


// Because our linked list has a category, we need to create a category table link_sorts. my idea is to display the location of the member chain, such as the homepage, channel page, and internal page.
Sort_id data id
Sort_name category name

The complete PHP code for creating a table is as follows:

The code is as follows:
// Select the database to operate
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 (20) not null,
Link_url varchar (50) not null,
Link_detail varchar (100) not null,
Link_contact varchar (100) 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 (20) not null
)";

// Execute the table creation 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 the database connection after execution
Mysql_close ($ link );
?>

First, create a table to enter the data to be written to the MySQL database:

# Writing data to a database

The code is as follows:

// Insert. php


The others are input in the text box, and whether to display the use of check box to achieve, selected by default.

Execute the written program page

The code is as follows:

// Insert_ OK .php


Include ("conn. php ");

// Read the data in 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 whether the check box is selected. if not selected, the value is 0.

Mysql_select_db ("link_system", $ link); // select the 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 "failed to add data:". mysql_error ();
}
Else
{
Echo "data added! ";
Echo $ _ POST [site_name]."
". $ _ POST [site_url]."
". $ _ POST [site_contact]."
". $ _ POST [site_detail]."
". $ _ POST [site_order]."
". $ _ POST [site_sort]."
". $ _ POST [site_show];
}
}
?>

If the operation is successful, the user-linked data is added. for classification, the user-linked data is not added first, and then the user-linked data is added later. The next step is to display, edit, and delete data.

Login # below is the simplest PHP code for connecting to the MySQL database: the code is as follows? Php $ link = mysql_connect (localho...

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.