Super Simple PHP writes data examples to MySQL

Source: Internet
Author: User
Tags md5 md5 encryption php and php and mysql

PHP writes data to a MySQL database in three steps:

1,php and MySQL establish a connection relationship

2, open MySQL database

3, accept the page data, PHP input to the specified table

1, 22 steps can directly use a database link file can: conn.php

The code is as follows Copy Code

<?php
mysql_connect ("localhost", "root", "");//Connect MySQL
mysql_select_db ("Hello");//Select Database
?>


Of course, the premise is that the Web server, PHP and MySQL are installed, and the MySQL table "Cnbruce" is established.

Three parameters in mysql_connect () are MySQL address, MySQL username and mysql password respectively

Then it is through the Web page to pass data, so that PHP through SQL statements to write data to the MySQL database specified tables, such as new files post.php

The code is as follows Copy Code

<?php
Require_once ("conn.php");//referencing Database link file

$uname = $_get[' n '];//get method is passed to the URL parameter
$PSW = $_get[' P '];
$PSW =md5 ($PSW);//Direct use of MD5 encryption

$sql = "INSERT into the members (Username,password) VALUES (' $uname ', ' $psw ')";
mysql_query ($sql);//Borrow SQL statement to insert data

Mysql_close ()//close MySQL connection
echo "Successful data entry";
?>

Test page: http://localhost/post.php?n=cnbruce&p=i0514

You can insert new data "Cnbruce" to Username field, "i0514" to Password field in the members table of the MySQL database hello

Add: Reading tables

Read the contents of the table, here we use while, can be based on the specific situation, with for or other.

The code is as follows Copy Code

while ($row = Mysql_fetch_array ($result))
{
echo "<div style=" HEIGHT:24PX; line-height:24px; Font-weight:bold; " > "; Typesetting code
echo $row [' Topic ']. "<br/>";
echo "</div>"; Typesetting code
}

Above is the accepted post example

The code is as follows Copy Code

<?php
$con =mysql_connect ("localhost", "root", "GL");
mysql_select_db ("Happysea");

$name =$_post[' name '];
$password =$_post[' password '];
$sex =$_post[' sex '];
$e _mail=$_post[' E_Mail '];
$question =$_post[' question '];
$answer =$_post[' answer '];

$sql = "INSERT into UserInfo (name,password,sex,e_mail,question,answer)
VALUES (' $name ', ' $password ', ' $sex ', ' $e _mail ', ' $question ', ' $answer ') ';

if (! $sql)
{
Echo (' Registration failed! ');
}
Else
{
Echo (' Register successful! ');
}

?>

This is the database.

Mysql> describe UserInfo;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| ID | Int (11) | NO | PRI | NULL | auto_increment |
| name | Text |        YES | |                NULL | |
| password | varchar (20) |        YES | |                NULL | |
| sex | CHAR (4) |        YES | |                NULL | |
| E_Mail | varchar (30) |        YES | |                NULL | |
| Question | Text |        YES | |                NULL | |
| Answer | Text |        YES | |                NULL | |
+----------+-------------+------+-----+---------+----------------+

Related Article

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.