PDA

View Full Version : trouble with insert into db with jscript


bigdude
09-23-2002, 07:00 AM
hello
using mysql server with asp and jscript
it looks like the jscript doesn't like "" around variables in the insert string....
can anyone help me on how to get the value of a variable into this?
here are some things i've tried:

var insertBird=Request.Form("txtBird");
result=conn.Execute("INSERT INTO birds VALUES('$insertBird$')");
// database gets "$insertBird$"
result=conn.Execute("INSERT INTO birds VALUES('$insertBird')");
// database gets "$insertBird"
result=conn.Execute("INSERT INTO birds VALUES('insertBird')");
// database error that there is no col "insertbird"
result=conn.Execute("INSERT INTO birds VALUES('"insertBird"')");
// jscript error that it expects ";" after "('"

anyone help on this?
thanks in advance

Alekz
09-23-2002, 09:23 AM
Hi,
Should $insertBird$ be a JScript variable or a string constant?
If it's a variable then
result=conn.Execute("INSERT INTO birds VALUES(\"" + insertBird + "\")");
should be correct syntax.
If it's a string constant then:
result=conn.Execute("INSERT INTO birds VALUES(\"insertBird\")");
should be what You want.

Alex

bigdude
09-23-2002, 09:18 PM
hi
thanks for the reply :)
insertBird is supposed to be a variable
what works is

result=conn.Execute("INSERT INTO birds VALUES('"+insertBird+"')");
// value in insertBird does to database :-)

result=conn.Execute("INSERT INTO birds VALUES(\""+insertBird+"\")");
// also value in insertBird goes to database :-)

so another question is, does it matter which of the 2 ways above are used since they both seem to work?
is one syntactically better or more likely to keep me out of trouble?
thanks again
cheers

Alekz
09-24-2002, 07:41 AM
Hi,
It depends on what Your database accepts as string delimiter...
Some of them accept only single quote, some of them double and some of them both... You have to evaluate what is Your database, will it evolve and to which one to answer Your question...
Your first example sends the SQL statement with single quote string delimiter, second one with double quote. mySQL supports both syntaxes.

Alex