php+mysql留言板小案例
生活随笔
收集整理的這篇文章主要介紹了
php+mysql留言板小案例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.連接數(shù)據(jù)庫
<?php $link = mysqli_connect('localhost','數(shù)據(jù)庫登錄名','數(shù)據(jù)庫登陸密碼','要連接的數(shù)據(jù)庫名稱'); #連接數(shù)據(jù)庫$result = mysqli_query($link,'SELECT * FROM liuyan '); #獲取數(shù)據(jù)庫中l(wèi)iuyan表里的內(nèi)容 ?>2.向數(shù)據(jù)庫寫入內(nèi)容
<meta charset="utf-8"> <!DOCTYPE html> <html lang="en"> <head></head>#樣式 <style>ul,li{list-style: none}.ly{text-align: center}.author{} </style> <body>#連接數(shù)據(jù)庫 <?php $link = mysqli_connect('localhost','root','123','liuyanban'); $result = mysqli_query($link,'SELECT * FROM liuyan '); ?>#將表里的內(nèi)容遍歷出來 <?php while($row = mysqli_fetch_array($result)){ ?> <ul class="ly"><div class="author"><?php echo $row['author']; ?></div><div class="time"><?php echo $row['time']; ?></div><div style="clear: both"><?php echo $row['content']; ?></div><hr> </ul> <?php }?>#通過post獲取表單中的內(nèi)容 <form style="text-align: center" method="post"><p>作者</p><input type="text" name="author" ><p>內(nèi)容</p><textarea name="content"></textarea><div></div><input type="submit"> </form>#向數(shù)據(jù)庫寫入用戶輸入的數(shù)據(jù) <?php $author = $_POST['author']; $content = $_POST['content']; $time =date("Y-m-d H:i:s"); $sql = "insert into liuyan values(null,'$author','$time','$content');"; if (mysqli_query($link,$sql)){echo "插入成功!"; }else{echo "失敗!"; } mysqli_close($link); #關閉數(shù)據(jù)庫連接 ?> </body> </html>數(shù)據(jù)庫名為liuyanban
liuyan表結構如下:
總結
以上是生活随笔為你收集整理的php+mysql留言板小案例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python爬虫之美剧排行榜爬取(加浏览
- 下一篇: mysql安装了老版本_Windows环