首页 > 代码库 > php实现blog
php实现blog
1.建立表单
2.连接数据库,测试其功能
/怎样给按钮加连接?
//<input type="submit" name="add post" value="http://www.mamicode.com/add post">
<a href="http://phpblog.local/test.php?" target="_blank">add post</a>
//<input type="submit" onclick="http://phpblog.local/test.php?">
3.实现index.php页面
1 <!DOCTYPE HTML> 2 <html> 3 <body> 4 <?php $TitleErr = $BodyErr = ""; 5 $Title = $Body = ""; 6 7 <h4>Add post:</h4> 8 <p><span class="error">* Required</span></p> 9 <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 10 11 <label>Title<span class="error">*<?php echo $TitleErr;?>:</span></label> 12 <br> 13 <input type="text" maxlength="50" name="Title" value="http://www.mamicode.com/<?php echo $Title;?>"> 14 <br> 15 16 <label>Body<span class="error">* <?php echo $BodyErr;?>: </span></label><br> 17 <textarea name="Body" rows="5" cols="40"><?php echo $Body;?></textarea>* 18 <br><br> 19 20 <input type="submit" name="save post" value="http://www.mamicode.com/save post"> 21 22 </form> 23 24 //$dbc = mysqli_connect("localhost","root","root","cakephp2"); 25 $servername = "localhost"; 26 $username = "root"; 27 $password = "root"; 28 $dbname = "cakephp2"; 29 30 // 创建连接 31 $conn = new mysqli($servername, $username, $password, $dbname); 32 // 检测连接 33 if ($conn->connect_error) { 34 die("连接失败: " . $conn->connect_error); 35 } 36 37 $sql = "INSERT INTO posts (title, body,created,modified) VALUES ($Title, $Body, NOW(),NOW())"; 38 39 if ($conn->query($sql) === TRUE) { 40 echo "新记录插入成功"; 41 } else { 42 echo "Error: " . $sql . "<br>" . $conn->error; 43 } 44 45 $conn->close(); 46 47 function test_input($data) 48 { 49 $data = trim($data); 50 $data = stripslashes($data); 51 $data = htmlspecialchars($data); 52 return $data; 53 54 } 55 var_dump($_SERVER["REQUEST_METHOD"]); 56 if ($_SERVER["REQUEST_METHOD"] == "POST"){ 57 if (empty($_POST["Title"])){ 58 $nameErr = "Title is required"; 59 60 }else{ 61 $name = test_input($_POST["Title"]); 62 63 } 64 if (empty($_POST["Body"])){ 65 $BodyErr = "Body is required"; 66 67 } 68 else{ 69 $Body = test_input($_POST["Body"]); 70 } 71 } 72 //var_dump($_POST[‘Title‘]); 73 74 ?> 75 76 <?php 77 echo $Title; 78 echo "<br>"; 79 echo $Body; 80 echo "<br>"; 81 ?> 82 </body> 83 </html>
updat.php
<!DOCTYPE HTML> <html> <body> <?php $TitleErr = $BodyErr = ""; $Title = $Body = ""; <h4>Add post:</h4>//显示出错,但没找出来。 <p><span class="error">* Required</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> <label>Title<span class="error">*<?php echo $TitleErr;?>:</span></label> <br> <input type="text" maxlength="50" name="Title" value="http://www.mamicode.com/<?php echo $Title;?>"> <br> <label>Body<span class="error">* <?php echo $BodyErr;?>: </span></label><br> <textarea name="Body" rows="5" cols="40"><?php echo $Body;?></textarea>* <br><br> <input type="submit" name="save post" value="http://www.mamicode.com/save post"> </form> //$dbc = mysqli_connect("localhost","root","root","cakephp2"); $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "cakephp2"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } $sql = "INSERT INTO posts (title, body,created,modified) VALUES ($Title, $Body, NOW(),NOW())"; if ($conn->query($sql) === TRUE) { echo "新记录插入成功"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } var_dump($_SERVER["REQUEST_METHOD"]); if ($_SERVER["REQUEST_METHOD"] == "POST"){ if (empty($_POST["Title"])){ $nameErr = "Title is required"; }else{ $name = test_input($_POST["Title"]); } if (empty($_POST["Body"])){ $BodyErr = "Body is required"; } else{ $Body = test_input($_POST["Body"]); } } //var_dump($_POST[‘Title‘]); echo $Title; echo "<br>"; echo $Body; echo "<br>"; </body> </html>
title.php
<?php echo "<table style=‘border: solid 1px black;‘>"; echo "<tr><th>Id</th><th>Title</th><th>Body</th><th>Update</th><th>created</th></tr>"; class TableRows extends RecursiveIteratorIterator { function __construct($it) { parent::__construct($it, self::LEAVES_ONLY); } function current() { return "<td style=‘width: 150px; border: 1px solid black;‘>" . parent::current(). "</td>"; } function beginChildren() { echo "<tr>"; } function endChildren() { echo "</tr>" . "\n"; } } $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "cakephp2"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $conn->prepare("SELECT * FROM posts"); $stmt->execute(); // 设置结果集为关联数组 $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { echo $v; } $dsn = null; } catch(PDOException $e) { echo "Error: " . $e->getMessage(); } $conn = null; echo "</table>"; ?>
php实现blog
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。