php+mysql-form data into database and data extraction complete process

Source: Internet
Author: User
Tags prepare

When a new user registers, the website will store the user's registration information in the database, and then extract it when needed. A simple example is written today.

The following functions are mainly accomplished:

(1) The user registers, realizes the password duplicates confirms, the verification code proofreading function.

(2) After successful registration, the user is inserted into the database to save.

(3) Extract the data from the database table and print it.

1. Registration Form

In previous blogs, you shared the code for registration and login forms. This time the code is roughly the same, just a slight change. Only as a case study

There is really nothing to say on the form page, except for the format alignment plus a few (spaces).

2. Verification code Page
<strong><span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><?PHPHeader("Content-type:text/html Charset=utf-8"); //Open Session    Session_Start(); //Prepare the canvas    $im=imagecreatetruecolor (50,25); //Preparing pigments    $black=imagecolorallocate ($im, 0,0,0); $gray=imagecolorallocate ($im, 200,200,200); //Background FillImagefill ($im, 0, 0,$gray); //Center Text    $x= (50-10*4)/2; $y= (25-5)/2+5; //Add Interferon     for($i= 0;$i<50;$i++) {Imagesetpixel ($im,Mt_rand(0,50),Mt_rand(0,25),$black); }     //Prepare text    $arr=Array_merge(Range(0,9),Range(' A ', ' Z '),Range(' A ', ' Z ')); Shuffle($arr); $str=implode(Array_slice($arr, 0,4)); //put $str in session to make it easy to call all pages    $_session[' Vstr ']=$str; $file=".. /FONTS/SIMSUN.TTC "; Imagettftext ($im, 15, 0,$x,$y,$black,$file,$str); //output to the browser or save.    Header("Content-type:image/png"); Imagepng ($im); //Close CanvasImagedestory ($im);?></span></strong>

The Verification Code feature has been explained in detail in a previous blog post. This time the code is basically directly used, the only upgrade is the addition of interferon, so that the verification code does not have a dry four characters where. The Imagesetpixel () function is used to create some interference points. Please refer to the PHP manual for specific instructions.

3. Submission page (Data extraction page)
<?PHPHeader("Content-type:text/html;charset=utf-8"); //Open Session    Session_Start(); //Convert the captcha and the string in the input box to lowercase    $code=Strtolower($_post[' Vcode ']); $str=Strtolower($_session[' Vstr ']); //receive the user name and password for form delivery    $name=$_post[' username ']; $pwd=$_post[' Password ']; $repwd=$_post[' Repassword ']; //Determine if the password is consistent    if($pwd!=$repwd){        Echo"<script>alert (' two password input inconsistent, please re-enter ');</script>"; Echo"<script>location= ' regform.html ' </script>"; }Else{        //determine if the verification code is correct        if($code!=$str){              Echo"<script>alert (' Verification code input error, please re-enter ');</script>"; Echo"<script>location= ' regform.html ' </script>"; }Else{              //connect to MySQL database via PHP            $conn=mysql_connect("localhost", "", "" "); //Select Database            mysql_select_db("Test"); //set up client and connection character sets            mysql_query("Set Names UTF8"); //insert operation via PHP            $sqlinsert= "INSERT into T1 (Username,password) values (' {$name}‘,‘{$pwd}‘)"; //select operation via PHP            $sqlselect= "SELECT * from T1 ORDER by id"; //Adding user information to a database            mysql_query($sqlinsert); //returns the user information character set            $result=mysql_query($sqlselect); Echo"; Echo"; Echo"<table width= ' 700px ' border= ' 1px ' >"; //take a line from the result            Echo"<tr>"; Echo"<th>ID</th><th>USERNAME</th><th>PASSWORD</th>"; Echo"</tr>";  while($row=Mysql_fetch_assoc($result)){                Echo"<tr>"; //Print out $row this line                 Echo"<td>{$row[' ID ']} </td><td>{$row[' Username ']} </td><td>{$row[' Password ']} </td> "; Echo"</tr>"; }            Echo"</table>"; //Releasing connection Resources            Mysql_close($conn); }     }?>

The main function of this page is to accomplish the most important functions. The data submitted by the form is stored in variables, and then the password and verification code are judged correctly, and then the user information is stored in the database and all data extracts are printed in the table where the database holds the user information. To put it bluntly, the second sentence is data deposit and extraction. The comments in the code are quite clear, and basically every use is involved. But there are some easy-to-ignore points to point out.:<1> Connection Database function mysql_connect ("localhost", "username", "password"). The user name and password, according to everyone's own configuration to write, my database is too lazy to encrypt so it is empty.

<2> when data is inserted into the database

VALUES (",") must be enclosed in single quotation marks (with double quotes in the outer, only in single quotes), because the data itself is a string, and you cannot write a variable directly. If you do not add single quotes, $name and $pwd variables are parsed into several characters, not in quotation marks, who knows who. Must be an error.

<3> a while loop is used to print the extracted data. The MYSQL_FETCH_ASSOC () function inside the returned array, the subscript is the data belongs to the field name. Functions with similar functions are mysql_fetch_array (), Mysql_fetch_row (), Mysql_fetch_field (). For specific use please see requirements, generally mysql_fetch_assoc () comparison commonly used.

php+mysql-form data into database and data extraction complete process

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.