1. Establish a connection to the MySQL database.
First, we should create a database in MySQL to create an infostar table:
Follow the steps to test MySQL, see the "mysql>" prompt and manually create the table we need:
Mysql> Create Database mydb
Mysql> Use mydb
Mysql> Create Table infostar (
-> Name varchar (25 ),
-> Email varchar (25 ),
-> Choice varchar (10 ));
Now, the table is created. Next we will first create a form for meeting users (a pure HTML file)
Diaocha.htm
<HTML>
<Head>
<Title> new document </title>
</Head>
<Body bgcolor = "# cccccc">
<Center>
<H1> what kind of fruit do you like <Form method = "Post" Action = "diaocha. php">
<Table Border>
<Tr> <TD> Name:
<TD>
<Input type = "text" name = "name" size = "20" maxlength = "20" value = "">
<Tr> <TD> Email:
<TD>
<Input type = "text" name = "email" size = "20" maxlength = "20" value = "">
<Tr> <TD> select:
<TD>
<Input type = "radio" name = "choice" value = "apple"> Apple
<Input type = "radio" name = "choice" value = "orange"> orange
<Input type = "radio" name = "choice" value = "Pear"> pears
<Br>
<Input type = "radio" name = "choice" value = "Coconut"> coconut
<Input type = "radio" name = "choice" value = "watermelon"> Watermelon
<Input type = "radio" name = "choice" value = "banana"> bananas
</Table>
<P>
<Input type = "Submit" name = "submit_button" value = "OK">
<Input type = "reset" name = "reset_button" value = "reset">
</Form>
</Body>
</Html>
The format is as follows:
What kind of fruit do you like
Name:
Email:
Please select: Apple orangli
Coconut, watermelon, and bananas
Next we will build PHPProgramTo receive and process the information from the diaocha.htm form.
Diaocha. php
<? PHP
/* Define related variables for the mysql_connect () function below */
$ Hostname = "localhost ";
$ Username = "root ";
$ Password = "";
$ Dbname = "mydb ";
/* Used to store the Site supervisor's email address */
$ Adminaddress = "webmaster@youweb.com ";
/* This is the table we just created to store user data */
$ Userstable = "infostar ";
/* Establish a connection below */
Mysql_connect ($ hostname, $ username, $ password) or die ("unable to connect database ");
/* Select database mydb. Adding the (@) symbol before the function will suppress the display of error information .*/
@ Mysql_select_db ("$ dbname") or die ("Unable Ro select database ");
Print "<center> ";
Print "Hello, $ name .";
Print "<br> ";
Print "your email is: $ email <br> ";
Print "Thank you for your participation <br> ";
/* Insert information to the infostar table */
/* Because the database table is selected using the mysql_select_db function, the following table name does not need to be input */
$ Query = "insert into $ userstable values ('$ name',' $ email ',' $ choice ')";
$ Result = mysql_query ($ query );
Print "your information has been saved to the Database .";
/* Close the database connection */
Mysql_close ();
?>
Note: For programming, if you define string variables at the beginning, it will be easy to modify.
We have stored the information in the database. How can we view the data?
Next, we will try to list all our friends who like to eat apple and create an apple. php file.
Apple. php
<? PHP
/* Define related variables for the mysql_connect () function below */
$ Hostname = "localhost ";
$ Username = "root ";
$ Password = "";
$ Dbname = "mydb ";
$ Userstable = "infostar ";
/* Create a connection and select a database */
Mysql_connect ($ hostname, $ username, $ password) or die ("unable to connect database ");
@ Mysql_select_db ("$ dbname") or die ("Unable Ro select database ");
/* Select all users who like Apple */
$ Query = "select * from $ userstable where choice = 'apple '";
$ Result = mysql_query ($ query );
/* Calculate the number of such users */
$ Number = mysql_num_rows ($ result );
/* Display the result */
$ I = 0;
If ($ number = 0 ){
Print "<center> <p> no one liked apple </center> ";}
Else {
Print "<center> <p> Users who like Apple: $ number <br> ";
While ($ I <$ number ):
$ Name = mysql_result ($ result, $ I, "name ");
$ Email = mysql_result ($ result, $ I, "email ");
Print "$ name like Apple <br> ";
Print "email address: $ email <br> ";
$ I ++;
Endwhile;
Print "</center> ";
}
?>
Call apple. php In the browser to see what is going on.