title: How to insert into and update sql using NodeJS, socket.io, postgres correctly?
body: <p>I'm a baby in programming especially NodeJS and Javascript. I've been learning about how to connect to PostgreSQL via Javascript using NodeJS and socket.io. </p>
  
  <p>I have searched and found this link <a href="http://gonzalo123.com/2012/05/07/asynchronous-queries-to-relational-databases-from-browser-with-node-js-and-socket-io/" rel="nofollow">http://gonzalo123.com/2012/05/07/asynchronous-queries-to-relational-databases-from-browser-with-node-js-and-socket-io/</a>. Followed him and works well with select and update statement.</p>
  
  <p>However, I still can't accomplish with insert into statement and update with specified values. Here is my current code.</p>
  
  <p><strong>Server Side:</strong></p>
  
  <pre><code>var pg = require('pg');
  
  var conString = "tcp://postgres:1234@localhost:5432/testdb";
  var client = new pg.Client(conString);
  client.connect();
  
  var io = require('socket.io').listen(8888);
  
  io.sockets.on('connection', function (socket) {
  socket.on('sql', function (data) {
  var query = client.query(data.sql, data.values);
  query.on('row', function(row) {
  socket.emit('sql', row);
  });
  });
  });
  </code></pre>
  
  <p><strong>Client (html):</strong></p>
  
  <pre><code>&lt;html&gt;
   &lt;head&gt;&lt;/head&gt;
   &lt;body&gt;
   &lt;script src="http://localhost:8888/socket.io/socket.io.js"&gt;&lt;/script&gt;
   &lt;script&gt;
   var socket = io.connect('http://localhost:8888');
   var mobileno = '0123456789';
   function sendSql(sql, values, cbk) {
  socket.emit('sql', { sql: sql, values : values});
  socket.on('sql', function(data){
      console.log(data);
  });
  }
  
  &lt;/script&gt;    
  &lt;p&gt;
   &lt;a href="#" onclick="sendSql('select * from user', [], function(data)          {console.log(data);})"&gt;Select&lt;/a&gt;
   &lt;/p&gt;
  
   &lt;a href="#" onclick="sendSql('UPDATE user set mobile = 987654321 where id=12 ', [],  function(data) {})"&gt;Update&lt;/a&gt;
   &lt;/p&gt;
   &lt;p&gt;
     &lt;a href="#" onclick="sendSql('INSERT INTO user (name) values('Michael'),[], function(data)    {console.log(data);})"&gt;Insert&lt;/a&gt;
   &lt;/p&gt;
  
  
   &lt;/body&gt;
   &lt;/html&gt;
  </code></pre>
  
  <p>From above code, Select statement works perfectly with all data shows in Chrome's console.</p>
  
  <p>For Update Statement, I can update data in column 'mobile' with value '987654321'. But what I want is 
  the value from <code>var mobileno = '123456789';</code> then I tried...</p>
  
  <pre><code>UPDATE user set mobile = mobileno
  </code></pre>
  
  <p>It doesn't works and the console of server says "column mobileno doesn't exists".</p>
  
  <p>For Insert Statement, I really have no idea with this. Tried to change syntax many ways but still can't make it works and still get various errors.</p>
  
  <p>I guess somebody here should have experience with this. Please kindly teach me how to use update and insert into statement of this thing correctly. </p>