zoukankan      html  css  js  c++  java
  • PHP MySQLi 增删改查

         最近几天,我们一直在学习利用MySQLi访问数据库并对其中的数据进行操作。今天给大家展现一个完整的例子,我们来制作一个新闻发布系统,利用MySQLi来操作数据库,实现对新闻的添加、修改、删除、查询等基本功能。(以下代码分为前端显示和后台php处理代码,中间用空行隔开,注意区分)

    1、登陆页面:这由本公司内部人员通过员工号和身份证号才能登陆,代码如下:

    //前端显示部分

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>

    <body>
    <h2>管理员登陆</h2>
    <form action="dlyz.php" method="post">
    <div>用户名:<input type="text" name="user" value="请输入您的工号" /></div>
    <br />
    <div>密&nbsp;&nbsp;码:<input type="password" name="psd" /></div>
    <br />
    <input type="submit" value="登录" />
    <input type="submit" value="注册新用户" formaction="zhuc.php"/>
    </form>
    </body>
    </html>

    //php代码对提交登陆的信息进行处理

    <?php

    $user = $_POST["user"];
    $psd = $_POST["psd"];


    //造对象
    $db = new MySQLi("localhost","root","","newssystem");

    //判断是否出错
    !mysqli_connect_error() or die("连接失败!!");

    //写sql语句

    $sql = "select psd from yonghu where user='{$user}'";


    //执行SQL语句

    $result = $db-> query($sql);
    $v = $result->fetch_row();
    if($psd==$v[0])
    {
    header("location:fabuxinwen.php");
    }
    else
    {
    echo"您输入的用户名或密码不正确,请重新输入!!";
    }

    2、注册页面:公司/报社来了新员工,只有注册后才能登陆

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>

    <body>
    <h2>欢迎注册</h2>
    <body>

    <form action="zhucyz.php"method="post">
    <div>用户名:<input type="text" name="user" value="请输入您的工号"/></div><br />

    <div>密&nbsp;&nbsp;码:<input type="password" name="psd" /></div><br />

    <input type="submit" value="提交" />
    </form>

    </body>
    </html>

    <?php
    $user = $_POST["user"];
    $psd = $_POST["psd"];


    //造对象
    $db = new MySQLi("localhost","root","","newssystem");

    //判断是否出错
    !mysqli_connect_error() or die("连接失败!!");

    //写sql语句

    $sql = "insert into yonghu values('{$user}','{$psd}')";

    //执行SQL语句

    $result = $db-> query($sql);

    if($result)
    {
    header("location:dl.php");
    }
    else
    {
    echo"很抱歉,注册失败!!";
    }

    3、登陆进去以后,是发布新闻页面,点击提交按钮进行提交保存到已经建立好的数据库,点击查看按钮进行查看确认

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>

    <body>
    <div style="100%; text-align:center" >
    <h2>发布新闻</h2>
    <form method="post">
    <input type="hidden" name="newsid"/>
    <table style="margin:0 auto; text-align:left" >
    <tr>
    <td >标题:</td><td><input type="text" style="400px" name="title" /></td>
    </tr>
    <tr>
    <td >作者:
    </td><td><input type="text" style="400px" name="Author" /></td>
    </tr>
    <tr>
    <td >来源:</td><td><input type="text" style="400px" name="source"/></td>
    </tr>
    <tr>
    <td >内容:</td>
    <td><textarea cols="auto" rows="auto" style="400px; height:400px" name="content"></textarea></td>
    </tr>

    </table><br />
    <?php
    $time = date('y-m-d h:i:s');

    echo "<input type="hidden" name="time" value="{$time}"/>";
    ?>

    <input type="submit" value="提交" formaction="tijiao.php"/>

    <input type="submit" value="查看" formaction="chakan.php"/>
    </form>
    </div>
    </body>
    </html>

    <?php
    $title = $_POST["title"];
    $Author = $_POST["Author"];
    $source = $_POST["source"];
    $content = $_POST["content"];

    //造对象
    $db = new MySQLi("localhost","root","","newssystem");

    //判断是否出错
    !mysqli_connect_error() or die("添加失败!!");

    //写sql语句

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


    //执行SQL语句

    $result = $db-> query($sql);

    if($result)
    {
    header("location:fabuxinwen.php");
    }
    else
    {
    echo"很抱歉,添加失败!!";
    }

    4、查看页面

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>

    <body>
    <table width="70%" border="1px" cellpadding="0" cellspacing="0" style="text-align:center">
    <tr>
    <td>编号</td>
    <td>标题</td>
    <td>作者</td>
    <td>来源</td>
    <td>日期</td>
    <td>删除</td>
    <td>修改</td>

    </tr>

    <?php
    $db=new mysqli("localhost","root","","newssystem");
    !mysqli_connect_error() or die("连接错误");
    $sql="select * from news";
    $result=$db->query($sql);
    while($attr=$result->fetch_row())
    {
    echo "
    <tr>
    <td>{$attr[0]}</td>
    <td>{$attr[1]}</td>
    <td>{$attr[2]}</td>
    <td>{$attr[3]}</td>
    <td>{$attr[5]}</td>
    <td><a onclick=" return confirm('确定删除')" href='scchuli.php?newsid={$attr[0]}'>删除</a></td>
    <td><a href='xiugai.php?newsid={$attr[0]}'>修改</a></td>
    </tr>
    ";
    }

    ?>
    </table>
    </body>
    </html>

    5、在查看页面可以进行相关内容的修改和删除

    //修改:在点击修改按钮后,会跳转到修改页面,此时会显示出之前已经发布的内容、标题等相关内容

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>

    <body>
    <?php
    $id = $_GET["newsid"];

    $db=new mysqli("localhost","root","","newssystem");
    !mysqli_connect_error() or die("连接错误");
    $sql="select * from news where newsid='{$id}'";
    $result=$db->query($sql);
    $attr=$result->fetch_row();
    ?>
    <div style="100%; text-align:center" >
    <h2>修改新闻</h2>
    <form action="xgchuli.php" method="post">
    <input type="hidden" name="newsid" <?php echo "value='{$attr[0]}'";?>/>
    <table style="margin:0 auto; text-align:left" >
    <tr>
    <td >标题:</td><td><input type="text" style="400px" name="title" <?php echo "value='{$attr[1]}'";?>/></td>
    </tr>
    <tr>
    <td >作者:
    </td><td><input type="text" style="400px" name="Author" <?php echo "value='{$attr[2]}'";?>/>
    </td>
    </tr>
    <tr>
    <td >来源:</td><td><input type="text" style="400px" name="source" <?php echo "value='{$attr[3]}'";?>/>
    </td>
    </tr>
    <tr>
    <td >内容:</td>
    <td><textarea cols="auto" rows="auto" style="400px; height:400px" name="content"><?php echo "{$attr[4]}";?>
    </textarea></td>
    </tr>

    </table><br />
    <?php
    $time = date('y-m-d h:i:s');

    echo "<input type="hidden" name="time" value="{$time}"/>";
    ?>

    <div><a href="chakan.php"><input type="button" title="查看" value="查看" /></a><input type="submit" title="修改" value="修改"/>
    </div>

    </form>

    </body>
    </html>

    <?php

    $id=$_POST["newsid"];
    $title=$_POST["title"];
    $Author=$_POST["Author"];
    $source=$_POST["source"];
    $content=$_POST["content"];
    $time=$_POST["time"];

    $db = new MySQLi("localhost","root","","newssystem");

    !mysqli_connect_error() or die("连接失败");

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

    $result=$db->query($sql);
    if ($result)
    {
    header("location:chakan.php");
    }
    else
    {
    echo "修改失败";
    }

    //删除:如果要删除一条新闻,在点击删除按钮之后,不会跳转到任何前台显示页面,只会在后台通过php代码进行相关处理

    <?php
    $id=$_GET["newsid"];

    $db=new mysqli("localhost","root","","newssystem");

    !mysqli_connect_error() or die("连接失败");

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

    $result=$db->query($sql);

    if ($result)
    {
    header("location:chakan.php");
    }
    else
    {
    echo "删除失败";
    }

    6、修改完成之后,点击页面下方的修改按钮进行提交,在这里也不会有相关的前端显示页面,只是在后台利用相关php代码进行相应处理,成功后返回发布新闻页面

    <?php
    $title = $_POST["title"];
    $Author = $_POST["Author"];
    $source = $_POST["source"];
    $content = $_POST["content"];

    //造对象
    $db = new MySQLi("localhost","root","","newssystem");

    //判断是否出错
    !mysqli_connect_error() or die("添加失败!!");

    //写sql语句

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


    //执行SQL语句

    $result = $db-> query($sql);

    if($result)
    {
    header("location:fabuxinwen.php");
    }
    else
    {
    echo"很抱歉,添加失败!!";
    }

  • 相关阅读:
    http基础知识摘录
    数据库基础常用知识
    2018第18周总结
    pycharm里html注释是{# #}而不是<!-- -->?
    paramiko错误信息:Paramiko error: size mismatch in put
    jmeter的Classpath即类或者jar包的搜索路径设置
    scp、paramiko、rsync复制文件的区别
    maven打包插件maven-shade-plugin简单介绍
    sqlalchemy多表联合查询的左连接、右连接等使用
    如何查看sqlalchemy执行的原始sql语句?
  • 原文地址:https://www.cnblogs.com/sdzbxfcy/p/5596936.html
Copyright © 2011-2022 走看看