PHP+MySQL開發小項目的集合筆記(一)提交表單操作數據庫

環境爲: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標籤內。因爲沒有多數據,所以不需要循環。

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章