没有人能阻挡我前进的道路
cd Desktop
brew install mysql
Database |
---|
information_schema |
mysql |
performance_schema |
sys |
4 rows in set(0.01 sec)
mysql> create database my_db;
mysql> CREATE TABLE Account
(
ID int NOT NULL AUTO_INCREMENT,
AccountName varchar(255) NOT NULL,
Password varchar(255) NOT NULL,
PRIMARY KEY (ID)
);
Query OK, 0 rows affected (0.09 sec)
mysql> INSERT INTO Account (AccountName, Password)
VALUES ('ArthurSlog','ArthurSlog);
Query OK, 1 row affected (0.08 sec)
mysql> SELECT * FROM Account;
ID AccountName Password 1 ArthurSlog ArthurSlog 1 row in set (0.00 sec)
exit;
Bye
cd ~/Desktop/nodejs_learningload
<html>
<head>
<meta charset="utf-8">
<title>SingUptitle>
head>
<body>
<p>Sing upp>
<form action="http://127.0.0.1:8081/singupAccount" method="GET">
Name: <input type="text" name="name">
<br>
Password: <input type="text" name="password">
<br>
<input type="submit" value="注册">
form>
<br>
body>
html>
<form action="http://127.0.0.1:8081/singupAccount" method="GET">
Name: <input type="text" name="name">
<br>
Password: <input type="text" name="password">
<br>
<input type="submit" value="注册">
form>
action="http://127.0.0.1:8081/singupAccount" method="GET"
action="http://127.0.0.1:8081/singupAccount"
/singupAccount
index.js
var express = require('express');
var fs = require("fs");
var app = express();
app.use(express.static(__dirname));
app.get('/singupAccount', function (req, res) {
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '88888888',
database : 'my_db'
});
connection.connect();
var response = {
"name":req.query.name,
"password":req.query.password
};
var addSql = 'INSERT INTO Account(AccountName, Password) VALUES(?,?)';
var addSqlParams = [response.name, response.password];
connection.query(addSql,addSqlParams,function (err, result) {
if(err){
console.log('[INSERT ERROR] - ',err.message);
res.send('执行sql出错!');
return;
}
res.send('Welcome~ SingUp Success ^_^');
});
connection.end();
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("应用实例,访问地址为 http://%s:%s", host, port)
})
var express = require('express');
var app = express();
app.use(express.static(__dirname));
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("应用实例,访问地址为 http://%s:%s", host, port)
}
app.get('/singupAccount', function (req, res) {
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '88888888',
database : 'my_db'
});
connection.connect();
var response = {
"name":req.query.name,
"password":req.query.password
};
var addSql = 'INSERT INTO Account(AccountName, Password) VALUES(?,?)';
var addSqlParams = [response.name, response.password];
connection.query(addSql,addSqlParams,function (err, result) {
if(err){
console.log('[INSERT ERROR] - ',err.message);
res.send('执行sql出错!');
return;
}
res.send('Welcome~ SingUp Success ^_^');
});
connection.end();
})
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '88888888',
database : 'my_db'
});
connection.connect();
req.query.name
req.query.password
var response = {
"name":req.query.name,
"password":req.query.password
};
var addSql = 'INSERT INTO Account(AccountName, Password) VALUES(?,?)';
var addSqlParams = [response.name, response.password];
connection.query(addSql,addSqlParams,function (err, result) {
if(err){
console.log('[INSERT ERROR] - ',err.message);
res.send('执行sql出错!');
return;
}
res.send('Welcome~ SingUp Success ^_^');
});
connection.end();
mysql -u root -p
Enter password:
mysql>
mysql> use my_db;
Database changed
mysql> select * from Account;
ID AccountName Password
1 KingArthur KingArthur
1 row in set (0.00 sec)
mysql>
node index.js
ID AccountName Password
1 KingArthur KingArthur
2 KingArthur2 KingArthur2
2 row in set (0.00 sec)