PHP MySQLi 增删改查(上)

PHP MySQLi 增删改查(上)

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代码对注册信息进行处理

<?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="width: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="width:400px" name="title" /></td>
                </tr>
                <tr>
                    <td>作者:
                    </td>
                    <td><input type="text" style="width:400px" name="Author" /></td>
                </tr>
                <tr>
                    <td>来源:</td>
                    <td><input type="text" style="width:400px" name="source" /></td>
                </tr>
                <tr>
                    <td>内容:</td>
                    <td><textarea cols="auto" rows="auto" style="width: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

<?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 "很抱歉,添加失败!!";
}
猜你喜欢