#2 create and populate a database && realistic and practical applications
阅读原文时间:2022年04月02日阅读:1

The Chapter3 & Chapter4 of this book tells you how to create a realistic app on the web through the lab.

That is really amazing when you finished yourself. I will show you how to make it as follows.

FIRST OF ALL, we need a plan of how to make an application, generally speaking, you can following the steps like this(as we did it in the lab1):

1. Create a database and table for the app (the email list)

2. Create and edit a web form for the customer

3. Create and edit a PHP script to handle the web form

To Finish the application, we should start with the table, actually, it all starts with a database.

step 1 :

what you have to do is to use these command line with MySQL :

CREATE DATABASE elvis_store

Then you need to create a table inside the database, just like we did in the lab1, but beofore you create tables, Please

make sure you have selected the database or you will get an ERROR. This command may help you with it :

USE elvis_store

Next you can create table(s) inside this database, table structure is based on your application, In this app you can design the table like this:

CREATE TABLE email_list
(
first_name VARCHAR (20) ,
last_name VARCHAR (20) ,
email VARCHAR (60)
);

It is quite simple, yeah ? Next we will move to step2:

step2 :

make a directory to store this application, you can name it lab2 or anything you want, and add some html files && css files to it :

/***    addemail.html     ***/


Make Me Elvis - Add Email Make Me Elvis

Enter your first name, last name, and email to be added to the Make Me Elvis mailing list.



¨C9C ¨C10C

/***    style.css        ***/

body, td, th {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}

If you run it on your apache Server , you will get a page like this :

Its just a simple form and serveral css. Very Simple. Now we have finished step2.

The only thing we need to notice is that this form's action = 'addemail.php' which means it will send this form to the file on the Server.

lets move to the step3. you may probably know how to do it, yes create an edit addemail.php file on the Server.

Step3:

you create the addemail.php file in the same folder and edit it like this :

/***    addemail.php    ***/

'; $first\_name = $\_POST\['firstname'\]; $last\_name = $\_POST\['lastname'\]; $email = $\_POST\['email'\]; $query = "INSERT INTO email\_list(first\_name, last\_name, email)". "VALUES('$first\_name', '$last\_name', '$email')"; mysqli\_query($dbc,$query) or die("Error querying database"); echo 'Quering successful
'; echo 'Custom added !'; mysqli\_close($dbc); ?>

If you finish it correctly , after you submit the form you will see "Connected successful Quering successful Custom added !" on the screen.

You can alse check the database in MySQL to confirm it. Use "SELECT * FROM emal_list".

what we will do next is to repeat the step2 and step3. We will need another form to send the email to the cosumer who have registered .

we will still use the email_list database, so we dont need to design another database. First we need a Form to collect data about the email.

create sendemal.html in the same folder(like lab2) :  

/***     sendemail.html    ***/


Make Me Elvis - Send Email Make Me Elvis

Private: For Elmer's use ONLY
Write and send an email to mailing list members.




¨C25C ¨C26C

if you visit this page you will see sth like this :

Thats a simple form which you can input the email title and the email body, then you click submit it will send to the sendemail.php on the Web Server which we will edit next.

It can send this package email to all the register user in the email_list table. create and edit the sendemail.php as follows:

/***      sendemail.php       ***/      

"; $query = "SELECT \* FROM email\_list"; $result = mysqli\_query($dbc, $query) or die("Error querying database!"); echo "Quering success!
"; while( $row = mysqli\_fetch\_array($result) ) { $first\_name = $row\['first\_name'\]; $last\_name = $row\['last\_name'\]; $to = $row\['email'\]; $msg = "Dear $first\_name $last\_name, \\n $text"; mail($to, $subject, $msg, 'From: '.$from ); echo 'Emai sent to :'.$to. "
"; } mysqli\_close($dbc); ?>