简体   繁体   English

我无法使用HTML和JavaScript将数据存储到mysql数据库

[英]I'm not able to store the data to mysql database using HTML and JavaScript

I'm writing a program in Node.js to get the data and store it in database. 我正在Node.js中编写程序以获取数据并将其存储在数据库中。 I'm passing the data from HTML page to JavaScript for validation and to store the data in MySQL database, if the validations are true, but after validation, it's not storing the data in MySQL. 我将数据从HTML页面传递到JavaScript以进行验证,并将数据存储在MySQL数据库中(如果验证是正确的),但是在验证之后,它没有将数据存储在MySQL中。

Can you help me identifying the problem? 您能帮我找出问题所在吗?

Code: 码:

var mysql = require('mysql');

function validateform(){

    var fname = document.myform.fname.value;
    var lname = document.myform.lname.value;
    var email_id = document.myform.email.value;

    if (fname==null || fname==""){
        alert("First Name can't be blank");
            return false;
    }
    else if(lname==null || lname==""){
        alert("Last Name  can't be blank");
            return false;
    }
    else if(email_id.length<6){
        alert("Email must be at least 6 characters long.");
            return false;
    }
    else{

        var connection = mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: 'xxx',
        database: 'example'
        });

        connection.connect(function(err) {
        if(err){
            console.log('Error');
        }
        else{
            console.log('Connected');
            }
        });

        var exampleData = {
            firstName: fname,
            lastName: lname,
            email: email_id
        };


        var query = connection.query('insert into example set ?', exampleData, function (err, result) {
            console.log(query.sql);
            if (err) {
                console.error(err);
                return;
            }
            console.log(result);
        });
    }
}

You have an error in your query. 您的查询有误。 Use values instead of set 使用values代替set

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JavaScript将数据存储到mysql数据库中? - How to store data into a mysql database using javascript? 单击插入按钮后,我正在使用 firebase 实时数据库存储来自我的 html 页面的数据,但它没有在数据库中添加数据 - I'm using firebase realtime database to store data from my html page after clicking on insert button, but it's not adding data in database 我无法使用 JavaScript Ajax 发布数据 - I'm not able to POST data using JavaScript Ajax 如何使用javascript将数据存储到数据库中 - how do I store data into database using javascript Javascript,我无法将焦点设置到 html 表单元素 - Javascript, I'm not being able to set focus to html form element 我可以在 mySQL 数据库中存储 JavaScript 对象吗? - Can I store a JavaScript Object in a mySQL database? 如何使用javascript和node.js-mysql而不使用php将HTML表单数据提交到MySql数据库表中? - How can I submit HTML form data into MySql database table using javascript along with node.js - mysql and without php? CRUD - 我无法将特定数据发送到数据库 - CRUD - I'm not able to send a especific data to the database 为什么我无法使用jQuery将HTML动态添加到页面? - Why I'm not able to dynamically add the HTML to the page using jQuery? Html - Javascript:Mysql 不在数据库中保存数据 - Html - Javascript: Mysql doesnt saving data in database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM