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     ***/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Make Me Elvis - Add Email</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<img src="blankface.jpg" width="161" height="350" alt="" style="float:right" />
<img name="elvislogo" src="elvislogo.gif" width="229" height="32" border="0" alt="Make Me Elvis" />
<p>Enter your first name, last name, and email to be added to the <strong>Make Me Elvis</strong> mailing list.</p>
<form method="post" action="addemail.php">
<label for="firstname">First name:</label>
<input type="text" id="firstname" name="firstname" /><br />
<label for="lastname">Last name:</label>
<input type="text" id="lastname" name="lastname" /><br />
<label for="email">Email:</label>
<input type="text" id="email" name="email" /><br />
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>

/***    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    ***/

<?php
/**
* Created by IntelliJ IDEA.
* User: beyond_acm
* Date: 8/20/2015
* Time: 3:00 PM
*/
$dbc = mysqli_connect("localhost","root","root",elvis_store)
or die("Error connection to MySQL Server");
echo 'Connected successful </br>'; $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 </br>';
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    ***/

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Make Me Elvis - Send Email</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<img src="blankface.jpg" width="161" height="350" alt="" style="float:right" />
<img name="elvislogo" src="elvislogo.gif" width="229" height="32" border="0" alt="Make Me Elvis" />
<p><strong>Private:</strong> For Elmer's use ONLY<br />
Write and send an email to mailing list members.</p>
<form method="post" action="sendemail.php">
<label for="subject">Subject of email:</label><br />
<input id="subject" name="subject" type="text" size="30" /><br />
<label for="elvismail">Body of email:</label><br />
<textarea id="elvismail" name="elvismail" rows="8" cols="40"></textarea><br />
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>

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       ***/      

 <?php
/**
* Created by IntelliJ IDEA.
* User: beyond_acm
* Date: 8/20/2015
* Time: 6:23 PM
*/
$from = 'beyond_acm@163.com'; $subject = $_POST['subject'];
$text = $_POST['elvismail']; $dbc = mysqli_connect("localhost","root","root","elvis_store")
or die("Error connectiong to MySQL");
echo "Connecting success! </br>"; $query = "SELECT * FROM email_list";
$result = mysqli_query($dbc, $query)
or die("Error querying database!");
echo "Quering success! </br>"; 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. "<br/>";
}
mysqli_close($dbc);
?>

#2 create and populate a database && realistic and practical applications的更多相关文章

  1. #2 create and populate a database && realistic and practical applications (PART 2)

    Extends from the last chapter , This chapter takes a look at some real-world problems that can occur ...

  2. Create a SQL Server Database on a network shared drive

    (原文地址:http://blogs.msdn.com/b/varund/archive/2010/09/02/create-a-sql-server-database-on-a-network-sh ...

  3. [转]How to: Create a Report Server Database (Reporting Services Configuration)

    本文转自:https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ms157300%28v%3dsql.10 ...

  4. Create an Azure SQL database in the Azure portal

    Create a SQL database An Azure SQL database is created with a defined set of compute and storage res ...

  5. [Windows Azure] How to Create and Configure SQL Database

    How to Create and Configure SQL Database In this topic, you'll step through logical server creation ...

  6. HiveSQLException: Error while compiling statement: No privilege 'Create' found for outputs { database:default }

    今天用Hive的JDBC实例时出现了HiveSQLException: Error while compiling statement: No privilege 'Create' found for ...

  7. [odb-users] Create schema error (unknown database schema '')

    Boris Kolpackov boris at codesynthesis.comFri May 31 11:13:02 EDT 2013 Previous message: [odb-users] ...

  8. [置顶] How to create Oracle 11g R2 database manually in ASM?

    Step 1: Specify an Instance Identifier (SID) export ORACLE_SID=maomi Step 2: Ensure That the Require ...

  9. Create schema error (unknown database schema '')

    Andrey Devyatka 4 years ago Permalink Raw Message Hi,Please tell me, can I use the static library in ...

随机推荐

  1. 数据库2_sqlHelper

    封装一个受影响的行 public static int ExcuteNonQuery(string sqlText,params SqlParameter[] parameters) { using ...

  2. Java基础:(八)异常

    Throwable可以用来表示任何可以作为异常抛出的类,分为两种:Error和Exception.其中Error用来表示JVM无法处理的错误, Exception又分为两种: 受检异常:需要用try. ...

  3. JS和jquery获取各种屏幕的宽度和高度的代码

    Javascript: 网页可见区域宽: document.body.clientWidth 网页可见区域高: document.body.clientHeight 网页可见区域宽: document ...

  4. MUI获取文本框的值

    MUI事件绑定注意父节点.子节点(也可以是标签选择器) js部分 html部分

  5. JavaScript笔记3--标识符和保留字

    1.标识符 javaScript标识符必须以字母,下划线(_)或美元符($)开始.后续的字符可以是字母/数字/下划线/美元符.也可以使用非英语语言或数学符号来书写标识符; 2.保留字 break/de ...

  6. Android 适配底部返回键等虚拟键盘的完美解决方案

    这个问题来来回回困扰了我很久,一直没能妥善解决. 场景1:华为手机遮挡了屏幕底部. 场景2:进入应用时,虚拟键自动缩回,留下空白区域. 需求: 需要安卓能自适应底部虚拟按键,用户隐藏虚拟按键时应用要占 ...

  7. C#调用SAP S4/HANA Gateway Service

    公司使用SAP,并且实施公司做了一些提供报表数据的接口供调用,首先说明一下我对SAP不熟悉 但SAP用到的接口信息提供大家参考一下,这些Gateway Service使用的是DCP方式写的,SAP提供 ...

  8. HDU 2546 饭卡(带限制的01背包变形)

    思路:有几个解法,如下 1)先拿出5块买最贵的菜,剩下的菜再进行01背包.如何证明正确性?设最贵的菜价e,次贵的菜价s,设减去5后的余额为x,会不会产生这样的情况,假设用5元买了e,余额最多能买到x- ...

  9. java入门第一章——java开发入门

    习题解答 一.填空题 (p2)1.java的三个技术平台分别是(java SE.java EE.java ME)(标准.企业.小型) (p3)2.java程序的运行环境简称为(JRE)(开发环境-JD ...

  10. Caused by: java.lang.IllegalStateException: javax.websocket.server.ServerContainer not available

    java.lang.IllegalStateException: Failed to load ApplicationContext    at org.springframework.test.co ...