0

在我正在开发的网站上,用户可以使用 jQuery 插件添加多个表单字段。

但是我怎样才能将这些多条记录插入到数据库中呢?

    SQL = "INSERT INTO sehirler (memberid, sehiradi, sehirkodu, dateENTERED) VALUES ('"& Session("MEMBERID") &"', '"& sehiradi &"', '"& sehirkodu &"',  '"& NOW() &"')"
    Set objSehirEkle = objConn.execute(SQL)
4

3 回答 3

0

This is how you insert multiple "sets" in a single INSERT statement (you're not saying which database and version you're using; I believe SQL Server 2008 and above and MySQL support this syntax):

INSERT 
  INTO Table ( Col1, Col2, Col3 ) 
  VALUES
    ( Value1, Value2, Value3 ), 
    ( Value4, Value5, Value6 ), 
    ( Value7, Value8, Value9 )

But as other people are saying, mind your SQL Injection exposure first.

于 2013-05-29T03:09:52.903 回答
0

好吧,您可以一次简单地执行多个 SQL 语句,并用分号将它们链接起来:

SQL = "INSERT INTO MyTable (Field1, Field2) VALUES ('name1', 1);" & _
      "INSERT INTO MyTable (Field1, Field2) VALUES ('name2', 2);"
Set objSehirEkle = objConn.execute(SQL)

现在,如果您想对返回的记录集 (objSehirEkle) 做一些事情,那么您仅指的是 FIRST 语句。您可以使用该NextRecordset方法访问以下语句的结果,如下所示:

Set objNext = objSehirEkle.NextRecordset
于 2013-05-28T22:01:53.997 回答
-1

如果您使用 jQuery 插件插入许多字段,则必须在 asp 页面中控制它。
当您通过选择字段创建许多输入字段时,我模拟了一种情况。我设置了一个隐藏字段来控制我在页面上创建的元素的数量。asp 页面表单提交必须传递此字段。

尝试这样做(HTML页面):

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">    
    <title>Creating input fields</title>
    <script src="http://code.jquery.com/jquery-2.0.1.min.js"></script>
    <script>
      $().ready(function(){
        $('#branches').change(function(){
          var nCounter = 0;
          var nr_branches = $('#branches').val();

          //-- Cleaning all elements
          var $divB = $('#divBranches');                    

          if (!$('#divBranches').is(':empty')){            
            while($divB.children('input').length >= 1) {
              $divB.children("input:first").remove();
            }                                    
          }          

          while(nCounter < nr_branches) {
            //-- Create an input field
            var input = $("<input type='text' class='bra' name='branche_nr_"+ nCounter +"' placeholder='Branche Nr."+ nCounter +"' />");
            $divB.append(input);            
            nCounter++;
          }

          //-- Set number of fields in hidden
          $('#qty_fields').val(nCounter);

        });        
      });
    </script>
  </head>  
  <body>
    <form name="frm" action="savefields.asp" method="post">
      <input type="text" name="college" placeholder="University Name" />
      <select name="branches" id="branches">
        <option value="0">-select your branche-</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
      </select>
      <input type="hidden" name="qty_fields" id="qty_fields" />
      <div id="divBranches"></div>
      <input type="submit" value="submit" />      
    </form>
  </body>
</html>

ASP 页面(提交):

<%
nQtdFields = request.form("qty_fields")
nCount = 0

do while cint(nCount) < cint(nQtdFields)  
  SQL = "insert into table_name(name) values ('"& request.form("branche_nr_" & nCount) &"')"
  Set objSehirEkle = objConn.execute(SQL)
  nCount = nCount + 1
loop
%>

而已。
问候,文

于 2013-05-29T12:26:10.227 回答