- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试更新我的数据库以显示新分数,但它总是返回为空。我知道该元素已创建,当我检查时它一直说它为空。我已使用控制台日志看到我正在调用该函数,但无法进一步调试?
createDB.sql
DROP DATABASE IF EXISTS THUNTDB;
CREATE DATABASE THUNTDB;
use THUNTDB;
CREATE TABLE USER (
USER_ID int NOT NULL AUTO_INCREMENT,
USER_EMAIL varchar(255) UNIQUE NOT NULL,
USER_PASS varchar(60) NOT NULL,
USER_LOW int NOT NULL,
PRIMARY KEY (USER_ID)
);
index.html
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style>
#loginBtn, #logoutBtn, #registerBtn, #resetLowBtn
{
display: none;
}
a, a:hover
{
color: white;
}
.box
{
height: 50px;
background-color: black;
border: white 2px solid;
}
.prize
{
background-color: green !important;
}
.flipped
{
background-color: red !important;
}
.selected
{
color: blue;
}
</style>
</head>
<body>
<div>
<div class="jumbotron jumbotron-fluid mb-0">
<div class="container">
<h1 class="display-4">THUNT</h1>
<p class="lead">The coolest thunt on the web!</p>
</div>
</div>
<div class="">
<ul class="nav justify-content-start border p-2 bg-dark text-light">
<li class="nav-item">
<a href="#" id='resetLowBtn' class='nav-link'>Reset Low Score</a>
</li>
<li class="nav-item">
<a href="#" id='loginBtn' class='nav-link'>Login</a>
</li>
<li class="nav-item">
<a href="#" id='registerBtn' class='nav-link'>Register</a>
</li>
<li class="nav-item">
<a href="#" id='logoutBtn' class='nav-link'>Logout</a>
</li>
<li class="nav-item">
<span id="msg"></span>
</li>
</ul>
<div class="container">
<!--boxes-->
<div id='game'>
<div id='boxes' class='row'>
</div>
<p>Guesses: <span id='guesses'>0</span> Low Score: <span id='lowScore'>N/A</span></p>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<!--put your modal dialogs here...-->
<div id='confirm' class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header bg-dark text-light">
<h5 class="modal-title">Play Again?</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Would you like to play again?</p>
</div>
<div class="modal-footer">
<button id='yes' type="button" class="btn btn-primary">Yes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">No</button>
</div>
</div>
</div>
</div>
<div id='loginDlg' class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header bg-dark text-light">
<h5 class="modal-title">Login</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<h4>Login</h4>
<form class="border border-primary p-3">
<div class="form-group">
<input type="text" class="form-control" id="email" name="email" placeholder="Email">
</div>
<div class="form-group">
<input type="password" class="form-control" id="password" name="password" placeholder="Password">
</div>
</form>
</div>
<div class="modal-footer">
<button id='confirmLoginBtn' type="button" class="btn btn-primary">Login</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
<div id='registerDlg' class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header bg-dark text-light">
<h5 class="modal-title">Register</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<h4>Register</h4>
<form class="border border-primary p-3">
<div class="form-group">
<input type="text" class="form-control" id="regEmail" name="regEmail" placeholder="Email">
</div>
<div class="form-group">
<input type="password" class="form-control" id="regPassword" name="regPassword" placeholder="Password">
</div>
</form>
</div>
<div class="modal-footer">
<button id='confirmRegisterBtn' type="button" class="btn btn-primary">Register</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
$(document).ready(function()
{
// model ....
let model = {};
let Gmodel= {};
function initializeModel()
{
sendRequest("whoIsLoggedIn", function(){});
let boxCount = 10;
Gmodel.guesses = 0;
Gmodel.boxes = [];
let prize = Math.floor(Math.random() * boxCount);
for (var i = 0; i < boxCount; i++)
{
Gmodel.boxes.push({flipped: false, prize: false});
}
Gmodel.boxes[prize].prize = true;
Gmodel.gameOver = false;
}
// view...
function updateView()
{
console.log(model);
console.log(Gmodel);
if (model.result == undefined)
{
$("#msg").text(model.error);
$("#logoutBtn").hide();
$("#loginBtn").show();
$("#registerBtn").show();
$("#resetLowBtn").hide();
$("#game").hide();
}
else
{
$("#logoutBtn").show();
$("#resetLowBtn").show();
$("#loginBtn").hide();
$("#registerBtn").hide();
$("#msg").text("Hello " + model.result.email);
$("#game").show();
$("#boxes").empty();
for (var i in Gmodel.boxes)
{
let item = $("<div class='col-6 box'></div>");
if (Gmodel.boxes[i].flipped && Gmodel.boxes[i].prize)
{
item.addClass("prize");
}
else if (Gmodel.boxes[i].flipped && !Gmodel.boxes[i].prize)
{
item.addClass("flipped");
}
item.attr("id", i);
$("#boxes").append(item);
}
$("#guesses").text(Gmodel.guesses);
$("#lowScore").text(model.result.low);
}
}
// controller ...
function sendRequest(url, callback)
{
var jqxhr = $.get(url);
jqxhr.done(function(json)
{
model = json;
updateView();
if (callback !== undefined)
callback();
})
jqxhr.fail(function(json)
{
let error = JSON.stringify(json);
model.error = error;
updateView();
})
}
$(document).on("click",".box",function()
{
if (Gmodel.gameOver == true)
{
return;
}
let i = $(this).attr("id");
if (!Gmodel.boxes[i].flipped)
{
Gmodel.guesses = Gmodel.guesses+1;
Gmodel.boxes[i].flipped = true;
if(Gmodel.boxes[i].prize)
{
Gmodel.gameOver = true;
$('#confirm').modal('show');
}
else
{
updateView();
}
}
});
$('#yes').click(function()
{
$('#confirm').modal('hide');
sendRequest("updateLow?low=" + Gmodel.guesses, function(){});
sendRequest("whoIsLoggedIn", function(){});
initializeModel();
updateView();
});
$('#loginBtn').click(function()
{
$('#password').val('');
$('#loginDlg').modal('show');
});
$('#registerBtn').click(function()
{
$('#regEmail').val('');
$('#regPassword').val('');
$('#registerDlg').modal('show');
});
$('#logoutBtn').click(function()
{
sendRequest("logout", function()
{
$("#boxes").empty();
initializeModel();
updateView();
});
});
$('#confirmLoginBtn').click(function()
{
let email = $('#email').val().trim();
let password = $('#password').val().trim();
sendRequest("login?email="+email+"&password="+password, function ()
{
// if (model.error == undefined)
// sendRequest("listSongs");
});
$('#loginDlg').modal('hide');
});
$('#confirmRegisterBtn').click(function()
{
let email = $('#regEmail').val().trim();
let password = $('#regPassword').val().trim();
sendRequest("register?email="+email+"&password="+password, function ()
{
// if (model.error == undefined)
// sendRequest("listSongs");
});
$('#registerDlg').modal('hide');
});
// run...
initializeModel();
updateView();
});
</script>
</body>
服务器.js
const express = require('express');
const app = express();
// added so we can serve index.html...
var http = require('http');
var fs = require('fs');
var mysql = require('mysql');
// install first using npm install bcrypt
const bcrypt = require('bcrypt');
const conInfo =
{
host: process.env.IP,
user: process.env.C9_USER,
password: "",
database: "THUNTDB"
};
var session = require('express-session');
app.use(session({ secret: 'happy jungle',
resave: false,
saveUninitialized: false,
cookie: { maxAge: 600000 }}))
app.all('/', serveIndex);
app.all('/whoIsLoggedIn', whoIsLoggedIn);
app.all('/register', register);
app.all('/login', login);
app.all('/logout', logout);
app.all('/updateLow', updateLow);
app.listen(process.env.PORT, process.env.IP, startHandler())
function startHandler()
{
console.log('Server listening on port ' + process.env.PORT)
}
function updateLow(req, res)
{
var con = mysql.createConnection(conInfo);
console.log("@updateLow");
let sql = `UPDATE USER
SET USER_LOW = ?
WHERE USER_ID = ?`;
let data = [req.query.low, req.session.user.result.id];
// execute the UPDATE statement
if(req.query.low < req.session.user.result.low || isNaN(req.session.user.result.low)){
con.query(sql, data, (error, results, fields) => {
if (error){
return console.error(error.message);
}
console.log('Rows affected:', results.affectedRows);
});
}
}
function whoIsLoggedIn(req, res)
{
if (req.session.user == undefined)
writeResult(req, res, {'error' : 'Nobody is logged in.'});
else
writeResult(req, res, req.session.user);
}
function register(req, res)
{
if (req.query.email == undefined || !validateEmail(req.query.email))
{
writeResult(req, res, {'error' : "Please specify a valid email"});
return;
}
if (req.query.password == undefined || !validatePassword(req.query.password))
{
writeResult(req, res, {'error' : "Password must have a minimum of eight characters, at least one letter and one number"});
return;
}
var con = mysql.createConnection(conInfo);
con.connect(function(err)
{
if (err)
writeResult(req, res, {'error' : err});
else
{
// bcrypt uses random salt is effective for fighting
// rainbow tables, and the cost factor slows down the
// algorithm which neutralizes brute force attacks ...
let hash = bcrypt.hashSync(req.query.password, 12);
con.query("INSERT INTO USER (USER_EMAIL, USER_PASS, USER_LOW) VALUES (?, ?,?)", [req.query.email, hash,10], function (err, result, fields)
{
if (err)
{
if (err.code == "ER_DUP_ENTRY")
err = "User account already exists.";
writeResult(req, res, {'error' : err});
}
else
{
con.query("SELECT * FROM USER WHERE USER_EMAIL = ?", [req.query.email], function (err, result, fields)
{
if (err)
writeResult(req, res, {'error' : err});
else
{
req.session.user = {'result' : {'id': result[0].USER_ID, 'email': result[0].USER_EMAIL, 'low': result[0].USER_LOW}};
writeResult(req, res, req.session.user);
}
});
}
});
}
});
}
function login(req, res)
{
if (req.query.email == undefined)
{
writeResult(req, res, {'error' : "Email is required"});
return;
}
if (req.query.password == undefined)
{
writeResult(req, res, {'error' : "Password is required"});
return;
}
var con = mysql.createConnection(conInfo);
con.connect(function(err)
{
if (err)
writeResult(req, res, {'error' : err});
else
{
con.query("SELECT * FROM USER WHERE USER_EMAIL = ?", [req.query.email], function (err, result, fields)
{
if (err)
writeResult(req, res, {'error' : err});
else
{
if(result.length == 1 && bcrypt.compareSync(req.query.password, result[0].USER_PASS))
{
req.session.user = {'result' : {'id': result[0].USER_ID, 'email': result[0].USER_EMAIL, 'low': result[0].USER_LOW}};
writeResult(req, res, req.session.user);
}
else
{
writeResult(req, res, {'error': "Invalid email/password"});
}
}
});
}
});
}
function logout(req, res)
{
req.session.user = undefined;
writeResult(req, res, {'error' : 'Nobody is logged in.'});
}
function writeResult(req, res, obj)
{
res.writeHead(200, {'Content-Type': 'application/json'});
res.write(JSON.stringify(obj));
res.end('');
}
function validateEmail(email)
{
if (email == undefined)
{
return false;
}
else
{
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}
}
function validatePassword(pass)
{
if (pass == undefined)
{
return false;
}
else
{
var re = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/;
return re.test(pass);
}
}
function serveIndex(req, res)
{
res.writeHead(200, {'Content-Type': 'text/html'});
var index = fs.readFileSync('index.html');
res.end(index);
}
***我复制的代码中缺少一个 script 标记,位于 jquery 上方、div 之后。它给文本框带来了问题,或者更确切地说我是。
我想弄清楚为什么我在选择再次玩后似乎无法更新低分。如果我注销,它将在我重新登录时更新,但在选择再次玩后不会更新。
最佳答案
我明白了。我需要添加另一个 SQL 语句来重新连接到更新后的用户。
function updateLow(req, res)
{
var con = mysql.createConnection(conInfo);
console.log("@updateLow");
let sql = `UPDATE USER
SET USER_LOW = ?
WHERE USER_ID = ?`;
let data = [req.query.low, req.session.user.result.id];
if(req.query.low < req.session.user.result.low || isNaN(req.session.user.result.low)){
con.query(sql, data, (error, results, fields) => {
if (error){
return console.error(error.message);
}
else
{
con.query("SELECT * FROM USER WHERE USER_EMAIL = ?", [req.session.user.result.email], function (err, result, fields)
{
if (err)
writeResult(req, res, {'error' : err});
else
{
req.session.user = {'result' : {'id': result[0].USER_ID, 'email': result[0].USER_EMAIL, 'low': result[0].USER_LOW}};
writeResult(req, res, req.session.user);
}
});
}
console.log('Rows affected:', results.affectedRows);
});
}
}
关于mysql - 为什么选择"is"再次播放后无法更新我的 html 模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55874046/
是否有某种方法可以使用 JPA 或 Hibernate Crtiteria API 来表示这种 SQL?或者我应该将其作为 native 执行吗? SELECT A.X FROM (SELECT X,
在查询中, select id,name,feature,marks from (....) 我想删除其 id 在另一个 select 语句中存在的那些。 从 (...) 中选择 id 我是 sql
我想响应用户在 select 元素中选择一个项目。然而这个 jQuery: $('#platypusDropDown').select(function () { alert('You sel
这个问题在这里已经有了答案: SQL select only rows with max value on a column [duplicate] (27 个回答) 关闭8年前。 我正在学习 SQL
This question already has answers here: “Notice: Undefined variable”, “Notice: Undefined index”, and
我在 php 脚本中调用 SQL。有时“DE”中没有值,如果是这种情况我想从“EN”中获取值 应该是这样的,但不是这样的 IF (EXISTS (SELECT epf_application_deta
这可能是一个奇怪的问题,但不知道如何研究它。执行以下查询时: SELECT Foo.col1, Foo.col2, Foo.col3 FROM Foo INNER JOIN Bar ON
如何在使用 Camera.DestinationType.FILE_URI. 时在 phonegap camera API 中同时选择或拾取多个图像我能够一次只选择一张图像。我可以使用 this 在
这是一个纯粹的学术问题。这两个陈述实际上是否相同? IF EXISTS (SELECT TOP 1 1 FROM Table1) SELECT 1 ELSE SELECT 0 相对 IF EXIS
我使用 JSoup 来解析 HTML 响应。我有多个 Div 标签。我必须根据 ID 选择 Div 标签。 我的伪代码是这样的 Document divTag = Jsoup.connect(link
我正在处理一个具有多个选择框的表单。当用户从 selectbox1 中选择一个选项时,我需要 selectbox2 active 的另一个值。同样,当他选择 selectbox2 的另一个值时,我需要
Acme Inc. Christa Woods Charlotte Freeman Jeffrey Walton Ella Hubbard Se
我有一个login.html其中form定义如下: First Initial Plus Last Name : 我的do_authorize如下: "; pri
$.get( 'http://www.ufilme.ro/api/load/maron_online/470', function(data
我有一个下拉列表“磅”、“克”、“千克”和“盎司”。我想要这样一种情况,当我选择 gram 来执行一个函数时,当我在输入字段中输入一个值时,当我选择 pounds 时,我想要另一个函数来执行时我在输入
我有一个 GLSL 着色器,它从输入纹理的 channel 之一(例如 R)读取,然后写入输出纹理中的同一 channel 。该 channel 必须由用户选择。 我现在能想到的就是使用一个 int
我想根据下拉列表中的选定值生成输入文本框。 Options 2 3 4 5 就在这个选择框之后,一些输入字段应该按照选定的数字出现。 最佳答案 我建议您使用响应式(Reac
我是 SQL 新手,我想问一下如何根据首选项和分组选择条目。 +----------+----------+------+ | ENTRY_ID | ROUTE_ID | TYPE | +------
我有以下表结构: CREATE TABLE [dbo].[UTS_USERCLIENT_MAPPING_USER_LIST] ( [MAPPING_ID] [int] IDENTITY(1,1
我在移除不必要的床单时遇到了问题。我查看了不同的论坛并将不同的解决方案混合在一起。 此宏删除工作表(第一张工作表除外)。 Sub wrong() Dim sht As Object Applicati
我是一名优秀的程序员,十分优秀!