简体   繁体   中英

How to get data from form and add inputs to Mysql table.Using JQuery

I'm trying to add input from HTML page to Mysql, I don't know what is wrong with my code. Why my input data from the form is not adding in MySQL database table? I already created a table in Mysql, but not able to insert values.

<form action="/data" method="post">
    <div class="form-group">
        <label for="exampleInputName">🙏 Name</label>
        <input type="text" class="form-control" id="exampleInputName" placeholder="Enter Name">
    </div>
    <div class="form-group">
        <label for="exampleInputEmail"> 📩 Email Address</label>
        <input type="email" class="form-control" id="exampleInputEmail" placeholder="Enter Email">
    </div>
    <div class="form-group">
        <label for="exampleInputcontactnumber"> 🔢 Contact number</label>
        <input type="number" class="form-control" id="exampleInputcontactnumber" placeholder="Enter Contact number">
    </div>
    <div class="form-group">
        <label for="exampleInputRestaurantName">🍽 Restaurant Name</label>
        <input type="text" class="form-control" id="exampleInputRestaurantName" placeholder="Enter RestaurantName">
    </div>
    <div class="res mt-5 mb-5 text-center" >
        <input type="submit" value="Submit" id="startapp" class="btn btn-primary" ></a>
    </div>   
</form>

var express    = require('express');
var app        = express();
var bodyParser = require('body-parser');
var mysql = require('mysql');
var con = mysql.createConnection({
    host     : '127.0.0.1',
    user     : 'root',
    password : 'password',
    database :'mydb'

});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static('public'));
app.set('view engine', 'ejs')

app.get('/', function (req, res) {

    res.sendFile(__dirname + '/Register.html');

});

app.post('/data', function(req, res){

    connection.query(sql , function(err, result){
    var sql = "INSERT INTO customers (Name,Email,ContactNumber,RestaurantName) VALUES (" + req.body.exampleInputName + "', '"+req.body.exampleInputEmail +"','"+ req.body.exampleInputcontactnumber +"','"+ req.body.exampleInputRestaurantName +"')";
        if(err) throw err;
        console.log("Data added");
    });

    res.send(data);

});

con.end();

app.listen(3000, function () {
console.log('App is running on port');
});

In you query you have an insert for a single column but you have values for 4

you also miss the single quote at the begin of the values so if you want insert a single column you should use

var sql = "INSERT INTO customers (Item)
 VALUES ('" + req.body.exampleInputName  +"')";
        if(err) throw err;
        console.log("Data added");
    });

if you want insert value for 4 columns you should

  var sql = "INSERT INTO customers (your_col_name, your_col_email, your_col_contact, your_col_resturant)
   VALUES ('" + req.body.exampleInputName + "', '"+req.body.exampleInputEmail +"','"+ req.body.exampleInputcontactnumber +"','"+ req.body.exampleInputRestaurantName +"')";
          if(err) throw err;
          console.log("Data added");
      });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM