Php+mysql ———— form data insertion database and data extraction

Source: Internet
Author: User

Site in the new user register, the user's registration information will be stored in the database, the need for the time to extract. A simple example is written today.

Main completed the following several functions:

(1) The user carries on the registration, realizes the password repeatedly confirms, the verification code proofreading function.

(2) After the 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 the past several blogs, shared the registration and login form code. This time the code, roughly the same, is only slightly different. 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;//join? 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>

For the verification code function, in a previous blog, has carried out specific commentary. This time the code is basically directly used, the only upgrade is to join? The interferon, so that the verification code does not have a dry four characters where. The Imagesetpixel () function is used to create some interference points. For specific usage, please check the PHP manual.

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 form delivery username and password$name=$_post[' username ']; $pwd =$_post[' password '; $repwd =$_post[' Repassword '];// Infer if the password is consistent if ($pwd! = $repwd) {echo "<script>alert (' two password input inconsistent, please enter again ');</script>"; echo "<script> location= ' regform.html ' </script> ';}  else{//infers if the captcha is correct if ($code! = $str) {echo "<script>alert (' Code input error, please enter ' again ');</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 via PHP $sqlinsert= "insert into T1 (Username,password) VALUES (' {$name} ', ' {$pwd} ');//select operation via PHP $sqlselect= "SELECT * from T1 order by id";//join? user information to Database mysql_query ($sqlinsert) ;//return user information Character set $result=mysql_query ($sqlselect); echo "
The main function of this page is to complete the most important functions. The data submitted by the form are stored in variables, and then the password and verification code are inferred, and then the user information is stored in the database and all the data in the table that holds the user information in the database is printed. To put it bluntly, the second sentence is data deposit and extraction. In the code the gaze is 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"). Among 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 added with a single argument (the outer layer is double-cited, it can only use a single-quote), because its own data is a string, you can not directly write a variable is finished. Assuming that no single argument is added, $name and $pwd variable are parsed into several characters, not within the argument, 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 detailed use please see the requirements, generally mysql_fetch_assoc () more often used.


Php+mysql ———— form data insertion database and data extraction

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.