资讯专栏INFORMATION COLUMN

php+mysql实现留言板

番茄西红柿 / 2871人阅读

摘要:文章目录写出留言板的代码连接的代码利用来美化留言板我的代码为留言板提交连接的代码

文章目录


1、写出留言板的html代码
2、连接mysql的代码
3、利用bootstrap来美化留言板

1、我的html代码为

<html><head>    <meta charset='utf-8'>    <title>留言板</title>    <style>        .a{ width:100%;            margin:0 auto;        }        textarea{            width: 100%;            height:100px;            margin-bottom:10px;        }         .b{            border:  solid 1px #28e7d7;margin-top:10px; padding:5px;         }          .u{            float:left;        }           .s{            float:right;        }    </style>        </head><body>     <div calss='a'>     <form action="db.php" method="POST">        <textarea name='content'></textarea>        <input name='username'  type='text'/>        <input class='s'type='submit'  value='提交'/>        <div style='clear:both;'></div>     </div>    </form>    <div class='a'>        <?php        foreach ($rows as $key => $test) {        ?>        <div class='b'>            <p><?php echo $test['username'];?></p>            <p><?php echo $test['content'];?></p>                </div >         <?php         }        ?>        </div></body></html>

2、连接mysql的代码

 $username=$_POST['username']; $content=$_POST['content'];  var_dump($username,$content);// 检验传来的数据是否有误$dsn='mysql:dbname=quan;host=localhost';$pdo=new PDO($dsn,'root','root');// 上一行以及这一行是连接数据库$sql="INSERT INTO test (username,content) VALUES('{$username}','{$content}')";// 插入语句到test表中,数值为values后的两个//  echo $sql;//检验是否可以输出$sth=$pdo->prepare($sql);// 准备执行//  var_dump($sql);$sth->execute();// 执行header('location:index.php');//完成上述操作后回到留言板界面?> 

扩充:trim函数(自动删除代码之中的空格键)

3、利用bootstrap美化
这里是链接
这是之后的代码(其中我将公用连接mysql的代码多带带放置于一文件中,如果不理解,可以观看编程狮十天学会php)来了解
下面分别是留言板界面和数据库语句界面

include('7.php');$sql = "SELECT * FROM `test` ORDER BY  id DESC";$rows=read($pdo, $sql);?><!doctype html><html lang="en"><head>  <!-- Required meta tags -->  <meta charset="utf-8">  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">  <!-- Bootstrap CSS -->  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">  <title>留言板</title></head><body>  <div class="container">    <div class="jumbotron">      <h1 class="display-4">留言板</h1>      <p class="lead">这里是留言区,请大家提出自己的意见</p>    </div>    <form action="db.php" method="POST">      <div class='row'>        <div class='col-12'>          <div class='form-group'>            <textarea name='content' class="form-control" rows='4'></textarea>          </div>        </div>        <div class='col-3'>          <div class='form-group'>            <input name='username' class='form-control' type='text' />          </div>        </div>        <div class='col-9 d-flex'>          <div class="form-group ml-auto">            <input class='btn btn-primary' type='submit' value='提交' />          </div>        </div>      </div>    </form>    <div class='row'>      <?php      foreach ($rows as $key => $tesy) {      ?>        <div class='col-12'>          <div class='border rounded p-2 mb-2'>            <div class='text-primary'>              <p><?php echo $tesy['username']; ?></p>            </div>            <div>              <p><?php echo $tesy['content']; ?></p>            </div>          </div>        </div>      <?php      }      ?>    </div>  </div></body></html>

数据库界面(上述代码中7.php为下面代码的文件名)

$dsn='mysql:dbname=test;host=127.0.0.1';$pdo=new PDO($dsn,'root','root');function write($pdo,$sql){$sth=$pdo->prepare($sql);return $sth->execute();}function read($pdo,$sql){$sth=$pdo->prepare($sql);$sth->execute();$rows=$sth->fetchAll();return $rows;}?>

实现效果


下面与我自己的代码界面进行对比(7.php不再写,与上面一致)

    include('7.php');    $sql="SELECT * FROM `test` ORDER BY  id DESC";    $sth= $pdo->prepare($sql);    $sth->execute();    $rows=$sth->fetchAll();?><html><head>    <meta charset='utf-8'>    <title>留言板</title>    <style>        .a{ width:100%;            margin:0 auto;        }        textarea{            width: 100%;            height:100px;            margin-bottom:10px;        }         .b{            border:  solid 1px #28e7d7;margin-top:10px; padding:5px;         }          .u{            float:left;        }           .s{            float:right;        }    </style>        </head><body>     <div calss='a'>     <form action="db.php" method="POST">        <textarea name='content'></textarea>        <input name='username'  type='text'/>        <input class='s'type='submit'  value='提交'/>        <div style='clear:both;'></div>     </div>    </form>    <div class='a'>        <?php        foreach ($rows as $key => $test) {        ?>        <div class='b'>            <p><?php echo $test['username'];?></p>            <p><?php echo $test['content'];?></p>                </div >         <?php         }        ?>        </div></body></html>

输出结果


可见bootstarp对于我这种无想象力的小白作用是非常之大的,大家也可以借鉴bootstarp来美化自己的界面

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/125060.html

相关文章

  • CSRF攻击是什么并且如何防止

    摘要:,意为跨网站请求伪造,也有写为。攻击者伪造目标用户的请求,然后此请求发送到有漏洞的网站,网站执行此请求后,引发跨站请求伪造攻击。 CSRF(Cross Site Request Forgeries),意为跨网站请求伪造,也有写为XSRF。攻击者伪造目标用户的HTTP请求,然后此请求发送到有CSRF漏洞的网站,网站执行此请 求后,引发跨站请求伪造攻击。攻击者利用隐蔽的HTTP连接,让目标...

    Flands 评论0 收藏0
  • 一篇写给新手入门PHP的文章

    摘要:如果在学习过程中有什么问题可以关注我公众号琉忆编程库给我留言。有兴趣可以深入学习最后附上学习的知识结构图谱,可以按着下面的这个知识结构图进行学习不一定完整,更多资料,面试题,都可以关注公众号琉忆编程库获取。 你好,是我——琉忆。PHP程序员面试系列图书作者。 作为一名PHP开发者过来人,也是经历了菜鸟到老手的过程,在此给那些想学PHP的同学指条路,即使你是转行学PHP一样可以学会PHP...

    Ilikewhite 评论0 收藏0
  • 系统的讲解 - PHP WEB 安全防御

    摘要:支持自动识别密码哈希格式并通过字典破解密码哈希。支持枚举用户密码哈希权限角色数据库数据表和列。支持在数据库管理系统中搜索指定的数据库名表名或列名。水平越权用户未授权可以访问用户的数据。对于所有需要权限控制的位置,必须严格检验用户权限级别。 常见漏洞 showImg(https://segmentfault.com/img/bVbst5x?w=918&h=921); 看到上图的漏洞是不是...

    LinkedME2016 评论0 收藏0
  • php项目—个人博客

    摘要:链接地址田超的博客源码放在介绍学习语言做的练习,由最初的一个留言板拓展到现在,还将持续更新中工具,,,主要技术构成前端后端博客功能前台页面,文章目录归档,文章标题搜索,留言版,说说,文章评论,注册和登录,文章说说点赞后台页面,文章发布,说说 链接地址:田超的博客源码放在:github 介绍: 学习php语言做的练习,由最初的一个留言板拓展到现在,还将持续更新中…… 工具 1,MacO...

    Dean 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<