Forums
This topic is locked
Insert in two tables with the same ID
28 Nov 2005 15:52:16 Tom Theys posted:
Hi, I've been able to add data into two tables in my database. But now I need to know how I insert de second table with the same ID. I know how to do this when I load the insert page with a requested ID, but how do I do this when I loading the insert page without page that first requests the ID. So when I make a hidden field in my form I need to request the ID of the current post I'm gonna post.Here's the DB
tabPost
- PostID (PK, Autonr)
- PostSubject (text)
- PostMessage (memo)
tabImages
- ImageID (PK, Autonr)
- ImageSource (text)
- PostID (nr)
here's the code from the insert.asp
<pre id=code><font face=courier size=2 id=code>
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="Connections/multiasp.asp" -->
<!--#include file="ScriptLibrary/incPureUpload.asp" -->
<%
'*** Pure ASP File Upload 2.1.9
Dim GP_uploadAction,UploadQueryString
PureUploadSetup
If (CStr(Request.QueryString("GP_upload") <> "" Then
Dim pau_thePath,pau_Extensions,pau_Form,pau_Redirect,pau_storeType,pau_sizeLimit,pau_nameConflict,pau_requireUpload,pau_minWidth,pau_minHeight,pau_maxWidth,pau_maxHeight,pau_saveWidth,pau_saveHeight,pau_timeout,pau_progressBar,pau_progressWidth,pau_progressHeight
pau_thePath = """images"""
pau_Extensions = ""
pau_Form = "form1"
pau_Redirect = ""
pau_storeType = "path"
pau_sizeLimit = ""
pau_nameConflict = "uniq"
pau_requireUpload = "false"
pau_minWidth = ""
pau_minHeight = ""
pau_maxWidth = ""
pau_maxHeight = ""
pau_saveWidth = ""
pau_saveHeight = ""
pau_timeout = "600"
pau_progressBar = ""
pau_progressWidth = ""
pau_progressHeight = ""
Dim RequestBin, UploadRequest
CheckPureUploadVersion 2.19
ProcessUpload pau_thePath,pau_Extensions,pau_Redirect,pau_storeType,pau_sizeLimit,pau_nameConflict,pau_requireUpload,pau_minWidth,pau_minHeight,pau_maxWidth,pau_maxHeight,pau_saveWidth,pau_saveHeight,pau_timeout
end if
%>
<%
' *** Edit Operations: (Modified for File Upload) declare variables
Dim MM_editAction
Dim MM_abortEdit
Dim MM_editQuery
Dim MM_editCmd
Dim MM_editConnection
Dim MM_editTable
Dim MM_editRedirectUrl
Dim MM_editColumn
Dim MM_recordId
Dim MM_fieldsStr
Dim MM_columnsStr
Dim MM_fields
Dim MM_columns
Dim MM_typeArray
Dim MM_formVal
Dim MM_delim
Dim MM_altVal
Dim MM_emptyVal
Dim MM_i
Dim MM_typeArray2
Dim formVal
Dim delim
Dim altVal
Dim emptyVal
Dim i
MM_editAction = CStr(Request.ServerVariables("SCRIPT_NAME")
If (UploadQueryString <> "" Then
MM_editAction = MM_editAction & "?" & Server.HTMLEncode(UploadQueryString)
End If
' boolean to abort record edit
MM_abortEdit = false
' query string to execute
MM_editQuery = ""
%>
<%
' *** Insert Record: (Modified for File Upload) set variables
If (CStr(UploadFormRequest("MM_insert") = "form1" Then
' INSERT SECOND TABLE
'MM_editConnection = MM_multiasp_STRING
MM_editTable2 = "tabImages"
'MM_editRedirectUrl = "view.asp"
MM_fieldsStr2 = "imageSource|value|PostID|value"
MM_columnsStr2 = "ImageSource|',none,''|PostID|none,none,NULL"
' create the MM_fields and MM_columns arrays
MM_fields2 = Split(MM_fieldsStr2, "|"
MM_columns2 = Split(MM_columnsStr2, "|"
' set the form values
For i = LBound(MM_fields2) To UBound(MM_fields2) Step 2
MM_fields2(i+1) = CStr(UploadFormRequest(MM_fields2(i)))
Next
'END INSERT SECOND TABLE
MM_editConnection = MM_multiasp_STRING
MM_editTable = "tabPost"
MM_editRedirectUrl = "view.asp"
MM_fieldsStr = "postSubject|value|postText|value"
MM_columnsStr = "postSubject|',none,''|PostText|',none,''"
' create the MM_fields and MM_columns arrays
MM_fields = Split(MM_fieldsStr, "|"
MM_columns = Split(MM_columnsStr, "|"
' set the form values
For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_fields(MM_i+1) = CStr(UploadFormRequest(MM_fields(MM_i)))
Next
' append the query string to the redirect URL
If (MM_editRedirectUrl <> "" And UploadQueryString <> "" Then
If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0 And UploadQueryString <> "" Then
MM_editRedirectUrl = MM_editRedirectUrl & "?" & UploadQueryString
Else
MM_editRedirectUrl = MM_editRedirectUrl & "&" & UploadQueryString
End If
End If
End If
%>
<%
' *** Insert Record: (Modified for File Upload) construct a sql insert statement and execute it
Dim MM_tableValues
Dim MM_dbValues
Dim MM_tableValues2
Dim MM_dbValues2
If (CStr(UploadFormRequest("MM_insert") <> "" Then
' create the sql insert statement
MM_tableValues = ""
MM_dbValues = ""
For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_formVal = MM_fields(MM_i+1)
MM_typeArray = Split(MM_columns(MM_i+1),","
MM_delim = MM_typeArray(0)
If (MM_delim = "none" Then MM_delim = ""
MM_altVal = MM_typeArray(1)
If (MM_altVal = "none" Then MM_altVal = ""
MM_emptyVal = MM_typeArray(2)
If (MM_emptyVal = "none" Then MM_emptyVal = ""
If (MM_formVal = "" Then
MM_formVal = MM_emptyVal
Else
If (MM_altVal <> "" Then
MM_formVal = MM_altVal
ElseIf (MM_delim = "'" Then ' escape quotes
MM_formVal = "'" & Replace(MM_formVal,"'","''" & "'"
Else
MM_formVal = MM_delim + MM_formVal + MM_delim
End If
End If
If (MM_i <> LBound(MM_fields)) Then
MM_tableValues = MM_tableValues & ","
MM_dbValues = MM_dbValues & ","
End If
MM_tableValues = MM_tableValues & MM_columns(MM_i)
MM_dbValues = MM_dbValues & MM_formVal
Next
MM_editQuery = "insert into " & MM_editTable & " (" & MM_tableValues & " values (" & MM_dbValues & ""
' create the sql insert statement
MM_tableValues2 = ""
MM_dbValues2 = ""
For i = LBound(MM_fields2) To UBound(MM_fields2) Step 2
formVal = MM_fields2(i+1)
MM_typeArray2 = Split(MM_columns2(i+1),","
delim = MM_typeArray2(0)
If (delim = "none" Then delim = ""
altVal = MM_typeArray2(1)
If (altVal = "none" Then altVal = ""
emptyVal = MM_typeArray2(2)
If (emptyVal = "none" Then emptyVal = ""
If (formVal = "" Then
formVal = emptyVal
Else
If (altVal <> "" Then
formVal = ltVal
ElseIf (delim = "'" Then ' escape quotes
formVal = "'" & Replace(formVal,"'","''" & "'"
Else
formVal = delim + formVal + delim
End If
End If
If (i <> LBound(MM_fields2)) Then
MM_tableValues2 = MM_tableValues2 & ","
MM_dbValues2 = MM_dbValues2 & ","
End If
MM_tableValues2 = MM_tableValues2 & MM_columns2(i)
MM_dbValues2 = MM_dbValues2& formVal
Next
MM_editQuery2 = "insert into " & MM_editTable2 & " (" & MM_tableValues2 & " values (" & MM_dbValues2 & ""
If (Not MM_abortEdit) Then
' execute the insert
Set MM_editCmd = Server.CreateObject("ADODB.Command"
MM_editCmd.ActiveConnection = MM_editConnection
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
' Code for second insert execution
MM_editCmd.CommandText = MM_editQuery2
MM_editCmd.Execute
' End of code for second insert execution
MM_editCmd.ActiveConnection.Close
If (MM_editRedirectUrl <> "" Then
Response.Redirect(MM_editRedirectUrl)
End If
End If
End If
%>
<%
Dim rsPost
Dim rsPost_numRows
Set rsPost = Server.CreateObject("ADODB.Recordset"
rsPost.ActiveConnection = MM_multiasp_STRING
rsPost.Source = "SELECT * FROM tabPost"
rsPost.CursorType = 0
rsPost.CursorLocation = 2
rsPost.LockType = 1
rsPost.Open()
rsPost_numRows = 0
%>
<%
Dim rsImages
Dim rsImages_numRows
Set rsImages = Server.CreateObject("ADODB.Recordset"
rsImages.ActiveConnection = MM_multiasp_STRING
rsImages.Source = "SELECT * FROM tabImages"
rsImages.CursorType = 0
rsImages.CursorLocation = 2
rsImages.LockType = 1
rsImages.Open()
rsImages_numRows = 0
%>
<html>
<head>
<title>INSERT PAGE</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript" src="ScriptLibrary/incPureUpload.js"></script>
</head>
<body>
<form ACTION="<%=MM_editAction%>" METHOD="post" ENCTYPE="multipart/form-data" name="form1" onSubmit="checkFileUpload(this,'',false,'','','','','','','');return document.MM_returnValue">
Subject<br>
<input name="postSubject" type="text" id="postSubject" size="60">
<br>
Text<br>
<textarea name="postText" cols="60" rows="5" id="postText"></textarea>
<br>
<br>
image 01<br>
<input name="imageSource" type="file" id="imageSource" onChange="checkOneFileUpload(this,'',false,'','','','','','','')" size="60">
<br>
<br>
<input type="submit" name="Submit" value="ADD POST">
<br>
<input name="PostID" type="hidden" id="PostID" value="<%=(rsPost.Fields.Item("postID".Value)%>">
<input type="hidden" name="MM_insert" value="form1">
</form>
</body>
</html>
<%
rsPost.Close()
Set rsPost = Nothing
%>
<%
rsImages.Close()
Set rsImages = Nothing
%>
</font id=code></pre id=code>
Everything works, the data gets inserted in the two tables (tabPost, tabImages) but the PostID in the tabImages table are always 0 or nothing, it needs to be the same as the new PostID
I used this tutorial from DW www.macromedia.com/cfusion/knowledgebase/index.cfm?id=tn_15535
hope somebody can help me
greetz, Tom
Replies
Replied 03 Dec 2005 17:02:07
03 Dec 2005 17:02:07 Tom Theys replied:
Ok, found a better way.
Need some help. I'm want to retriev the @@IDENTITY for the corr table, but I'm getting the following error:
ADODB.Recordset error '800a0cc1'
Item cannot be found in the collection corresponding to the requested name or ordinal.
/MultiASP/insertc.asp, line 27
Here's the code, the line in bold is where I get the error
<pre id=code><font face=courier size=2 id=code><% If Request("SubmitButton" <> "" Then %>
<%
Dim postSubject
Dim postText
Dim cInsert
postSubject = Request.form("postSubject"
postText = Request.Form("PostText"
set cInsert = Server.CreateObject("ADODB.Command"
cInsert.ActiveConnection = MM_multiasp_STRING
cInsert.CommandText = "INSERT INTO tabPost (postSubject, PostText) VALUES ('" & postSubject & "','" & postText & "')"
Dim rsIdentity
Set rsIdentity = cInsert.Execute("SELECT @@IDENTITY FROM tabPost"
Dim iCurrentValue
<b>iCurrentValue = rsIdentity(0)</b>
rsIdentity.Close
Set rsIdentity = Nothing
cInsert.Close
Set cInsert = Nothing
%>
<%
Dim imageSource
Dim cInsert2
imageSource = Request.Form("ImageSource"
set cInsert2 = Server.CreateObject("ADODB.Command"
cInsert2.ActiveConnection = MM_multiasp_STRING
cInsert2.CommandText = "INSERT INTO tabImages (ImageSource, PostID) VALUES ('" & imageSource & "'," & iCurrentValue & ""
cInsert2.CommandType = 1
cInsert2.CommandTimeout = 0
cInsert2.Prepared = true
cInsert2.Execute()
%>
<% End If %></font id=code></pre id=code>
Need some help. I'm want to retriev the @@IDENTITY for the corr table, but I'm getting the following error:
ADODB.Recordset error '800a0cc1'
Item cannot be found in the collection corresponding to the requested name or ordinal.
/MultiASP/insertc.asp, line 27
Here's the code, the line in bold is where I get the error
<pre id=code><font face=courier size=2 id=code><% If Request("SubmitButton" <> "" Then %>
<%
Dim postSubject
Dim postText
Dim cInsert
postSubject = Request.form("postSubject"
postText = Request.Form("PostText"
set cInsert = Server.CreateObject("ADODB.Command"
cInsert.ActiveConnection = MM_multiasp_STRING
cInsert.CommandText = "INSERT INTO tabPost (postSubject, PostText) VALUES ('" & postSubject & "','" & postText & "')"
Dim rsIdentity
Set rsIdentity = cInsert.Execute("SELECT @@IDENTITY FROM tabPost"
Dim iCurrentValue
<b>iCurrentValue = rsIdentity(0)</b>
rsIdentity.Close
Set rsIdentity = Nothing
cInsert.Close
Set cInsert = Nothing
%>
<%
Dim imageSource
Dim cInsert2
imageSource = Request.Form("ImageSource"
set cInsert2 = Server.CreateObject("ADODB.Command"
cInsert2.ActiveConnection = MM_multiasp_STRING
cInsert2.CommandText = "INSERT INTO tabImages (ImageSource, PostID) VALUES ('" & imageSource & "'," & iCurrentValue & ""
cInsert2.CommandType = 1
cInsert2.CommandTimeout = 0
cInsert2.Prepared = true
cInsert2.Execute()
%>
<% End If %></font id=code></pre id=code>