PHP instance ———— form data into database and data extraction

Source: Internet
Author: User

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 Sessionsession_start ();//Prepare Canvas $im= Imagecreatetruecolor (50,25);//preparation of pigments $black=imagecolorallocate ($im, 0,0,0); $gray =imagecolorallocate ($im, 200,200,200)///Background fill Imagefill ($im, 0,0, $gray);//Text Center $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 the text $arr=array_merge (range (0,9), Range (' A ', ' Z '), Range (' A ', ' Z ')), Shuffle ($arr), $str =implode (Array_slice ($arr, 0,4));//Put $str into SESSION, convenient for all pages to call $_session[' Vstr ']= $str; $file = ". /FONTS/SIMSUN.TTC "; Imagettftext ($im, 15,0, $x, $y, $black, $file, $STR);//output to the browser or save the header (" Content-type:image/png "); Imagepng ($im);//Close Canvas imagedestory ($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");//Turn on Sessionsession_start ();//convert the verification code and the string in the input box to lowercase $code=  Strtolower ($_post[' Vcode '); $str =strtolower ($_session[' vstr '); Receive the user name and password for the form delivery $name=$_post[' username '; $pwd =$_post[' password '); $repwd =$_post[' Repassword '];//determine if the password is consistent if ($pwd!). = $repwd) {echo "<script>alert (' two times 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 (' 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 the client and connection character set 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";//Add user information to database mysql_query ($sqlinsert);// Returns the user information character set $result=mysql_query ($sqlselect); echo "
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

$sqlinsert = "INSERT into T1 (Username,password) VALUES (' {$name} ', ' {$pwd} ')";
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 instance ———— form data into database and data extraction

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.