环境为:phpStudy 2016 php-5.4.45
数据库环境:http://localhost:81/phpMyAdmin/index.php
连接环境:E:\phpStudy\WWW\easyPHP\increase.html
所有内存都放在easyPHP文件夹内。
连接:http://localhost:81/easyPHP/increase.html
需求:提交表单,写入数据库。提交查看,查看数据库。提交删除,删除数据库。简单的单条数据即可。
预估时间:一天
MySQL脚本:
CREATE TABLE 'message' (
'id' tinyint(1) NOT NULL auto_increment,
'user' varchar(25) NOT NULL,
'position' varchar(50) NOT NULL,
'content' tinytext NOT NULL,
'lastdate' timestamp NOT NULL,
PRIMARY KEY ('id')
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
实现:建立data1的数据库,建立message的表单类型为utf8_general_ci,创建id类型tinyint(1),创建user类型varchar(25),创建position类型varchar(50),创建content类型tinytext,创建lastdate类型timestamp。设置主键为id。设置id为逐一递增(因为只有一条记录,所以然并卵)。
dao.php脚本:
<?php
$dao =mysql_connect("localhost", "root", "root1");
if (!$dao)
{
die('MySQL connect failed at first ' . mysql_error());
}
$db_selected =mysql_select_db("data1", $dao);
mysql_query("set names 'utf8'");
?>
实现:连接数据库,在连接失败后提示failed at first,和error代码。选择data1的数据库。设置查询utf8。
delete.php脚本:
<?php
include ("dao.php");
$id =$_POST["id"];
$user =$_POST["user"];
$position =$_POST["position"];
$content =$_POST["content"];
if ($_POST["delete"]){
$sql ="DELETE FROM message WHERE id=0";
mysql_query($sql);
echo "<script>alert('Delete Success');location.href='increase.html';</script>";
}
?>
实现:引入dao.php。实现删除id为0的数据,如果成功使用JavaScript提示出Success并跳转回increase.html
drawing.css使用css渲染页面:
form{
margin:0px;
padding:0px;
}
body{
font-size:10px;
line-height:13px;
backgroud-color:#b5e1e7;
}
textarea{
font-size:10px;
border:1px solid #9999CC;
padding:5px;
}
td{
line-height:16px;
font-size:10px;
font-family:"Microsoft YaHei", "΢ÈíÑźÚ";
}
a:link{
text-deoration:none;
color:#000000;
}
increase.php点击submit后把数据传到数据库
<?php
include ("dao.php");
$id =$_POST["id"];
$user =$_POST["user"];
$position =$_POST["position"];
$content =$_POST["content"];
if ($_POST["submit"]){
$sql ="insert into message(id,user,position,content,lastdate)VALUES
('','$user','$position','$content',now())";
mysql_query($sql);
echo "<script>alert('Submit Success');location.href='increase.html';</script>";
}
?>
实现:$_POST["submit"]出现后执行添加功能并提示添加成功,返回increase.html页面。
前台页面increase.html有多个选择按钮
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Submit and Display</title>
<?php include("increase.php")?>
<link href="drawing.css" rel="stylesheet" type="text/css">
</head>
<body>
<a href="#revenue-chart" data-toggle="tab">Area</a>
<b><a href="list.php">Display</a> </b>
<hr size="1">
<form action="increase.php" method="post">
Who you are: <input type="text" size="10" name="user" /><br/>
What do you do:<input type="text " name="position" /><br/>
How do you do it:<input type="text" name="content"></textarea><br/>
<input type="submit" name="submit" value="SUBMIT" />
</form>
<form action="delete.php" method="post">
<input type="submit" name="delete" value="DELETE" />
</form>
</body>
</html>
实现:引入increase.php和drawing.css。点击Display字体后跳转到list.php页面。输入表单,然后提交action为“submit”。点击DELETE按钮提交action为delete.php页面。
list.php最复杂的读取数据库并显示到页面,因为是跳转不用ajax,只能用拼接php和html。
<!DOCTYPE html>
<html lang="en">
<head>
<?php include ("dao.php");?>
<meta charset="UTF-8">
<title>Submit and Display</title>
<link href="drawing.css" rel="stylesheet" type="text/css">
</head>
<body>
<table wide="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#a9a9a9">
<?php
$sql="select * from message ORDER BY id DESC" ;
$query=mysql_query($sql);
while ($row=mysql_fetch_array($query)){
?>
<tr bgcolor="#eff3ff">
<td>user: <?php echo $row['user'];?>
position: <?php echo $row['position'];?>
</td>
</tr>
<tr bgcolor="#ffffff">
<td>content: <?php echo $row['content'];?></td>
</tr>
<tr bgcolor="#ffffff">
<td>time: <?php echo $row['lastdate'];?></td>
</tr>
<?php }?>
<tr bgcolor="#66ff00">
<td>Return: <a href="increase.html">Return</a></td>
</tr>
</table>
</body>
</html>
实现:引入dao.php和drawing.css。建立单一表格,然后把从数据库查到的数据装入到html标签内。因为没有多数据,所以不需要循环。