bootstrap框架+ajax+php+mysql实现发布与删除功能

bootstrap框架+ajax+php+mysql实现发布与删除功能

主页面learnReportindex.php

<?php
//从数据库获取数据
error_reporting(0);
header("Content-type:text/html;charset=UTF-8");
$con = mysqli_connect('localhost','root','root','learnreport');
mysqli_set_charset($con,'utf8');
if(!$con){
    die('Could not connect:' . mysql_error($con));
}
$sql = "select * from phplearn";
$result = mysqli_query($con,$sql);
$rows = array();
while($row = mysqli_fetch_assoc($result)){
    $rows[] = $row;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <script src="js/jquery-3.5.1.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="css/learnReport.css">
    <script type="text/javascript">
        //把数据传递到php插入数据库
        function login() {
            $.ajax({
                type:'post',
                dataType:'json',
                url:'learnReport.php',
                data:$('#form1').serialize(),
            })
            alert("发布成功");
            location.reload();
        }
        //把传递到php文件中执行删除语句
        function del($id){
            var id=$id;
            if (id){
                $.get("sc.php",{id:id},function (data) {                                       /*get获取数据*/
                    if (data=201){
                        alert("删除成功");
                        window.location.href=window.location.href;                                     /* 刷新*/
                    }else {
                        alert("失败");
                    }
                });
            }
        }
    </script>
</head>
<body>
<div class="container">
    <div class="h-center">
        <h1>学习汇报</h1>
    </div>
    <form class="form-horizontal" id="form1" method="post" action="learnReport.php">
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">标题</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="inputEmail3" name="mytitle" placeholder="title">
            </div>
        </div>
        <div class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label">内容</label>
            <div class="col-sm-10">
                <textarea class="form-control" id="inputPassword3" rows="6" name="mycontent" placeholder="content"></textarea>
            </div>
        </div>
        <div class="form-group col-sm-10">
            <button type="button" onclick="login()" class="btn btn-success" style="width: 800px; margin-left: 170px">发布</button>
        </div>
    </form>
    <table class="table table-bordered table-hover" id='tabletest'>
        <thead style="text-align: center">
        <tr>
            <th>标题</th>
            <th class="col-lg-6">内容</th>
            <th>时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <?php foreach($rows as $key => $v) {?>
<!--            把获取到的数据插入到表格中-->
            <tr>
<!--                <td>--><?php //echo $v['id'];?><!--</td>-->
                <td><?php echo $v['title'];?></td>
                <td><?php echo $v['content'];?></td>
                <td><?php echo $v['time'];?></td>
                <td style="color: red;"><a href="" onclick="del( <?php echo $v["id"]?> );">删除</a></td>
            </tr>
        <?php };?>
        </tbody>
    </table>
</div>
</body>
</html>

css样式learnReport.css

@media screen and (max-width: 1280px) {
    .container {
        width: 1000px;
    }
}
body{
    background: #C6E746;
}
.nui-textbox{
    background-color: #C6E746;
    border: 0px;
    font-size:25px
}
.form-horizontal{
    margin-top: 50px;
}
.container{
    margin: auto;
    text-align: center;
}
.btn btn-success{
    width: 1220px;

}
.table-bordered th,
.table-bordered td {
    border: 1px solid #000 !important;
    text-align: center;
}

数据插入mysql

learnReport.php

<?php
//接受前端传来的值插入数据库
header('Content-Type: text/html; charset=utf-8');
$servername = "localhost";
$username = "root";
$password = "root";
$database = "learnreport";
$mytitle = $_POST['mytitle'];
$mycontent = $_POST['mycontent'];
date_default_timezone_set('PRC');
$time = date('Y-m-d H:i:s', time());
// 创建连接
$conn = mysqli_connect($servername, $username, $password,$database);

// 检测连接
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "连接成功";

$sql = "INSERT INTO phplearn (id,title,content,time)
VALUES ('', '$mytitle', '$mycontent',now())";
if (mysqli_query($conn, $sql)) {
    echo "新记录插入成功";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
?>

删除数据

scphp

<?php
//接受前端传来的id,执行删除语句
header("Content-type:text/html;charset=UTF-8");
$link =mysql_connect("localhost","root","root");
mysql_select_db("learnReport", $link);
$del_id=$_GET["id"];
$exec="delete from phplearn where id=$del_id";
mysql_query($exec, $link);
echo "删除成功!";
mysql_close($link);

?>

mysql创建语句

create table phplearn(
     title varchar(25) not null,
     content varchar(100) not null,
     time date,
    id tinyint primary key auto_increment
);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值