8
六月

程式設計二程式 06/08: ex9

   Posted by: admin   in 103(下)程式設計(二)

//ex9.php
<?php
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'phpuser', '23323456');
if (isset($_POST["Submit"]))
{
   //讀入表單參數
   $email = $_POST["EMail"];
   $message = $_POST["Message"];
   $msgdate = date("Y-m-d H:i:s");
   //寫入資料庫
   $query = "INSERT INTO guestbook(email, message, msgdate) VALUES ('$email','$message','$msgdate')";
   $result = $db->exec($query);
}
 
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ex9:Guestbook</title>
</head>
 
<body>
<h1>Ex9:Guestbook</h1>
<form action="ex9.php" method="post">
<table border="1" width="100%">
<tr>
<td colspan="2">請問您有何建議</td>
</tr>
<tr>
<td >EMail:</td><td ><input type="text" name="EMail" /></td>
</tr>
<tr>
<td >留言:</td><td ><input type="text" name="Message" /></td>
</tr>
<tr><td> <input type="submit" name="Submit" value="送出留言" /></td><td></td>
</tr>
</table>
</form>
<h1>留言內容</h1>
<hr />
<table border="1" width="100%">
<?php
$query="SELECT * FROM guestbook ORDER BY msgdate DESC LIMIT 10";
$stmt = $db->prepare($query);
 
$stmt->execute();
while($row = $stmt->fetch()){
   print "<tr>
<td>".$row["gid"]."</td>
<td>".$row["email"]."</td>
<td>".$row['message']."</td>
<td>".$row['msgdate']."</td>
</tr>";
}
?>
</table>

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , , ,

This entry was posted on 星期一, 六月 8th, 2015 at 10:17:53 and is filed under 103(下)程式設計(二). You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a reply

Name (*)
Mail (will not be published) (*)
URI
Comment