将Checkbox数据插入数据库,ASP VBScript和SQL

时间:2011-07-12 13:48:07

标签: sql vbscript asp-classic ado checkbox

我还没有找到解决这个问题的方法,所以我想我会发布它,看看是否有人遇到过类似的问题。我基本上是尝试根据复选框表单数据将多个新项添加到Access SQL数据库的同一个表中。用户选择多个类别来对项目进行分类(也在另一个表中添加)。这是我必须检索数据并将其存储在静态大小数组中的代码,该数组等于最大可能选择(我有错误而没有将它放入已知长度的数组中):

Dim VidName, VidUrl, VidDescription, VidCategory, VidKeywords, VidSRC, VidType, Hidden, Approve, RunTime, mode_a, i, VidNail, myArray(5) 

VidCategory=Request("Category")

mode_a=split(VidCategory,",")

For i=LBound(mode_a) to UBound(mode_a)

    myArray(i) = mode_a(i)

Next 

然后在页面的后面,在将视频数据插入视频表后,我需要调用视频类别表并插入视频ID以及每个相应的类别ID:

SQL = "SELECT tbl_categories.category_name, tbl_categories.category_id"
SQL = SQL & " FROM (tbl_video_categories INNER JOIN tbl_video ON tbl_video_categories.video_id = tbl_video.video_id) INNER JOIN tbl_categories ON tbl_video_categories.category_id = tbl_categories.category_id"
SQL = SQL & " GROUP BY tbl_categories.category_name, tbl_categories.category_active, tbl_categories.category_id"
SQL = SQL & " HAVING (((tbl_categories.category_active)=1))"
SQL = SQL & " ORDER BY tbl_categories.category_id "


Set rs2 = LoadData(SQL)

If rs2.RecordCount > 0 Then

    rs2.MoveFirst

    Dim j

    j = 0

* '发送是其他地方定义的速记函数,基本上只是Response.Write这些发送仅用于测试目的。 *

    Do While Not rs2.EOF

        Send myArray(j)

        Send "<br>"

        Send rs2("category_id")

        Send "<br>"

        Send rs2("category_name")

        Send "<br>"
        Send "<br>"

        If rs2("category_name") = myArray(0) Then

            Send "ZERO"
            Send " <br/>"
            SQL = "INSERT INTO tbl_video_categories (video_id, category_id)"

            SQL = SQL & " VALUES (" & rs("video_id") & "," & rs2("category_id") & ")"

            PerformSQL(SQL)


        End If
        If rs2("category_name") = myArray(1) Then

            Send "ONE"
            Send " <br/>"
            SQL = "INSERT INTO tbl_video_categories (video_id, category_id)"

            SQL = SQL & " VALUES (" & rs("video_id") & "," & rs2("category_id") & ")"

            PerformSQL(SQL)


        End If
        If rs2("category_name") = myArray(2) Then

            Send "TWO"
            Send " <br/>"
            SQL = "INSERT INTO tbl_video_categories (video_id, category_id)"

            SQL = SQL & " VALUES (" & rs("video_id") & "," & rs2("category_id") & ")"

            PerformSQL(SQL)


        End If
        If rs2("category_name") = myArray(3) Then

            Send "THREE"
            Send " <br/>"
            SQL = "INSERT INTO tbl_video_categories (video_id, category_id)"

            SQL = SQL & " VALUES (" & rs("video_id") & "," & rs2("category_id") & ")"

            PerformSQL(SQL)


        End If
        If rs2("category_name") = myArray(4) Then
            Send "FOUR"
            Send " <br/>"
            SQL = "INSERT INTO tbl_video_categories (video_id, category_id)"

            SQL = SQL & " VALUES (" & rs("video_id") & "," & rs2("category_id") & ")"

            PerformSQL(SQL)


        End If

        rs2.MoveNext

        j = j + 1

    Loop

End If

Set rs2 = Nothing

没有错误,但无论我如何尝试更改它(使用Elseifs,更改顺序,使用计数器逐步执行myArray等)它只会添加第一个类别数组到表,即使有多个选中。如果你单步执行myArray(j)和rs2.categories,它们都有数据(假设点击了一些复选框),如果选择了所有类别并且在myArray中,它们甚至会在相同的迭代上打印出来。所以数据就在那里,但是“Send Zero”If语句是唯一一个真实的语句,它只出于某种原因执行一次,即使Do While not rs2.EOF执行5次(目前有5个类别)

非常感谢您对此的任何帮助!

编辑:rs是在页面上定义的另一个记录集,你可以假设它有正确的sql插入数据(也就是sql代码都可以工作)

* 编辑2:我尝试了ipr101的建议。选中第一个4/5复选框,发送myArray(n)和rs2(“category_name”),这是输出:*

  

迭代次数:0

     

数组:风险管理

     

RecordSet:风险管理

     

ZERO

     

迭代次数:1

     

数组:EVMS

     

RecordSet:EVMS

     

迭代次数:2

     

数组:PMP考试准备

     

RecordSet:计划规划

     

迭代次数:3

     

数组:讲座

     

RecordSet:PMP考试准备

     

迭代次数:4

     

数组:

     

RecordSet:讲座

正如您所看到的,它只执行了第一个if,但记录集和数组在将来的迭代中仍然有值,但没有执行其他if语句 < / p>

答:好的,我想出了如何使这项工作。基本上我试图过度复杂化。而不是从表单中发送类别名称并尝试将其与数据库值进行比较,然后尝试添加video_id和类别ID,我只是发送类别ID(这是唯一的)作为复选框值(s),然后在第一个for循环中执行sql:

For i=LBound(mode_a) to UBound(mode_a)

    SQL = "INSERT INTO tbl_video_categories (video_id, category_id)"

    SQL = SQL & "VALUES (" & rs("video_id") & "," & mode_a(i) & ")"

    PerformSQL(SQL)

Next

感谢大家的意见和建议,他们非常乐于助人!

1 个答案:

答案 0 :(得分:0)

你试过这个吗?

If rs2("category_name") = myArray(1) OR rs2("category_name") = myArray(2) OR rs2("category_name") = myArray(3) OR rs2("category_name") = myArray(4) Then
    SQL = "INSERT INTO tbl_video_categories (video_id, category_id)"
    SQL = SQL & " VALUES (" & rs("video_id") & "," & rs2("category_id") & ")"

    PerformSQL(SQL)
End If