Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Submitting Multiple Insert Records to Database - Help!

    Hello,

    Im using dreamweaver mx with msaccess databse and have come across a problem. I want to enter multiple records into the database but dreamweaver only allows me to enter one at a time.

    Basically i want to update anywhere from 2-30 records at a time but ill use 5 as an example below, they are automatically generated from the database named i1 - i30.

    I dont know how to edit dreamweaver code as its confusing and i dont know vbscript, i use javascript with asp and unfortunately its very hard to find any examples with javascript and asp.

    Here is the form that i want to update:

    Code:
    <%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body>
    <form name="form1" method="post" action="">
      <input name="i1" type="text" id="i1" size="5" maxlength="2">
      <br>
      <br>
      <input name="i2" type="text" id="i2" size="5" maxlength="2">
      <br>
      <br>
      <input name="i3" type="text" id="i3" size="5" maxlength="2">
      <br>
      <br>
      <input name="i4" type="text" id="i4" size="5" maxlength="2">
      <br>
      <br>
      <input name="i5" type="text" id="i5" size="5" maxlength="2">
      <br>
      <br>
      <input type="submit" name="Submit" value="Update">
    </form>
    </body>
    </html>

    Now here is the code that dreamweaver adds if i wanted to update the record. The only field in the table i would be updating would be the preference field and it would be a numeric value between 1-30.

    Code:
    <%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%> 
    <!--#include file="Connections/connection.asp" -->
    <%
    // *** Edit Operations: declare variables
    
    // set the form action variable
    var MM_editAction = Request.ServerVariables("SCRIPT_NAME");
    if (Request.QueryString) {
      MM_editAction += "?" + Request.QueryString;
    }
    
    // boolean to abort record edit
    var MM_abortEdit = false;
    
    // query string to execute
    var MM_editQuery = "";
    %>
    <%
    // *** Update Record: set variables
    
    if (String(Request("MM_update")) == "form1" &&
        String(Request("MM_recordId")) != "undefined") {
    
      var MM_editConnection = MM_connection_STRING;
      var MM_editTable  = "tblWishList";
      var MM_editColumn = "WishID";
      var MM_recordId = "" + Request.Form("MM_recordId") + "";
      var MM_editRedirectUrl = "myPage.asp";
      var MM_fieldsStr = "i1|value";
      var MM_columnsStr = "Preference|none,none,NULL";
    
      // create the MM_fields and MM_columns arrays
      var MM_fields = MM_fieldsStr.split("|");
      var MM_columns = MM_columnsStr.split("|");
      
      // set the form values
      for (var i=0; i+1 < MM_fields.length; i+=2) {
        MM_fields[i+1] = String(Request.Form(MM_fields[i]));
      }
    
      // append the query string to the redirect URL
      if (MM_editRedirectUrl && Request.QueryString && Request.QueryString.Count > 0) {
        MM_editRedirectUrl += ((MM_editRedirectUrl.indexOf('?') == -1)?"?":"&") + Request.QueryString;
      }
    }
    %>
    <%
    // *** Update Record: construct a sql update statement and execute it
    
    if (String(Request("MM_update")) != "undefined" &&
        String(Request("MM_recordId")) != "undefined") {
    
      // create the sql update statement
      MM_editQuery = "update " + MM_editTable + " set ";
      for (var i=0; i+1 < MM_fields.length; i+=2) {
        var formVal = MM_fields[i+1];
        var MM_typesArray = MM_columns[i+1].split(",");
        var delim =    (MM_typesArray[0] != "none") ? MM_typesArray[0] : "";
        var altVal =   (MM_typesArray[1] != "none") ? MM_typesArray[1] : "";
        var emptyVal = (MM_typesArray[2] != "none") ? MM_typesArray[2] : "";
        if (formVal == "" || formVal == "undefined") {
          formVal = emptyVal;
        } else {
          if (altVal != "") {
            formVal = altVal;
          } else if (delim == "'") { // escape quotes
            formVal = "'" + formVal.replace(/'/g,"''") + "'";
          } else {
            formVal = delim + formVal + delim;
          }
        }
        MM_editQuery += ((i != 0) ? "," : "") + MM_columns[i] + " = " + formVal;
      }
      MM_editQuery += " where " + MM_editColumn + " = " + MM_recordId;
    
      if (!MM_abortEdit) {
        // execute the update
        var MM_editCmd = Server.CreateObject('ADODB.Command');
        MM_editCmd.ActiveConnection = MM_editConnection;
        MM_editCmd.CommandText = MM_editQuery;
        MM_editCmd.Execute();
        MM_editCmd.ActiveConnection.Close();
    
        if (MM_editRedirectUrl) {
          Response.Redirect(MM_editRedirectUrl);
        }
      }
    
    }
    %>
    <%
    var rsWishlist = Server.CreateObject("ADODB.Recordset");
    rsWishlist.ActiveConnection = MM_connection_STRING;
    rsWishlist.Source = "SELECT * FROM tblWishList";
    rsWishlist.CursorType = 0;
    rsWishlist.CursorLocation = 2;
    rsWishlist.LockType = 1;
    rsWishlist.Open();
    var rsWishlist_numRows = 0;
    %>
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body>
    <form ACTION="<%=MM_editAction%>" METHOD="POST" name="form1">
      
      <input name="i1" type="text" id="i1" size="5" maxlength="2">
      <br>
      <br>
      <input type="submit" name="Submit" value="Update">
      <input type="hidden" name="MM_update" value="form1">
      <input type="hidden" name="MM_recordId" value="<%= rsWishlist.Fields.Item("WishID").Value %>">
    </form>
    </body>
    </html>
    <%
    rsWishlist.Close();
    %>
    Now the problem is i dont understand how to edit that code for it to loop through the textboxes and update multiple records, keeping in mind that there wont be a set amount of textboxes, it could be 2,10,15,29 etc

    I would be displaying the textboxes using a repeat region in dreamweaver as follows:

    Code:
    <%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
    <!--#include file="Connections/connection.asp" -->
    <%
    var rsMyRecordset = Server.CreateObject("ADODB.Recordset");
    rsMyRecordset.ActiveConnection = MM_connection_STRING;
    rsMyRecordset.Source = "SELECT * FROM tblWishList";
    rsMyRecordset.CursorType = 0;
    rsMyRecordset.CursorLocation = 2;
    rsMyRecordset.LockType = 1;
    rsMyRecordset.Open();
    var rsMyRecordset_numRows = 0;
    %>
    <%
    var Repeat1__numRows = -1;
    var Repeat1__index = 0;
    rsMyRecordset_numRows += Repeat1__numRows;
    %>
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body>
    <form name="form1">
      <% 
      var counter = 0;
      while ((Repeat1__numRows-- != 0) && (!rsMyRecordset.EOF)) { %>
      <input name="i<%=counter+1%>" type="text" id="i<%=counter+1%>" size="5" maxlength="2">
      <%
      counter ++;
      Repeat1__index++;
      rsMyRecordset.MoveNext();
    }
    %>
      <br>
      <br>
      <input type="submit" name="Submit" value="Update">
    </form>
    </body>
    </html>
    <%
    rsMyRecordset.Close();
    %>
    So i suppose you would probably set up an array inside the repeat region to hold the values of each textbox. Lastly ill include the hand written code i use to update records which might be helpful or might not be but ill include it all the same.

    Code:
    var adoConnection = Server.CreateObject("ADODB.Connection");
    adoConnection.Open("provider=Microsoft.Jet.OLEDB.4.0; data source=c:\\home\\db\\myDatabase.mdb");
    var SQL = "Update tblWishList SET Preference=" + String(Session("myPreference")) + " WHERE Preference= " + String(Session("oldPreference")) + " AND WishID = " + String(Session("myWishIdentity")) + " ";
    
    adoConnection.Execute(SQL);
    adoConnection.Close();
    adoConnection = null;
    So i would really appreciate it if anyone could help me in any way. Also if anyone knows of a good resource for asp and javascript tutorials as everywhere on the web seems to refer only to vbscript (and probably rightly so because asp and javascript is unusual)

    Thanks in advance
    Paul

  • #2
    Regular Coder
    Join Date
    Jul 2004
    Location
    France
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts
    why don't you create an array of textboxes

    for i = 0 to yourCount - 1
    anyWriteMethod = "<input id=""tb" + i .....
    next

    and you can allways find them later with the same loop from 0 to yourCount - 1

    yourvalue = Request.Form("tb" + i)



    and write your code yourself don't ask a machin to do it :-)
    .... in my opinion dreamweaver is just good for turism

  • #3
    New Coder
    Join Date
    Jan 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Good Idea

    Gweendal,

    I kinda understand what your saying, except im no good at writing raw code and that is why i use dreamweaver. However as long as i have an example of a similar code i can usually change it to suit. I would really appreciate it if you could do a small example of the code so i could use it. Request.Form is new to me aswell but thanks as this will prove useful in the future.

    Regarding using Request.Form i wonder whether it will work because after the page is loaded the values of the textboxes can be changed. After 1 or more values are changed the person would click either a submit button or a button to bring them to another page. I would need the values to be stored after clicking a button to save the changes to the textbox rather than when the page loads.

    If i had the values stored in a session variable i could then run the update like this:

    Code:
    <%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
    <!--#include file="Connections/connection.asp" -->
    <%
    var rsMyRecordset = Server.CreateObject("ADODB.Recordset");
    rsMyRecordset.ActiveConnection = MM_connection_STRING;
    rsMyRecordset.Source = "SELECT * FROM tblWishList";
    rsMyRecordset.CursorType = 0;
    rsMyRecordset.CursorLocation = 2;
    rsMyRecordset.LockType = 1;
    rsMyRecordset.Open();
    var rsMyRecordset_numRows = 0;
    %>
    <%
    var Repeat1__numRows = -1;
    var Repeat1__index = 0;
    rsMyRecordset_numRows += Repeat1__numRows;
    %>
    
    <% 
    var counter = 0;
    while ((Repeat1__numRows-- != 0) && (!rsMyRecordset.EOF)) { 
    
    var adoConnection = Server.CreateObject("ADODB.Connection");
    adoConnection.Open("provider=Microsoft.Jet.OLEDB.4.0; data source=c:\\home\\db\\myDatabase.mdb");
    var SQL = "Update tblWishList SET Preference=" + String(Session("myPreference"))+" WHERE Preference= "counter+1" AND WishID = " + String(Session("myWishIdentity")) + " ";
    
    adoConnection.Execute(SQL);
    adoConnection.Close();
    adoConnection = null;
    <%
      counter ++;
      Repeat1__index++;
      rsMyRecordset.MoveNext();
    }
    %>

    Id really appreciate a working example that i could use if you or someone has the time and dont forget the code has to be in server side javascript.

    Thanks ever so much
    Paul

  • #4
    Regular Coder
    Join Date
    Jul 2004
    Location
    France
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i am using only vbscript for asp3
    and vb net for asp net

    sorry

    but a request.form will do the job with a loop on all text boxes


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •