Blogger Information
Blog 6
fans 0
comment 0
visits 4820
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
程序功能设计考点
Maple201的博客
Original
679 people have browsed it

    编写一个在线留言本,实现用户的在线留言功能,留言信息存储到数据库,要求设计数据表内容以及使用php编码完成。

  1. 数据表设计

    分析数据表结构

    留言信息:ID,留言标题,留言内容,留言时间,留言人

2.创建表语句

   留言本表message:id,title,content,created_at,user_name

3.选择php链接数据库方式

pdo:可扩展性更好,支持预处理,面向对象(推荐)

mysqli:只支持mysql操作,支持预处理,面向对象和过程,效率更高

mysql:只支持mysql数据库,没有预处理,面向过程

4.编码能力

PDO的基本操作

<?php

try{

            //操作数据库代码

$pdo = new PDO($dsn,$username,$password,$attr);

$sql= 'SELECT id,title,content FROM message where user_name=:user_name';

$stmt = $pdo->prepare($sql);

$stmt->excute([':user_name' =>$user_name]);

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

}catch(PDOException $e){

    echo $e->getMessage();

}


分析应该存储哪些信息,设计好数据表,然后创建数据表,PDO来连接mysql,预处理防注入,可扩展。

<? php

$title = $_POST['title'];

$content = $_POST['content'];

$user_name = $_POST['username'];

if(empty($title) || empty($content) || empty(user_name)){

exit('标题或者内容或者用户名不能为空');

}

try{

$dsn='mysql:dbname=test;host=localhost';

$username = 'test';

$password = 'test';

$attr = [

PDO::ATTR_ERRMODE =>PDO::ERROR_EXCEPTION

];

$pdo = new PDO($dsn,$username,$password,$attr);

$sql= 'insert into message(title,content,create_at,user_name)  values(:title,:content,:created_at,:user_name)';

$stmt = $pdo->prepare($sql);

$data=[

':title' => $title,

':content' => $content,'

':created_at' => time(),

':user_name'=>$username

];

$stmt->excute($data);

$rows = $stmt->rowCount();

if($rows){

        exit('添加成功');

}else{

        exit('添加失败);

}

}catch{

//异常

echo $e->getMessage();

}

——————————————————————————————————————

设计一个无限分类表

id             title                pid                path

1              服装                  0                    0-1

2              上衣                  1                    0-1-2

3              长袖                  2                    0-1-2-3

pid:上衣属于服装,长袖属于上衣

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post