如何使用 MEAN 堆棧(MySQL、Express、Angular、Node)將數
2023-06-06
數據庫問題
html5模板網
how to insert data into database using MEAN stack (MySQL, Express, Angular, Node)(如何使用 MEAN 堆棧(MySQL、Express、Angular、Node)將數據插入數據庫)
本文介紹了如何使用 MEAN 堆棧(MySQL、Express、Angular、Node)將數據插入數據庫的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
如何使用 MEAN 堆棧(MySQL、Express、Angular、Node)將數據插入數據庫?我在插入時遇到了問題.請幫我解決這個問題.這是我的代碼.我是 MEAN 的新手.
index.html
<頭><link rel="stylesheet" ><標題>注冊頁面</title>頭部><身體><div class="container" ng-app="bookitnow" ng-controller="myCtrl"><h1>注冊</h1><form method="post"><div class="form-group"><input type="text" class="form-control" id="firstName" ng-model="fname" placeholder="First Name" required>
<div class="form-group"><input type="text" class="form-control" id="lastName" ng-model="lname" placeholder="Last Name" required>
<div class="form-group"><input type="email" class="form-control" id="email" ng-model="email" placeholder="Email Address" required>
<div class="form-group"><input type="text" class="form-control" id="phone" ng-model="phone" placeholder="Phone" required>
<button type="submit" class="btn btn-info" ng-click="submit()">提交</button></表單>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script><script src="controllers/controller.js"></script></html>
server.js
var express = require("express");var app = express();var bodyParser = require('body-parser');var mysql = require('mysql');var connection = mysql.createConnection({主機:'本地主機',用戶:'根',密碼 : '',數據庫:'bookitnow'});connection.connect(function(err) {如果(!錯誤)console.log('數據庫已連接');別的console.log('數據庫連接錯誤.');});app.use(express.static(__dirname + '/public'));app.get('/index.html', function(req, res){res.sendFile(__dirname + '/public' + 'index.html');});app.post('/index.html', function(req, res, next) {var data = req.body;console.log('收到請求:', 數據);connection.query('INSERT INTO register SET ?', data, function(err,res){如果(錯誤)拋出錯誤;console.log('記錄插入');});app.listen(5500);console.log('你好,來自 server.js');
controller.js
var app = angular.module('bookitnow',[]);app.controller('myCtrl', function($scope,$http){$scope.submit = function() {變量數據 = {fName : $scope.fname,lName : $scope.lname,電子郵件:$scope.email,電話:$scope.phone}};$http.post('/index.html', &scope.data).success(功能(數據,狀態,標題,配置){$scope.result = 數據;console.log("插入成功")}).錯誤(功能(數據,狀態,標題,配置){$scope.result = "數據:" + 狀態;});
解決方案
在您的 controller.js 中,嘗試以下操作,看看會發生什么.
var app = angular.module('bookitnow',[]);app.controller('myCtrl', function($scope, $http){$scope.submit = function() {變量數據 = {fName : $scope.fname,lName : $scope.lname,電子郵件:$scope.email,電話:$scope.phone};$http.post('/index.html', 數據).then(功能(響應){控制臺日志(響應)},功能(響應){控制臺日志(響應)});};});
How to insert data into database using MEAN stack (MySQL, Express, Angular, Node)?
I am facing the problem at the time of insertion. Please help me to solve this. Here is my code. I'm newbie in MEAN.
index.html
<html ng-app="bookitnow">
<head>
<link rel="stylesheet" >
<title> Registration Page </title>
</head>
<body>
<div class="container" ng-app="bookitnow" ng-controller="myCtrl">
<h1>Register</h1>
<form method="post">
<div class="form-group">
<input type="text" class="form-control" id="firstName" ng-model="fname" placeholder="First Name" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="lastName" ng-model="lname" placeholder="Last Name" required>
</div>
<div class="form-group">
<input type="email" class="form-control" id="email" ng-model="email" placeholder="Email Address" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="phone" ng-model="phone" placeholder="Phone" required>
</div>
<button type="submit" class="btn btn-info" ng-click="submit()">Submit</button>
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src="controllers/controller.js"></script>
</body>
</html>
server.js
var express = require("express");
var app = express();
var bodyParser = require('body-parser');
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'bookitnow'
});
connection.connect(function(err) {
if (!err)
console.log('Database is connected');
else
console.log('DB connection error.');
});
app.use(express.static(__dirname + '/public'));
app.get('/index.html', function(req, res){
res.sendFile(__dirname + '/public' + 'index.html');
});
app.post('/index.html', function(req, res, next) {
var data = req.body;
console.log('request received:', data);
connection.query('INSERT INTO register SET ?', data, function(err,res){
if(err) throw err;
console.log('record inserted');
});
app.listen(5500);
console.log('Hi from server.js');
controller.js
var app = angular.module('bookitnow',[]);
app.controller('myCtrl', function($scope,$http){
$scope.submit = function() {
var data = {
fName : $scope.fname,
lName : $scope.lname,
email : $scope.email,
phone : $scope.phone
}
};
$http.post('/index.html', &scope.data)
.success(function (data, status, headers, config) {
$scope.result = data;
console.log("inserted successfully")
})
.error(function(data, status, header, config){
$scope.result = "Data: " + status;
});
解決方案
In your controller.js, try the following and see what happens.
var app = angular.module('bookitnow',[]);
app.controller('myCtrl', function($scope, $http){
$scope.submit = function() {
var data = {
fName : $scope.fname,
lName : $scope.lname,
email : $scope.email,
phone : $scope.phone
};
$http
.post('/index.html', data)
.then(function (response) {
console.log(response)
}, function(response){
console.log(response)
});
};
});
這篇關于如何使用 MEAN 堆棧(MySQL、Express、Angular、Node)將數據插入數據庫的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!