Question

I am having some issue while trying to insert a row into my database.

Here is the structure of my table :

mysql> describe emprunt;
+------------------+---------+------+-----+---------+----------------+
| Field            | Type    | Null | Key | Default | Extra          |
+------------------+---------+------+-----+---------+----------------+
| idEmprunt        | int(11) | NO   | PRI | NULL    | auto_increment |
| Joueur_idEmprunt | int(11) | NO   |     | NULL    |                |
| valeur_empruntee | float   | NO   |     | NULL    |                |
| duree_emprunt    | int(11) | NO   |     | NULL    |                |
| jour_emprunt     | int(3)  | NO   |     | NULL    |                |
| mois_emprunt     | int(3)  | NO   |     | NULL    |                |
+------------------+---------+------+-----+---------+----------------+

What my program must do is : The client open an HTML page in which there is a button. When clicked, data is sent to the server to add a row in my table.

I give you the whole code so that you may understand, but you should focus on the last lines, the function setEmprunt(..), because it is where the error comes from.

var server = require('http').createServer(function(req, res){
        res.end('chargement effectué');
    });
server.listen(8080);    

var io = require('socket.io').listen(server);

var mysql = require('mysql');
//using a pool to manage multi users queries
var pool = mysql.createPool({
    host     : 'localhost',
    user     : 'NoNeedToKnowThat', 
    password : 'Neither', 
    database : 'test' //
});

//A client open the HTML page
io.sockets.on('connection', function (socket) {
    console.log('Client !!');



    //===================================================
    // Client clicks the button and sends 2, 7000, 15
    //===================================================
    socket.on('setEmprunt', function (aIdJoueur, aSomme, aDuree) {

        pool.getConnection(function(err, connection) {

            setEmprunt(connection, aIdJoueur, aSomme, aDuree, function(err){
                if(err) {
                    console.log("error in setEmprunt : "+ err);
                    throw err;
                }
                else
                    console.log("callback of setEmprunt");
            });
            // And done with the connection.
            connection.release();

        });

    }); 


function setEmprunt(aConnection, aIdJoueur, aSomme, aDuree, aCallback){
    var maintenant= new Date(),
        jourEmprunt = maintenant.getDate(),
        moisEmprunt = maintenant.getMonth();
    rowToBeInserted = { Joueur_idEmprunt : aIdJoueur, valeur_empruntee : aSomme, duree_emprunt : aDuree, jour_emprunt : jourEmprunt, mois_emprunt : moisEmprunt };
    console.log("set emprunt de : " + rowToBeInserted);


//There is the problem line
    aConnection.query('INSERT INTO emprunt VALUES ?', rowToBeInserted, aCallback);


}

And there is my result :

C:\Users\QuentinB\Developpement>node serveur.js
   info  - socket.io started
   info  - handshake authorized ZIH1OSAUuRGHz8G9hCfc
Client !!
set emprunt de : [object Object]
error in setEmprunt : Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syn
tax to use near '`Joueur_idEmprunt` = 2, `valeur_empruntee` = 7000, `duree_emprunt` = 15, `jour_e' at line 1

Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`Joueur_
idEmprunt` = 2, `valeur_empruntee` = 7000, `duree_emprunt` = 15, `jour_e' at line 1
    at Query.Sequence._packetToError (C:\Users\QuentinB\Developpement\giserv5\node_modules\mysql\lib\protocol\sequences\Sequence.js:30:14)

I know this is the last line of my code which is blocking, because I tried to replace it by :

aConnection.query('INSERT INTO emprunt(Joueur_idEmprunt, valeur_empruntee, duree_emprunt, jour_emprunt, mois_emprunt) VALUES('+aIdJoueur+','+aSomme+','+aDuree+','+jourEmprunt+','+moisEmprunt+');', aCallback);

and it worked. I would like to understand why aConnection.query('INSERT INTO emprunt VALUES ?', rowToBeInserted, aCallback) is not working.

Thank you for your help.

Was it helpful?

Solution

From a quick look, I believe you want to use INSERT INTO...SET ? not INSERT INTO...VALUES ? in your code in this case. The ? expands into name=val pairs, while the VALUES clause is looking for just values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top