The author has 9 posts.
Font size: Small - 100% (Default)  Content converter: No conversion
 
Clicks Replies
1106 8
一個使用PDO連接MySQL並保存表單內容的例子
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 1 Posted at: 5/31/15 11:41
<?php
include_once("conn.php");

function filterTextarea($source) {
    $source = htmlspecialchars(trim($source));
    $source = str_replace("\r\n", "<br>", $source);
    return $source;
}
?>
<!doctype html>
<html>
<head>
<title>Form</title>
<script>
function checkForm() {
    // stop submitting if the content is empty
    var textarea = document.getElementById("content");
    var content = textarea.value;
    if (content == ""){
        textarea.focus();
        return false;
    }
}
</script>
</head>

<body>
<?php
if (isset($_POST["content"])) {
    // if the form has been submitted
    $content = filterTextarea($_POST["content"]);
    if (mb_strlen($content) >= 500) {
        echo "The content is too long.";
    } else {
        $sql = "INSERT INTO Contents (Content, TimeCreated) VALUES (:content, NOW())";
        $stmt = $dbh->prepare($sql);
        $stmt->bindParam(":content", $content);
        $stmt->execute();
        if ($stmt->rowCount()) {
            echo "Your content has been successfully saved.";
        } else {
            echo "An error occurred when saving your content.";
        }
    }
} else {
?>
<form onsubmit="return checkForm()" method="post">
  <label>Content:</label><br>
  <textarea style="width:300px;height:100px" id="content" name="content"></textarea><br>
  <button>Submit</button>
</form>
<?php } ?>
</body>
</html>
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 2 Posted at: 5/31/15 11:42
插入的一條數據例子:
&lt;script&gt;<br>hablar
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 3 Posted at: 5/31/15 11:43
數據表:
CREATE TABLE `Contents` (
 `ContentID` int(11) NOT NULL AUTO_INCREMENT,
 `Content` varchar(500) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
 `TimeCreated` datetime NOT NULL,
 PRIMARY KEY (`ContentID`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=latin1
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 4 Posted at: 5/31/15 11:43
CONN:
<?php
define("DB_PW", "密碼");

try {
    $dbh = new PDO("mysql:host=localhost;dbname=test", "用戶名", DB_PW, array(PDO::ATTR_PERSISTENT => true));
} catch (PDOException $e) {
    trigger_error($e->getMessage(), E_USER_ERROR);
}
?>
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 5 Posted at: 5/31/15 11:49
<?php
include_once("conn.php");
?>
<!doctype html>
<html>
<head>
<title>View</title>
</head>

<body>
<?php
$sql = "SELECT * FROM Contents ORDER BY ContentID ASC";
$stmt = $dbh->query($sql);
?>
<ol>
  <?php foreach ($stmt as $row): ?>
  <li><?php echo $row["Content"]; ?></li>
  <?php endforeach; ?>
</ol>
</body>
</html>
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 6 Posted at: 5/31/15 11:49
<!doctype html> <html> <head> <title>View</title> </head> <body> <ol> <li>Array</li> <li>firebird, mysql, odbc, pgsql, sqlite</li> <li>Initiates a transaction</li> <li>數據表中已經有相同的內容了。</li> <li>Oh baaaaah!</li> <li>Parse error: syntax error, unexpected '/' in /var/www/html/temp/11330/2.php on line 27</li> <li>295956</li> <li>744210</li> <li>1740</li> <li>997732</li> <li>856175</li> <li>120648</li> <li>76537</li> <li>248696</li> <li>779802</li> <li>d&lt;script&gt;<br>hablar</li> </ol> </body> </html>
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 7 Posted at: 5/31/15 11:50
<!doctype html>
<html>
<head>
<title>View</title>
</head>

<body>
<ol>
    <li>Array</li>
    <li>firebird, mysql, odbc, pgsql, sqlite</li>
    <li>Initiates a transaction</li>
    <li>數據表中已經有相同的內容了。</li>
    <li>Oh baaaaah!</li>
    <li>Parse error: syntax error, unexpected '/' in /var/www/html/temp/11330/2.php on line 27</li>
    <li>295956</li>
    <li>744210</li>
    <li>1740</li>
    <li>997732</li>
    <li>856175</li>
    <li>120648</li>
    <li>76537</li>
    <li>248696</li>
    <li>779802</li>
    <li>d&lt;script&gt;<br>hablar</li>
  </ol>
</body>
</html>

巨大八爪鱼
武林盟主 二十一级
Reply
Floor 8 Posted at: 5/31/15 12:13
解決時區問題和中文亂碼問題:
<?php
define("DB_PW", "密碼");

date_default_timezone_set("Etc/GMT"); // Time zone for date library in PHP
mb_internal_encoding("UTF-8"); // Charset for mbstring library in PHP

try {
    $dbh = new PDO("mysql:host=localhost;dbname=test", "用戶名", DB_PW, array(PDO::ATTR_PERSISTENT => true));
    $dbh->exec("SET time_zone = '+0:00'");
    $dbh->exec("SET names utf8");
} catch (PDOException $e) {
    trigger_error($e->getMessage(), E_USER_ERROR);
}
?>
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 9 Posted at: 5/31/15 12:15
Reply the post
Content:
User: You are currently anonymous.
Captcha:
Unclear? Try another one.
(Shortcut key: Ctrl+Enter)
Post Information
Clicks: 1106 Replies: 8
Author: 巨大八爪鱼
Last reply: 巨大八爪鱼
Last reply time: 5/31/15 12:15
Bar Hot Posts
Announcements