Here, we want to talk more about PHP and MySQL connection, add and query.
We take an investigative procedure as an example:
1, from the MySQL database to establish a connection to start.
First, we should create a Infostar table in MySQL in a database:
Follow the steps to test MySQL and see the "mysql>" prompt to manually build the table we need:
Mysql>create DATABASE MyDB
Mysql>use MyDB
Mysql>create TABLE Infostar (
->name VARCHAR (25),
->email VARCHAR (25),
->choice VARCHAR (10));
Well, the watch is ready. The following first set up with the user to meet the form (pure HTML file)
Diaocha.htm
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>
<body bgcolor= "#CCCCCC" >
<center>
<form method= "POST" action= "diaocha.php" >
<table border>
<tr><td> Name:
<td>
<input type= "text" name= "name" size= "maxlength=" "value=" ">
<tr><td>email:
<td>
<input type= "text" name= "email" size= "maxlength=" "value=" ">
<tr><td> Please choose:
<td>
<input type= "Radio" name= "Choice" value= "Apple" > Apples
<input type= "Radio" name= "Choice" value= "Orange" > Orange
<input type= "Radio" name= "Choice" value= "pear" > Pear
<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 fruit do you like to eat? Top of form
name: |
|
email: |
|
Please select: |
Apple Pear coconut Watermelon > banana |
Bottom of form |
Here's how to build a PHP program to receive and process information from diaocha.htm forms
diaocha.php
<?php
/* Define some related variables for the following mysql_connect () function with the */
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "MyDB";
/* Used to store the email address of the site supervisor * *
$adminaddress = "webmaster@youweb.com";
/* This is the table we just created to store user data.
$userstable = "Infostar";
/* Set up the connection below * *
Mysql_connect ($hostname, $username, $password) or die ("Unable to connect database");
/* Below Select database MyDB, precede function with (@) symbol will suppress error message display.
@mysql_select_db ("$dbname") or Die ("Unable ro Select database");
print "<center>";
Print "Hello: $name.";
print "<br><br>";
Print "Your email is: $email <br><br>";
Print "Thank you for your participation <br><br>";
/* Insert the information into the table Infostar * *
/* Because the database table was previously selected with the mysql_select_db function. Therefore, the following is not required in the input table name * *
$query = "INSERT into $userstable VALUES (' $name ', ' $email ', ' $choice ')";
$result =mysql_query ($query);
Print "Your information we have saved to the database.";
/* Close Database connection/*
Mysql_close ();
?>
Note: If you are programming as above: Define string variables at the outset, it is easier to modify them.
We have put the information into the database, so how can we browse the data?
Next, we try to make a list of all the friends who like to eat Apple, set up apple.php file
apple.php
<?php
/* Define some related variables for the following mysql_connect () function with the */
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "MyDB";
$userstable = "Infostar";
/* Set up the connection below and select the 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 how many of these users * *
$number =mysql_num_rows ($result);
* * Show the results
$i = 0;
IF ($number ==0) {
Print "<center><p> no one likes Apple </center>";}
else{
Print "<center><p> like to eat Apple users have: $number <br><br>";
while ($i < $number):
$name =mysql_result ($result, $i, "name");
$email =mysql_result ($result, $i, "email");
Print "$name like to eat Apple <br>";
print "Mail address: $email <br><br>";
$i + +;
Endwhile;
print "</center>";
}
?>
Call apple.php in the browser to see what appears.