11.2随笔

mysql写法
//mysql_connect("主机名","用户名","密码") 链接池
mysql_connect("127.0.0.1","root","root") or die("链接失败");

mysql_select_db("bbscms");//选择操作的数据库名

//设置字符集编码格式
mysql_query("set names utf8");
$res = mysql_query($sql); //执行sql语句
$row = mysql_fetch_assoc($res); //每执行一次得到一条结果集 返回的数据是数组


mysqli写法
//链接数据库
$link = mysqli_connect("主机名","用户名","密码") 链接池
$link = mysqli_connect("127.0.0.1","root","root") or die("链接失败");

mysqli_select_db(链接池,"要操作的数据库名");
mysqli_select_db($link,"bbscms");//选择操作的数据库名

设置数据库编码格式
mysqli_set_charset(链接池,"编码格式");
mysqli_set_charset($link,"utf8");

$res = mysqli_query(链接池,操作的sql语句)
$res = mysqli_query($link,$sql);//执行sql语句

$row = mysqli_fetch_assoc($res); //每执行一次得到一条结果集 返回的数据是数组


$_SESSION[]   $_COOKIE[] 临时的会话
$_SESSION['username'] = 数据库值

清除session
unset($_SESSION['username'])


文章管理

文章内容

id title content time

init.php : init :配置 config:配置


select * from `news` limit 偏移量(编号为0对应第一条数据),固定的条数 
select * from `news` limit 0,5 //第一页的数据 1,2,3,4,5
0,5 //第一页 1,2,3,4,5

5,5 //第二页 6,7,8,9,10

10,5 //第三页 11,12,13,14,15

15,5 //第四页 16,17,18

($curpage - 1) * pagesize


得到总条数语句
select count(*) from `news`

一 html文件 设置数据库表单 设置核心配置文件

二   登录页面 连接数据库

 三 首页和新建表news表单 判断是否有用户记录 查询文章内容

 

 四  退出

五 增

六 删

 七 改

自己的小练习:

一 html文件 设置数据库表单 设置核心配置文件

<?php
//核心配置文件
header("Content-Type:text/html;chaset=utf-8");
//(1):链接数据库 (2中链接方式) (1)mysql (2)mysqli
//mysql_connect("主机名","用户名","密码") 链接池
//mysql_connect("127.0.0.1","root","root") or die("链接失败");
$link = mysqli_connect("127.0.0.1","root","root") or die("链接失败");
//mysql_select_db("xiexie");//选择操作的数据库名
mysqli_select_db($link,"xiexie");//选择操作的数据库名

//设置字符集编码格式
//mysql_query("set names utf8");
mysqli_set_charset($link,"utf8");


?>

二   登录页面 连接数据库

<?php

   //表单里面的name值对应上数据里面字段名

   //链接数据库 

   require("x666/init.php");

   session_start();//开启会话

    if(!empty($_POST))

{

$username = $_POST['username'];//获取文本框输入的用户名

$password  = $_POST['password'];//获取文本框输入的密码

//操作sql语句

$sql = "select * from `admin` where `username`='{$username}' and `password`='{$password}'";

//echo ($sql);

//$res = mysql_query($sql); //执行sql语句

$res = mysqli_query($link,$sql);

//$row = mysql_fetch_assoc($res); //每执行一次得到一条结果集 返回的数据是数组

$row = mysqli_fetch_assoc($res); //每执行一次得到一条结果集 返回的数据是数组

//print_r($row);die;

//验证验证码

if($_SESSION['code'] != md5(strtolower($_POST['code'])))

{

   die("验证码不对");

    }else if($row['username']=="" and $row['password']==""){

        die("账号密码不得为空");

    }else 

//验证账号密码

    if($row['username']==$username and $row['password']==$password)

        {

$_SESSION["username"]=$row['username'];

            header("location:http://localhost/11.2/index.php"); //跳转页面

        }else{

            die("请输入正确的账号密码");

        }

}

include("03.html");//视图加载

?>

 三 首页和新建表news表单 判断是否有用户记录 查询文章内容

<?php

  require("x666/init.php"); //加载核心配置文件

  session_start();

  //判断是否有用户记录

  if(empty($_SESSION["username"]))

  {

  

header("location:http://localhost/11.2/11.2.php"); 

  

  }

  $pagesize = 5;//固定的条数

  

  $curpage = !empty($_GET['p'])? $_GET['p']: 1;//得到当前用户点击的值

  

  $offsetpage = ($curpage - 1) * $pagesize; //计算偏移量

  

  

  //查询文章内容

  $sql = "select * from `news` limit {$offsetpage},{$pagesize}";

  $res = mysqli_query($link,$sql);

  $data = array();//声明一个空数组

  while($row = mysqli_fetch_assoc($res))

  {

 $data[] = $row;

   }

 //统计总条数   ceil(总条数 / 固定的条数)  得到总页数

 $sql = "select count(*) as count  from `news`";

 $res = mysqli_query($link,$sql);

 $row = mysqli_fetch_assoc($res);

 //print_r($row);die;

 $pageNum = $row['count'];//得到总条数

 $perpage = ceil($pageNum/$pagesize);//得到总页数

 //echo ($perpage);die;

 $page_str = "";

 $page_str .='<a href="index.php?p=1" title="First Page">&laquo; 首页</a>';

 $page_str .='<a href="index.php?p='.($curpage-1).'" title="Previous Page">&laquo; 上一页</a> ';

 for($i=1;$i<=$perpage;$i++)

 {

  $class = ($i==$curpage)? "number current":"number" ;

  $page_str .='<a href="index.php?p='.$i.'" class=".$class." title="1">'.$i.'</a>';

 }

 $page_str .='<a href="index.php?p='.($curpage+1).'" title="Next Page">下一页 &raquo;</a>';

 $page_str .='<a href="index.php?p='.$perpage.'" title="Last Page">尾页 &raquo;</a>';

 include("header.html");

 include("index.html");

 include("footer.html");

?>

四 退出

<?php

  session_start();

  unset($_SESSION['username']);

  header("location:http://localhost/11.2/11.2.php"); 

?>

五 增

<?php

    require("x666/init.php"); //加载核心配置文件

session_start();

    if(!empty($_POST))

{

$title = $_POST['title'];//获取文本框输入的标题

$content = $_POST['content'];//获取文本框输入的内容

$time = date("y-m-d");//获取当前的日期

//操作数据库

$sql = "insert into `news` values ('','{$title}','{$content}','{$time}')";

//echo ($sql);die;

$res = mysqli_query($link,$sql);

header("location:http://localhost/11.2/index.php"); 

  include("header.html");

  include("add.html");

  include("footer.html");

  

?>

六 删

<?php

  require("x666/init.php"); //加载核心配置文件

  

  if(!empty($_GET))

  {

$id =  $_GET['id']; 

$sql = "delete from `news` where id={$id}";  

//$sql = "delete from `news` where id=".$id;

$res = mysqli_query($link,$sql);

header("location:http://localhost/11.2/index.php"); 

  }

  

 七 改

?>

<?php

  require("x666/init.php"); //加载核心配置文件

  session_start();

  

  

  //从数据库获取内容

   if(!empty($_GET))

   {

  $id = $_GET['id']; 

  $sql = "select * from `news` where id={$id}";

      $res = mysqli_query($link,$sql);

      $row = mysqli_fetch_assoc($res);  

  //print_r($row);die;

   

   }

  

  

  

  //再把修改的内容存到数据库 

  

  if(!empty($_POST))

  {

$id = $_POST['id']; 

$title = $_POST['title']; //修改后的标题

     $content = $_POST['content'];//修改后的内容

     $time = date("y-m-d");

     

$sql = "update `news` set `title`='{$title}',`content`='{$content}',`time`='{$time}' where id={$id}"; 

     //echo ($sql);die; 

$res = mysqli_query($link,$sql);

//$row =mysqli_fetch_assoc($res); 

header("location:http://localhost/11.2/index.php"); 

  }

include("header.html");

  include("update.html");

  include("footer.html");

?>

猜你喜欢

转载自www.cnblogs.com/liuyangya/p/9910248.html