新闻系统(发布,删除,修改)

1、登录表单及验证

<form action="login.php" method="post">
  用户名:<input type="text" name="uname"/>
  <br>
  密 码:<input type="password" name="pwd"/>
  <br>
  <input type="submit" name="sub" value="用户登录"/>
  <input type="submit" name="sub1" value="管理员登录"/>
  <input type="submit" name="sub2" value="注册"/>
</form>

登录验证

<?php
header("content-type:text/html;charset=utf-8");
if(isset($_POST["sub"])){
try {
@$pdo=new PDO("mysql:hostname=localhost;dbname=news","root","123456");//当时出错了
} catch (Exception $e) {
echo "连接失败".$e->getMessage();
}
try {
$name=$_POST['uname'];
$pwd=md5($_POST['pwd']);
//$statm=$pdo->prepare("insert into login(uname,pwd)value('222','222')");
$statm=$pdo->prepare("select * from login where uname=:name and pwd=:pwd");
$statm->bindParam(':name', $name);
$statm->bindParam(':pwd', $pwd);
$statm->execute();
$c=$statm->rowCount();
if ($c>=1){
header('Location:kehu.php');
}else {
echo "<script type='text/javascript'>alert('登录失败!');</script>";
}
} catch (Exception $e) {
echo "登录失败".$e->getMessage();
}
}

注册用户

 1 if(isset($_POST["sub2"])){
 2 try {
 3 @$pdo=new PDO("mysql:hostname=localhost;dbname=news","root","123456");//当时出错了
 4 } catch (Exception $e) {
 5 echo "连接失败".$e->getMessage();
 6 }
 7 try {
 8 $name=$_POST['uname'];
 9 $pwd=md5($_POST['pwd']);
10 $statm=$pdo->prepare("insert into login(uname,pwd)value(:name,:pwd)");
11 $statm->bindParam(':name', $name);
12 $statm->bindParam(':pwd', $pwd);
13 $c=$statm->execute();
14 if ($c){
15 echo "<script type='text/javascript'>alert('注册成功!');</script>";
16 }
17 } catch (Exception $e) {
18 echo "注册失败".$e->getMessage();
19 }
20 }

删除:

if ($_GET['del']){
                try {
                    @$pdo=new PDO("mysql:hostname=localhost;dbname=news","root","123456");//当时出错了
                } catch (Exception $e) {
                    echo "连接失败".$e->getMessage();
                }
                try {
                    $statm4=$pdo->prepare("select * from wz");
                    $statm4->execute();
                    $c=$statm4->fetchAll();
                    foreach ($c as $k=>$v){
                        echo "<li>";
                        echo "<a href='http://192.168.20.200/news/kehu.php?bs=$v[id]'>{$v[biaot]}</a>";
                        echo "<a href='http://192.168.20.200/news/home1.php?del=4&deli=$v[id]'>&nbsp;删除!</a>";
                        echo "</li>";
                    }
                } catch (Exception $e) {
                    echo "查询失败".$e->getMessage();
                }
                if ($_GET['deli']){
                    try {
                        $id=$_GET['deli'];
                        $statm5=$pdo->prepare("delete from wz where id=:i");
                        $statm5->bindParam(':i', $id);
                        $c=$statm5->execute();
                    } catch (Exception $e) {
                        echo "失败".$e->getMessage();
                    }
                }
            }

发布:

if ($_GET['add']){
                echo "<form action='home1.php' method='post'><br>";
                echo "标题:<input name='title' type='text' /><br><br>";
                echo "<textarea name='content' style='300px;height:300px;border:1px solid black'></textarea><br><br>";
                echo "<input name='sub' type='submit' />";
                echo "</form>";
            }
            if(isset($_POST['sub'])){
                try {
                    @$pdo=new PDO("mysql:hostname=localhost;dbname=news","root","123456");//当时出错了
                } catch (Exception $e) {
                    echo "连接失败".$e->getMessage();
                }
                try {
                    $title=$_POST['title'];
                    $content=$_POST['content'];
                    $statm1=$pdo->prepare("insert into wz(biaot,wcontent)value(:b,:c)");
                    $statm1->bindParam(':b', $title);
                    $statm1->bindParam(':c', $content);
                    $c=$statm1->execute();
                } catch (Exception $e) {
                    echo "失败".$e->getMessage();
                }
            }

 前端MVC思想。

  controller.php控制数据库得到数据。

if($flag=='point_newlist'||$flag=='point_content'||$flag=='point_pro'){
    //遍历分類
    $statm3=$pdo->prepare("select * from fenlei");
    $statm3->execute();
    while ($info=$statm3->fetch(PDO::FETCH_NUM)){
        $list3[]=$info;
    }
}

在客户界面遍历数据。

<?php 
                for ($i=0;$i<count($list1);$i++){
                    echo "<li><a href='product.php?id={$list1[$i][0]}&flag=point_pro'><img src='{$list1[$i][2]}'/><p>{$list1[$i][1]}</p></a></li>";
                }
                ?>

controller.php中的代码。

<?php
header("content-type:text/html;charset=utf-8");
include 'sql.php';
if(isset($_GET['flag'])){
    $flag=$_GET['flag'];
}else{
    $flag="";
}
//产品详情
if ($flag=='point_pro'){
    $cp_id=$_GET[id];
    $statm=$pdo->prepare("select * from cp_xq where id=:d");
    $statm->bindParam(':d', $cp_id);
    $statm->execute();
    while ($info=$statm->fetch(PDO::FETCH_NUM)){
        $list2[]=$info;
    }
}

if ($flag=='point_newlist'){
    //遍历新闻
    $statm=$pdo->prepare("select * from wz");
    $statm->execute();
    while ($info=$statm->fetch(PDO::FETCH_NUM)){
        $list[]=$info;
    }
    //产品列表查询
    $statm1=$pdo->prepare("select * from cp_list");
    $statm1->execute();
    while ($info=$statm1->fetch(PDO::FETCH_NUM)){
        $list1[]=$info;
    }
}
if($flag=='point_newlist'||$flag=='point_content'||$flag=='point_pro'){
    //遍历分類
    $statm3=$pdo->prepare("select * from fenlei");
    $statm3->execute();
    while ($info=$statm3->fetch(PDO::FETCH_NUM)){
        $list3[]=$info;
    }
}
if($flag='point_content'){
    //遍历分類內容
    $id=$_GET[id];
    $statm4=$pdo->prepare("select * from cp_list where fenlei_id=:d");
    $statm4->bindParam(':d', $id);
    $statm4->execute();
    while ($info=$statm4->fetch(PDO::FETCH_NUM)){
        $list4[]=$info;
    }
}
View Code
原文地址:https://www.cnblogs.com/h-g-f-s123/p/5686382.html