Forums

This topic is locked

Insert Multiple Records in one table at same time

Posted 21 Jan 2005 17:29:40
1
has voted
21 Jan 2005 17:29:40 Ken Ryan posted:
OK. I followed a tutorial that I found on Web Thang www.webthang.co.uk/tuts/tuts_dmx/rob8/rob8.asp. I don't know if I am dense but I can not get it to work. I don't get an error, but it doesn't create the rows or add any info passed from the previos page either. Here is the code. Help me if you can.

<pre id=code><font face=courier size=2 id=code>&lt;!--#include file="../../Connections/connPoints.asp" --&gt;
&lt;%
If Request.Form("insert" = "true" Then

Dim i, currentField

for i = 1 to Request.Form("icount"

currentField = "POINTS" & i

If Request.Form(currentField) &lt;&gt; "" Then

'Insert the points entered

Call sAddPoints(Request.Form("S_ID", Request.Form(currentField))

End If

Next
End If

Sub sAddPoints(pS_ID,pPOINTS)
set cmdSavePoints = Server.CreateObject("ADODB.Command"
cmdSavePoints.ActiveConnection = MM_connPoints_STRING
cmdSavePoints.CommandText = "INSERT INTO POINTS (S_ID, POINTS) VALUES ( " & pS_ID & ", " & pPOINTS & ""
cmdSavePoints.CommandType = 1
cmdSavePoints.CommandTimeout = 0
cmdSavePoints.Prepared = true
cmdSavePoints.Execute()
End Sub
%&gt;

</font id=code></pre id=code>

Ken Ryan



Edited by - kryan on 21 Jan 2005 18:18:50

Replies

Replied 21 Jan 2005 18:13:14
21 Jan 2005 18:13:14 Lee Diggins replied:
Hi Ken

As you're not getting any errors, I'm going to ask some dumb questions!

Does request.form("insert" return the text string true or false?
Is this an option button result - true/false?
What is this - request.form("icount" and what values are passed?
Also do you have all your form elements named the same except for end number item1, item2, item3 etc..?

Edited by - Digga the Wolf on 21 Jan 2005 18:16:44
Replied 21 Jan 2005 18:18:29
21 Jan 2005 18:18:29 Ken Ryan replied:
The request.form is a hidden field from the form that says yes I am submitting some info basically. I took that out and it isn't working either way. The icount is a record count from the previous page that tells the code how many times to loop and add records as it will not be the same everytime.

Yes the form elements are the same. The form elements though are coming in the way of a recordset using repeat region. But even if I only try to post one record it doesn't work. I don't know what I am doing wrong here.

Ken Ryan



Edited by - kryan on 21 Jan 2005 18:20:46
Replied 21 Jan 2005 18:24:47
21 Jan 2005 18:24:47 Lee Diggins replied:
Hi Ken

If you've removed the "insert" form item then you need to remove the IF statement.

The request.form("icount" might work better if you change it to CInt(request.form("icount").

If neither changes above get your pages moving then post the code from both pages (your submission page and your handler) and we'll see what the problem is.

Digga

Sharing Knowledge Saves Valuable Time!!!
Replied 21 Jan 2005 18:41:16
21 Jan 2005 18:41:16 Ken Ryan replied:
Ok, none of that works. I don't know what is going on.

Here is the page that is doing the submitting
<pre id=code><font face=courier size=2 id=code> &lt;!--#include file="../../Connections/connPoints.asp" --&gt;
&lt;%
Dim rsRacers__MMColParam
rsRacers__MMColParam = "1"
if (Request.QueryString("cid" &lt;&gt; "" then rsRacers__MMColParam = Request.QueryString("cid"
%&gt;
&lt;%
set rsRacers = Server.CreateObject("ADODB.Recordset"
rsRacers.ActiveConnection = MM_connPoints_STRING
rsRacers.Source = "SELECT CLASSES.CLASS_ID, CLASSES.C_CLASS, RACERS.R_ID, RACERS.R_NAME FROM CLASSES INNER JOIN RACERS ON CLASSES.CLASS_ID = RACERS.CLASS_ID WHERE CLASSES.CLASS_ID = " + Replace(rsRacers__MMColParam, "'", "''" + ""
rsRacers.CursorType = 0
rsRacers.CursorLocation = 2
rsRacers.LockType = 3
rsRacers.Open()
rsRacers_numRows = 0
%&gt;
&lt;%
Dim rsSched__MMColParam
rsSched__MMColParam = "1"
if (Request.QueryString("sid" &lt;&gt; "" then rsSched__MMColParam = Request.QueryString("sid"
%&gt;
&lt;%
set rsSched = Server.CreateObject("ADODB.Recordset"
rsSched.ActiveConnection = MM_connPoints_STRING
rsSched.Source = "SELECT * FROM SCHED WHERE S_ID = " + Replace(rsSched__MMColParam, "'", "''" + ""
rsSched.CursorType = 0
rsSched.CursorLocation = 2
rsSched.LockType = 3
rsSched.Open()
rsSched_numRows = 0
%&gt;
&lt;%
Dim rsCount__varID
rsCount__varID = "1"
if (Request.Form("cid" &lt;&gt; "" then rsCount__varID = Request.Form("cid"
%&gt;
&lt;%
set rsCount = Server.CreateObject("ADODB.Recordset"
rsCount.ActiveConnection = MM_connPoints_STRING
rsCount.Source = "SELECT Count(*) as icount FROM ClassRacerQuery WHERE CLASS_ID = " + Replace(rsCount__varID, "'", "''" + ""
rsCount.CursorType = 0
rsCount.CursorLocation = 2
rsCount.LockType = 3
rsCount.Open()
rsCount_numRows = 0
%&gt;
&lt;%
Dim Repeat1__numRows
Repeat1__numRows = -1
Dim Repeat1__index
Repeat1__index = 0
rsRacers_numRows = rsRacers_numRows + Repeat1__numRows
%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;/head&gt;
&lt;body bgcolor="#FFFFFF" text="#000000"&gt;
&lt;table width="100%" border="0" cellspacing="0" cellpadding="0"&gt;
&lt;tr&gt;
&lt;td class="bg_catHead"&gt; &nbsp;ADD &lt;font color="#CC0000"&gt;&lt;%= UCase((rsRacers.Fields.Item("C_CLASS".Value)) %&gt;&lt;/font&gt; POINTS for (&lt;%=(rsSched.Fields.Item("S_TITLE".Value)%&gt<img src=../images/dmxzone/forum/icon_smile_wink.gif border=0 align=middle>&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="left" valign="top" colspan="1" class="bg_colorHR"&gt;&lt;img src="/assets/horizontalBar.gif" width="5" height="1"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;&nbsp;&lt;/p&gt;
&lt;form name="form1" action="/admin/points/pointsAdding.asp" method="post"&gt;
&lt;table border="0" cellspacing="3" cellpadding="3"&gt;
&lt;%
While ((Repeat1__numRows &lt;&gt; 0) AND (NOT rsRacers.EOF))
%&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;input type="hidden" name="S_ID" value="&lt;%= Request.QueryString("sid"%&gt;" size="1" maxlength="1"&gt;
&lt;input type="hidden" name="R_ID" value="&lt;%=(rsRacers.Fields.Item("R_ID".Value)%&gt;"&gt;
&lt;input type="hidden" name="insert" value="true"&gt;
&lt;%=(rsRacers.Fields.Item("R_NAME".Value)%&gt;:&lt;/td&gt;
&lt;td&gt;
&lt;input type="text" name="POINTS" class="fields" size="3" maxlength="3"&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;%
Repeat1__index=Repeat1__index+1
Repeat1__numRows=Repeat1__numRows-1
rsRacers.MoveNext()
Wend
%&gt;
&lt;/table&gt;
&lt;br&gt;
&lt;input type="submit" name="Submit" value="Submit" class="buttons"&gt;
&lt;input type="hidden" name="icount" value="&lt;%=(rsCount.Fields.Item("icount".Value)%&gt;"&gt;
&lt;%=(rsCount.Fields.Item("icount".Value)%&gt;
&lt;/form&gt;
&lt;p&gt;&nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;%
rsRacers.Close()
%&gt;
&lt;%
rsSched.Close()
%&gt;
&lt;%
rsCount.Close()
%&gt;
</font id=code></pre id=code>

Here is the handling page.

<pre id=code><font face=courier size=2 id=code>&lt;!--#include file="../../Connections/connPoints.asp" --&gt;
&lt;%

Dim i, currentField

for i = 1 to CInt(request.form("icount")

currentField = "POINTS" & i

If Request.Form(currentField) &lt;&gt; "" Then

'Insert the points entered

Call sAddPoints(Request.Form("S_ID", Request.Form(currentField))

End If

Next

Sub sAddPoints(pS_ID,pPOINTS)
set cmdSavePoints = Server.CreateObject("ADODB.Command"
cmdSavePoints.ActiveConnection = MM_connPoints_STRING
cmdSavePoints.CommandText = "INSERT INTO POINTS (S_ID, POINTS) VALUES ( " & pS_ID & ", " & pPOINTS & ""
cmdSavePoints.CommandType = 1
cmdSavePoints.CommandTimeout = 0
cmdSavePoints.Prepared = true
cmdSavePoints.Execute()
End Sub
%&gt;

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;/head&gt;

&lt;body bgcolor="#FFFFFF" text="#000000"&gt;
&lt;form name="form1" method="post" action=""&gt;
&lt;input type="text" name="POINTS" value="&lt;%= Request.Form("POINTS" %&gt;"&gt;
&lt;input type="text" name="icount" value="&lt;%= Request.Form("icount" %&gt;"&gt;
&lt;input type="text" name="S_ID" value="&lt;%= Request.Form("S_ID" %&gt;"&gt;
&lt;input type="text" name="R_ID" value="&lt;%= Request.Form("R_ID" %&gt;"&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</font id=code></pre id=code>

I even added some form fields to see if the info was passing and it is. I know it is the inster but I do not know where.

Thanks in advance for all of your help.


Ken Ryan

Replied 21 Jan 2005 19:28:36
21 Jan 2005 19:28:36 Ken Ryan replied:
OK. I have figured out the problem. But I don't know how to fix it.

The tutorial I am using requires that you have the form from the previous page static. And the fields have to be named like:

POINTS1
POINTS2

and so on. You can not dynamically put them in with a repeat region. How can I assign a different name to each POINTS field with repeat region. Is this even possible?

I hope this makes sense.

Thanks,

Ken Ryan

Replied 21 Jan 2005 20:10:21
21 Jan 2005 20:10:21 Ken Ryan replied:
I have figured it out. You can name the form fields dynamically. It is crude but it worked. And you have to loop through all of the fields.

Here is the code for the form page:
<pre id=code><font face=courier size=2 id=code>&lt;!--#include file="../../Connections/connPoints.asp" --&gt;
&lt;%
Dim rsRacers__MMColParam
rsRacers__MMColParam = "1"
if (Request.QueryString("cid" &lt;&gt; "" then rsRacers__MMColParam = Request.QueryString("cid"
%&gt;
&lt;%
set rsRacers = Server.CreateObject("ADODB.Recordset"
rsRacers.ActiveConnection = MM_connPoints_STRING
rsRacers.Source = "SELECT CLASSES.CLASS_ID, CLASSES.C_CLASS, RACERS.R_ID, RACERS.R_NAME FROM CLASSES INNER JOIN RACERS ON CLASSES.CLASS_ID = RACERS.CLASS_ID WHERE CLASSES.CLASS_ID = " + Replace(rsRacers__MMColParam, "'", "''" + ""
rsRacers.CursorType = 0
rsRacers.CursorLocation = 2
rsRacers.LockType = 3
rsRacers.Open()
rsRacers_numRows = 0
%&gt;
&lt;%
Dim rsSched__MMColParam
rsSched__MMColParam = "1"
if (Request.QueryString("sid" &lt;&gt; "" then rsSched__MMColParam = Request.QueryString("sid"
%&gt;
&lt;%
set rsSched = Server.CreateObject("ADODB.Recordset"
rsSched.ActiveConnection = MM_connPoints_STRING
rsSched.Source = "SELECT * FROM SCHED WHERE S_ID = " + Replace(rsSched__MMColParam, "'", "''" + ""
rsSched.CursorType = 0
rsSched.CursorLocation = 2
rsSched.LockType = 3
rsSched.Open()
rsSched_numRows = 0
%&gt;
&lt;%
Dim rsCount__varID
rsCount__varID = "1"
if (Request.Form("cid" &lt;&gt; "" then rsCount__varID = Request.Form("cid"
%&gt;
&lt;%
set rsCount = Server.CreateObject("ADODB.Recordset"
rsCount.ActiveConnection = MM_connPoints_STRING
rsCount.Source = "SELECT Count(*) as icount FROM ClassRacerQuery WHERE CLASS_ID = " + Replace(rsCount__varID, "'", "''" + ""
rsCount.CursorType = 0
rsCount.CursorLocation = 2
rsCount.LockType = 3
rsCount.Open()
rsCount_numRows = 0
%&gt;
&lt;%
Dim Repeat1__numRows
Repeat1__numRows = -1
Dim Repeat1__index
Repeat1__index = 0
rsRacers_numRows = rsRacers_numRows + Repeat1__numRows
%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;/head&gt;
&lt;body bgcolor="#FFFFFF" text="#000000"&gt;
&lt;table width="100%" border="0" cellspacing="0" cellpadding="0"&gt;
&lt;tr&gt;
&lt;td class="bg_catHead"&gt; &nbsp;ADD &lt;font color="#CC0000"&gt;&lt;%= UCase((rsRacers.Fields.Item("C_CLASS".Value)) %&gt;&lt;/font&gt; POINTS for (&lt;%=(rsSched.Fields.Item("S_TITLE".Value)%&gt<img src=../images/dmxzone/forum/icon_smile_wink.gif border=0 align=middle>&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="left" valign="top" colspan="1" class="bg_colorHR"&gt;&lt;img src="/assets/horizontalBar.gif" width="5" height="1"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;&nbsp;&lt;/p&gt;
&lt;form name="form1" action="/admin/points/pointsAdding.asp" method="post"&gt;
&lt;table border="0" cellspacing="3" cellpadding="3"&gt;
&lt;%
While ((Repeat1__numRows &lt;&gt; 0) AND (NOT rsRacers.EOF))
%&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;input type="hidden" name="S_ID&lt;%=(rsRacers.Fields.Item("R_ID".Value)%&gt;" value="&lt;%= Request.QueryString("sid"%&gt;" size="1" maxlength="1"&gt;
&lt;input type="hidden" name="R_ID&lt;%=(rsRacers.Fields.Item("R_ID".Value)%&gt;" value="&lt;%=(rsRacers.Fields.Item("R_ID".Value)%&gt;"&gt;
&lt;%=(rsRacers.Fields.Item("R_NAME".Value)%&gt;:&lt;/td&gt;
&lt;td&gt;
&lt;input type="text" name="POINTS&lt;%=(rsRacers.Fields.Item("R_ID".Value)%&gt;" class="fields" size="3" maxlength="3"&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;%
Repeat1__index=Repeat1__index+1
Repeat1__numRows=Repeat1__numRows-1
rsRacers.MoveNext()
Wend
%&gt;
&lt;/table&gt;
&lt;br&gt;
&lt;input type="submit" name="Submit" value="Submit" class="buttons"&gt;
&lt;input type="hidden" name="icount" value="&lt;%=(rsCount.Fields.Item("icount".Value)%&gt;"&gt;
&lt;%=(rsCount.Fields.Item("icount".Value)%&gt;
&lt;/form&gt;
&lt;p&gt;&nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;%
rsRacers.Close()
%&gt;
&lt;%
rsSched.Close()
%&gt;
&lt;%
rsCount.Close()
%&gt;
</font id=code></pre id=code>

Here is the code for the insert page:

<pre id=code><font face=courier size=2 id=code> &lt;!--#include file="../../Connections/connPoints.asp" --&gt;
&lt;%

Dim i, currentField

for i = 1 to Request.Form("icount"

currentField = "POINTS"& i
rField = "R_ID"& i
sField = "S_ID"& i

If Request.Form(currentField) &lt;&gt; "" Then

'Insert the points, S_ID, AND R_ID entered

Call sAddPoints(Request.Form(sField), Request.Form(rField), Request.Form(currentField))

End If

Next

Sub sAddPoints(pS_ID, pR_ID, pPOINTS)
set cmdSavePoints = Server.CreateObject("ADODB.Command"
cmdSavePoints.ActiveConnection = MM_connPoints_STRING
cmdSavePoints.CommandText = "INSERT INTO POINTS (S_ID, R_ID, POINTS) VALUES ( " & pS_ID & ", " & pR_ID & ", " & pPOINTS & ""
cmdSavePoints.CommandType = 1
cmdSavePoints.CommandTimeout = 0
cmdSavePoints.Prepared = true
cmdSavePoints.Execute()
End Sub
%&gt;

</font id=code></pre id=code>

Took a while but what you said got me thinking!!!Appreciate all the help. Hope this helps other folks as well.



Ken Ryan

Replied 21 Jan 2005 20:15:05
21 Jan 2005 20:15:05 Ken Ryan replied:
Notice on form page I added the R_ID value to the end of the name of the form. Same with the POINTS field and S_ID field. This creates the form names dynamically so you can loop thru them on the Insert Page.

Ken Ryan

Replied 21 Jan 2005 21:06:21
21 Jan 2005 21:06:21 Chris Charlton replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>Notice on form page I added the R_ID value to the end of the name of the form. Same with the POINTS field and S_ID field. This creates the form names dynamically so you can loop thru them on the Insert Page.<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>
Sweet deal. After working with DW/UD for a while, you learn what little MM snippets to use/abuse/re-use. Honestly I learned my first days of PHP from watching Code View (PhAKT) every click of the way! <img src=../images/dmxzone/forum/icon_smile_tongue.gif border=0 align=middle> <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>
Replied 21 Jan 2005 21:19:44
21 Jan 2005 21:19:44 Ken Ryan replied:
I have never had the need to do this before. Updating multiple tables/multiple records is no problem. But this has eaten my lunch for several days. I am just glad I had a little help from my friends

Thanks,


Ken Ryan

Replied 21 Jan 2005 22:32:13
21 Jan 2005 22:32:13 Chris Charlton replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>...But this has eaten my lunch for several days...<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>
Welcome to the club! <img src=../images/dmxzone/forum/icon_smile_tongue.gif border=0 align=middle>

<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>I am just glad I had a little help from my friends.<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>
<img src=../images/dmxzone/forum/icon_smile_big.gif border=0 align=middle>
Replied 24 Jan 2005 10:50:00
24 Jan 2005 10:50:00 Lee Diggins replied:
Hi Ken

One of the typical subjects:

<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>Also do you have all your form elements named the same except for end number item1, item2, item3 etc..?
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

Glad you got it fixed.

Digga

Sharing Knowledge Saves Valuable Time!!!

Reply to this topic