Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 从数据库填充复选框列表,如果之前选中,则在加载时自动检查_Sql_Vbscript_Asp Classic - Fatal编程技术网

Sql 从数据库填充复选框列表,如果之前选中,则在加载时自动检查

Sql 从数据库填充复选框列表,如果之前选中,则在加载时自动检查,sql,vbscript,asp-classic,Sql,Vbscript,Asp Classic,我有一个用户表单,它将根据存储在数据库中的字段填充某些类别,使用以下方法可以很好地工作: <% DSN = "DSN=LocalSQLServer" set panDB = server.createobject("ADODB.recordset") panDB.open "select attribute_name,attribute_id,attribute_question_value from attributes where attribute_type=6 and attr

我有一个用户表单,它将根据存储在数据库中的字段填充某些类别,使用以下方法可以很好地工作:

<%
DSN = "DSN=LocalSQLServer"
set panDB = server.createobject("ADODB.recordset")
panDB.open "select attribute_name,attribute_id,attribute_question_value from   attributes where attribute_type=6 and attribute_id>217 and attribute_id<250",DSN
Do while Not panDB.eof 
    response.write "<input type=checkbox value='" & panDB("attribute_question_value") & "' name='country_" & panDB("attribute_question_value") & "'>" & panDB("attribute_name")
    response.write "<BR>"
    panDB.MoveNext
loop%>
表格属性只是各种属性名称、其对应的属性类型和属性id的列表。选中其中一个复选框并保存表单时,将更新第二个表格以记录表单属性id、表单id,和属性_id。我的问题是在重新打开表单时检查以前选择的字段。有什么想法吗

这就是我到目前为止在检索字段是否被选中时得到的结果,基本上列出了类别中的所有字段,这样复选框仍然会被填充,然后想法是如果attribute_id字段不为null,则选中复选框。如果panDBattribute_id不为null,那么我会得到一个对象必需的错误

DSN = "DSN=LocalSQLServer"
set panDB = server.createobject("ADODB.recordset")
panDB.open "select a.attribute_question_value, s.test as 'attribute_id', a.attribute_name from attributes a left outer join (select attribute_id as 'test' from survey_attributes where survey_id=2091) s on a.attribute_id=s.test where a.attribute_type=6 and a.attribute_question_value < 4",DSN
Do while Not panDB.eof 
    if panDB("attribute_id") is not null then
        response.write "<input type='checkbox' checked value='" & panDB("attribute_question_value") & "' name='country_" & panDB("attribute_question_value") & "'>" & panDB("attribute_name")
    else
        response.write "<input type='checkbox' value='" & panDB("attribute_question_value") & "' name='country_" & panDB("attribute_question_value") & "'>" & panDB("attribute_name")
    end if 
        response.write "<BR>"
    panDB.MoveNext
loop
set panDB = Nothing
只需添加选中的属性,并根据DB值将其设置为true或false

"<input type='checkbox' " & (if (DB_Expression) then "checked='checked'" else "") & "value='" & panDB("attribute_question_value") & "' name='country_" & panDB("attribute_question_value") & "'>"
不要忘记在复选框周围加引号

只需添加选中的属性,并根据您的DB值将其设置为true或false

"<input type='checkbox' " & (if (DB_Expression) then "checked='checked'" else "") & "value='" & panDB("attribute_question_value") & "' name='country_" & panDB("attribute_question_value") & "'>"

别忘了在复选框周围加引号

我设计了一个查询,生成了我想要的输出,以便填充列表并根据存储的数据对其进行检查。我知道if语句可以被清理,但我在测试时把它放在了那里

DSN = "DSN=LocalSQLServer"
set panDB = server.createobject("ADODB.recordset")
panDB.open "select a.attribute_question_value, s.test, a.attribute_name from attributes a left outer join (select attribute_id as 'test' from survey_attributes where survey_id=" & survey_id & ") s on a.attribute_id=s.test where a.attribute_type=6 and a.attribute_question_value < 4",DSN
Do while Not panDB.eof 
    if panDB("test") > 0 then
        response.write "<input type='checkbox' checked value='" & panDB("attribute_question_value") & "' name='country_" & panDB("attribute_question_value") & "'>" & panDB("attribute_name")
    else
        response.write "<input type='checkbox' value='" & panDB("attribute_question_value") & "' name='country_" & panDB("attribute_question_value") & "'>" & panDB("attribute_name")
    end if 
        response.write "<BR>"
    panDB.MoveNext
loop
set panDB = Nothing

我能够设计一个查询,生成我想要的输出,以便填充列表并根据存储的数据对其进行检查。我知道if语句可以被清理,但我在测试时把它放在了那里

DSN = "DSN=LocalSQLServer"
set panDB = server.createobject("ADODB.recordset")
panDB.open "select a.attribute_question_value, s.test, a.attribute_name from attributes a left outer join (select attribute_id as 'test' from survey_attributes where survey_id=" & survey_id & ") s on a.attribute_id=s.test where a.attribute_type=6 and a.attribute_question_value < 4",DSN
Do while Not panDB.eof 
    if panDB("test") > 0 then
        response.write "<input type='checkbox' checked value='" & panDB("attribute_question_value") & "' name='country_" & panDB("attribute_question_value") & "'>" & panDB("attribute_name")
    else
        response.write "<input type='checkbox' value='" & panDB("attribute_question_value") & "' name='country_" & panDB("attribute_question_value") & "'>" & panDB("attribute_name")
    end if 
        response.write "<BR>"
    panDB.MoveNext
loop
set panDB = Nothing

我已经能够做到这一点,我的问题在于DB_表达式应该是什么。我尝试使用多个记录集返回所需的表字段,但这不起作用,因为我不知道如何将属性表中的属性id与表单属性表中的属性id进行比较。@Phil当您提交选中复选框时,您将此信息保存在您所说的第二个表中。您必须检索此信息,以确定它是否以前被选中。这是应该去那里的感谢@Shreder的帮助,我能够使用我在下面的帖子中的查询让一切正常工作。我使用测试字段检查是否存储了与当前调查相对应的数据,并相应地添加了checked属性。我已经能够做到这一点,我的问题在于DB_表达式应该是什么。我尝试使用多个记录集返回所需的表字段,但这不起作用,因为我不知道如何将属性表中的属性id与表单属性表中的属性id进行比较。@Phil当您提交选中复选框时,您将此信息保存在您所说的第二个表中。您必须检索此信息,以确定它是否以前被选中。这是应该去那里的感谢@Shreder的帮助,我能够使用我在下面的帖子中的查询让一切正常工作。我使用测试字段来检查是否存储了与当前调查相对应的数据,并相应地添加了checked属性。但我遇到的真正问题不是checked属性,而是正确的sql查询返回字段名,然后确定我向您询问的DB_表达式。我知道就我的表的结构而言,我没有给你太多的东西来处理,但这是我的查询选择a.attribute\u question\u value,s.test,a.attribute\u name from attributes a left outer join从survey\u attributes中选择attribute\u id作为“test”,其中survey\u id=&survey\u id&s在a.attribute\u id=s上。test其中a.attribute\u type=6和a.attribute\u question\u value<4解决了我的问题。也不必添加checked='checked'值,只是在输入部分简单地“checked”就行了,就像我所做的一样。我遇到的真正问题不是checked属性,而是正确的sql查询返回字段名,然后确定我问过你的DB_表达式。我知道就我的表的结构而言,我没有给你太多的东西来处理,但这是我的查询选择a.attribute\u question\u value,s.test,a.attribute\u name from attributes a left outer join从survey\u attributes中选择attribute\u id作为“test”,其中survey\u id=&survey\u id&s在a.attribute\u id=s上。test其中a.attribute\u type=6和a.attribute\u question\u value<4解决了我的问题。也不必添加checked='checked'值,只需在输入部分简单地“选中”,就像我有它工作正常一样。