var express = require('express');
var mysql = require('mysql');
var urll = require('url');
var formidable = require('formidable');
var app = express();
app.use(express.static('node_modules'));
app.set('view engine','ejs');
app.set('views','./views');
//引入cookie模块
var cookie = require('cookie-parser');
app.use(cookie());
//配置数据库
var connection = mysql.createConnection({
host:'localhost',
user:'root',
password:'123',
database:'h5-56'
});
//连接数据库
connection.connect();
app.get('/index',function(req,res){
connection.query('select * from cheshi',function(error,results,fields){
res.render('index',{data:results});
res.end();
})
})
//添加操作
app.get('/add',function(req,res){
res.render('add');
res.end();
});
//执行添加操作
app.post('/doadd',function(req,res){
var form = new formidable.IncomingForm();
form.parse(req,function(err,fieles,fields){
// console.log(fieles);
// res.end();
if(!err){
var name = fieles.name;
var age = fieles.age;
var classid = fieles.classid;
/* console.log(name+age+classid);
res.end(); */
var sql = 'insert into cheshi values(null,?,?,?)';
var inserts = [name,age,classid];
var sql = mysql.format(sql,inserts);
connection.query(sql,function(error,results,fields){
// console.log(results);
if(results.affectedRows>0){
res.redirect('/index');
res.end();
}
})
}
})
})
//删除操作
app.get('/del',function(req,res){
var url = req.url;
var id = urll.parse(url,true).query.id;
// console.log(id);
connection.query('delete from cheshi where id='+id,function(error,results,fields){
//console.log(results);
if(results.affectedRows>0){
res.redirect('/index');
res.end();
}
})
})
//ajax删除
app.get('/delete',function(req,res){
var url = req.url;
var id = urll.parse(url,true).query.id;
// console.log(id);
var sql = 'delete from cheshi where id=?';
var sql = mysql.format(sql,id);
connection.query(sql,function(error,results,fields){
//console.log(results);
if(results.affectedRows>0){
res.json({'y':1});
res.end();
}else{
res.json({'y':0});
res.end();
}
})
})
app.get('/edit',function(req,res){
var url = req.url;
var id = urll.parse(url,true).query.id;
// console.log(id);
connection.query('select * from cheshi where id='+id,function(error,results,fields){
// console.log(results);
res.render('edit',{data1:results});
res.end();
})
})
app.post('/doedit',function(req,res){
var form = formidable.IncomingForm();
form.parse(req,function(err,fieles,fields){
// console.log(fieles);
var id = fieles.id;
var name = fieles.name;
var age = fieles.age;
var classid = fieles.classid;
/* console.log(id+name+age+classid);
res.end(); */
var sql = "update cheshi set name=?,age=?,classid=? where id=?";
var upates = [name,age,classid,id];
// console.log(upates);
var sql = mysql.format(sql,upates);
connection.query(sql,function(error,results,fields){
// console.log(results);
/* res.end(); */
if(results.affectedRows>0){
res.redirect('/index');
res.end();
}
})
})
})
app.get('/login',function(req,res){
res.render('login');
res.end();
})
app.post('/dologin',function(req,res){
var form = new formidable.IncomingForm();
form.parse(req,function(err,fieles,fields){
// console.log(fieles);
var user = fieles.user;
var pass = fieles.pass;
var sql = "select * from stu where user='"+user+"'";
connection.query(sql,function(error,results,fields){
// console.log(results);
if(results.length>0){
if(pass == results[0].pass){
res.write(`
<h1>successful</h1>
<script>
setTimeout(function(){
location.href = '/index';
},3000);
</script>
`)
}else{
console.log('账号或者密码错误2阶段');
res.end();
}
}else{
console.log('账户或密码错误1阶段');
res.end();
}
})
})
})
app.listen(8090);
```------------
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="/bootstrap/dist/css/bootstrap.min.css">
<script src="/jquery/dist/jquery.js"></script>
</head>
<body>
<a href="#" class="btn btn-info">用户管理</a>
<a href="/login" class="btn btn-info">用户登录</a>
<a href="/add" class="btn btn-info">用户添加</a>
<table class="table table-striped">
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>班级</th>
<th>操作</th>
</tr>
<% for(var i=0;i<data.length;i++){ %>
<tr>
<td><%=data[i].id%></td>
<td><%=data[i].name%></td>
<td><%=data[i].age%></td>
<td><%=data[i].classid%></td>
<td>
<a href="/edit?id=<%=data[i].id%>" class="btn btn-info">编辑</a>
<a href="/del?id=<%=data[i].id%>" class="btn btn-info">删除</a>
<a href="javascript:void(0);" class="btn btn-info a">ajax删除</a>
</td>
</tr>
<%}%>
</table>
<script>
// alert($);
$('.a').click(function(){
var id = $(this).parents('tr').find('td:first').html();
// console.log(id);
var qd = confirm('是否要进行删除操作');
var tr = $(this).parents('tr');
if(qd){
// console.log(111);
$.get('/delete',{id:id},function(data){
if(data.y == 1){
alert('删除成功');
tr.remove();
}else{
alert('删除失败');
}
})
}
})
</script>
</body>
</html>
2021-05-21
最新推荐文章于 2022-02-24 13:31:38 发布