PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, December 13, 2022

[FIXED] How to use placeholders while inserting data in MySql database using nodejs?

 December 13, 2022     documentation, faker, mysql, node.js, syntax     No comments   

Issue

let r={ email:faker.internet.email() } ;
connection_var.query("insert into users set ?",r, function(err,res){
    if(err) throw err;
    console.log(res);
} );

connection_var.end();

I wrote this code to insert a fake email addr in already existing database and into the users table just the problem is I am not fully able to understand what and how does the " SET ?" work and how is it relating to r and what if r has more than 1 key-value pairs


Solution

? is a placeholder. It gets replaced with all the keys and values in the object passed as the next argument. So if you have

let r = {col1: 1, col2: "abc", col3: 999};

the query will become

insert into users set col1 = 1, col2 = 'abc', col3 = 999


Answered By - Barmar
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing