Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating a SQL SELECTstatement in javascript

Status
Not open for further replies.

jjfjr

Programmer
Mar 10, 2004
13
US
Hi;

I'm creating some javascript code in frontpage so that I can retrieve some info from an MS Access database. The database has three fields: username,password and stylesheet. I'm trying to retrieve the stylesheet value by
having a user input their username and password in a form. I then create a SQL SELECT statement with the following code:

SQLstr = "SELECT [stylesheet] FROM userinfo WHERE
(([username] = '" + document.user_form.username.value + "') AND
([password] = '" + document.user_form.password.value + "'))";

When problem-free this will be used to access the info from the database via a connection string. When I try to run the form page , I get an error that says "Unterminated string constant line 22 column 51" . Line 22 column 51 is
the character v in value in the second line.

Once the SQL SELECT statement works, I want to use it to retrieve the stylesheet field from the MS Access database like this:

var strConn = "Driver={Microsoft Access Driver
(*.mdb)};DBQ=" + Server.MapPath("users.mdb");

// connection object creation
var dbConn = Server.CreateObject("ADODB.Connection");

// open the connection
dbConn.Open(strConn);

// Execute the query with constructed SQLstr
var rs = dbConn.Execute(SQLstr);

I'm hoping that the variable rs will hold the stylesheet info and I can pass it to the webpage via the url like this:

<a href="nextpage.asp?styles=<% thestylesheetfield %>"

and link it on the next page, e.g.,

<link rel="stylesheet" type="text/css" href="/styles/<%=
request.querystring(styles) %>" media="screen">

I'm little a new to javascript and not used to mixing single and double quotes; I had gotten this code from reading some texts. Any help is greatly appreciated.
 
Looks like there are spaces or the lack of them and/or line-break/carriage-return etc here?

[tt]>SQLstr = "SELECT [stylesheet] FROM userinfo WHERE
>(([username] = '" + document.user_form.username.value + "') AND
>([password] = '" + document.user_form.password.value + "'))";[/tt]

Try again with single line like this?

[tt]SQLstr = "SELECT [stylesheet] FROM userinfo WHERE[COLOR=red yellow] [/color](([username] = '" + document.user_form.username.value + "') AND[COLOR=red yellow] [/color]([password] = '" + document.user_form.password.value + "'))";
[/tt]
The major problem is also that the latter part of the post seems to suggest that it is operating on the server and that the db is on the server. Then you have to rethink about the whole thing. You cannot use document.user_form.... etc and then at the other part SQLstr being used on the server as a connection string. That usually shows a tell-tale confusion. Maybe you have use replace document.user_form.... parts by request.form("user_name") and request.form("password").

 

And you could store the details you get back from the access file as a cookie or session variable to prevent having to pass it around in the URL.

Remember to wrap any code you want to post here in [code]...[/code] tags -- this will preserve all the code you post (including whitespace etc).

Jeff

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top