PHP4 and MySQL Interactive use _php Foundation

Source: Internet
Author: User
Tags mysql in pear create database
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?
Name:
Email:
Please choose: Apple orange Pear
Coconut Watermelon Banana




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.

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.