账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    用node怎么将前端传过来的数据存入到mysql中去
    20
    0

    目的:使用nodejs将前端返回的数据插入到mysql中的表去

    代码逻辑:
    ##1、前端页面

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <meta http-equiv="X-UA-Compatible" content="ie=edge">
     <title>Document</title>
     <style>
         * {
             padding: 0;
             margin: 0;
         }
     </style>
    </head>
    
    <body>
     <!-- <form action=""> -->
     <label for="id">id</label>
     <input type="text" name='name' id='id'>
     <label for="name">商品名称</label>
     <input type="text" id='name'>
     <label for="price">口号</label>
     <input type="text" id='price'>
     <input type="submit" value="提交" id="submit">
     <!-- </form>  -->
     <input type="file" name="" id="" accept="image/*">
    </body>
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script>
      
     $('#submit').click(() => {
         $.ajax({
             type: 'post',
             url: 'http://localhost:3001/',
             data: {
                 id: $('#id').val(),
                 name: $("#name").val(),
                 price: $("#price").val()
             },
             datatype: 'json',
             success: function (res) {
                 console.log(res)
             },
             error: function (err) {
                 console.log(err)
             }
         })
     })
    </script>
    
    </html>

    2、后端页面

    connction.js

    var connction = {};
    
    connction.mysql = {
    
        host: "localhost", //这是数据库的地址
    
        user: "root", //需要用户的名字
    
        password: "******.", //用户密码 ,如果你没有密码,直接双引号就是
    
        database: "T2" //数据库名字
    
    } //好了,这样我们就能连接数据库了
    
    ////////////////////当然这是不可能的
    
    module.exports = connction; //用module.exports暴露出这个接口,
    

    接口index.js

    var express = require('express');
    var router = express.Router();
    var mysql = require('mysql');
    var sql01 = require('./../connction');
    
    // 使用DBConfig.js的配置信息创建一个MySQL连接池
    var pool = mysql.createPool(sql01.mysql);
    // var $sql = $mysql.createConnection(sql01.mysql) //创建一个连接        mysql是我们上面文件暴露出来的模板的方法
    // $sql.connect()
    
    // 响应一个JSON数据
    // var responseJSON = function (res, ret) {
    //   if (typeof ret === 'undefined') {
    //     res.json({
    //       code: '-200',
    //       msg: '操作失败'
    //     });
    //   } else {
    //     res.json(ret);
    //   }
    // };
    // 添加用户
    router.post('/', function (req, res, next) {
      // console.log(req )
      // 从连接池获取连接 
      pool.getConnection(function (err, connection) {
        // 获取前台页面传过来的参数  
        
        var param =req.body;
        var addSql = 'insert into goods(id,name,price) values (?,?,?)';
        var hh = [param.id,param.name,param.price];
        console.log(param) //
        // 建立连接 增加一个用户信息 
        connection.query(addSql,hh, function (err, result) {
          console.log(result)
          if (result) {
            result = {
              code: 200,
              msg: '增加成功'
            };
          }
    
          // 以json形式,把操作结果返回给前台页面     
          // responseJSON(res, result);
    
          // 释放连接  
          connection.release();
    
        });
      });
    });
    
    /* GET home page. */
    router.get('/', function (req, res, next) {
      res.send('halll')
    });
    router.get('/login', function (req, res, next) {
      res.send("this is  index/login page")
    })
    module.exports = router;

    现在可以获取前端传入的数据,但是读到的数据不能写入到mysql中指定的表中去,求解!!

    0
    打赏
    收藏
    点击回答
        全部回答
    • 共 0 条
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部