0

I need your help,

How can I program a user defined dynamic SQL query.

I have 4 text fields on my webpage.

var a = document.getElementById('prefix').value
var b = document.getElementById('firstname').value
var c = document.getElementById('middlename').value
var d = document.getElementById('lastname').value

How can I use javascript to compile and build an SQL statement if the user fills out either one of the textboxes.

For example, prefix, lastname and firstname are filled in:

SELECT * FROM TABLE WHERE PREFIX = a AND LASTNAME = d AND FIRSTNAME = b

For example, firstname and lastname are filled in

SELECT * FROM TABLE WHERE FIRSTNAME = b AND LASTNAME = d

Is there a better process, other than to use a bunch of if statements to check any 4 of the given conditions?

Seems like iterating through a whole wack of if statements is redundant.

Thanks

4

1 回答 1

0

不完全确定我理解您的要求,但使用 4 个 if 语句的替代方法是将它们包含在您的 SQL 语句中:

SELECT * 
FROM TABLE 
WHERE (PREFIX = a OR a = '') 
    AND (LASTNAME = d OR d = '')  
    AND (MIDDLENAME = c OR c = '') 
    AND (FIRSTNAME = b OR b = '') 

这样,如果值为空白,则不会搜索它们。

于 2013-05-01T17:27:48.847 回答