|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
My god, there are a lot of trouble ppl out here. I'm glad I'm not alone!
Anyway, as the Thread indicates, I seem to be haing truble with my SQL statements when it comes to INSERTING or ADDing information to a database that have a Parent table and a few child tables. Getting the Info and displaying it is OK, but I can't figure out why I can't add, update, or delete. If anyone could take a look at my code here and correnct me on my SQL, I'd be greatly appreciative. ----HTML Form that sends the inifo to the ASP page to process---- <HTML> <BODY text="#FFFF00" bgcolor="#800000"> <FORM NAME="frmAddMovie" ACTION="add.asp" METHOD="post"> <TABLE BORDER=0> <TR> <TH ALIGN="LEFT">Movie Name:</TH> <TD><INPUT TYPE="textbox" maxLength="20" NAME="txtMovieName" SIZE="25" VALUE=""></TD> </TR> <TR> <TH ALIGN="LEFT">Genre:</TH> <TD><SELECT NAME="optGenre"> <OPTION VALUE="0"></OPTION> <OPTION VALUE="1">Action</OPTION> <OPTION VALUE="2">Action/Adventure</OPTION> <OPTION VALUE="3">Adult (XXX)</OPTION> <OPTION VALUE="4">Animation (Classic Style)</OPTION> <OPTION VALUE="5">Animation (Modern Style)</OPTION> <OPTION VALUE="6">Comedy</OPTION> <OPTION VALUE="7">Drama</OPTION> <OPTION VALUE="8">Fantasy</OPTION> <OPTION VALUE="9">Horror</OPTION> <OPTION VALUE="10">JapanAmation</OPTION> <OPTION VALUE="11">Martial Arts</OPTION> <OPTION VALUE="12">Mystery</OPTION> <OPTION VALUE="13">Romance</OPTION> <OPTION VALUE="14">Romantic Comedy</OPTION> <OPTION VALUE="15">Science Fiction</OPTION> <OPTION VALUE="16">Spy Film</OPTION> <OPTION VALUE="17">Thriller/Suspense</OPTION> <OPTION VALUE="18">War</OPTION> <OPTION VALUE="19">Western</OPTION> </SELECT> </TD> </TR> <TR> <TH ALIGN="LEFT">Director:</TH> <TD> <INPUT TYPE="textbox" maxLength=30 NAME="txTDirector" SIZE="25" VALUE=""></TD> </TR> <TR> <TH ALIGN="LEFT">Year Released:</TH> <TD> <INPUT TYPE="textbox" maxLength=20 NAME="txtYearReleased" SIZE="25" VALUE=""></TD> </TR> <TR> <TH ALIGN="LEFT">Length:</TH> <TD> <INPUT TYPE="textbox" maxLength=20 NAME="txtLength" SIZE="25" VALUE=""></TD> </TR> <TR> <TH ALIGN="LEFT">Actor First Name:</TH> <TD> <INPUT TYPE="textbox" maxLength=20 NAME="txtFname" SIZE="25" VALUE=""></TD> </TR> <TR> <TH ALIGN="LEFT">Actor Last Name:</TH> <TD> <INPUT TYPE="textbox" maxLength=20 NAME="txtLname" SIZE="25" VALUE=""></TD> </TR> <TR> <TH ALIGN="LEFT">Rating:</TH> <TD><SELECT NAME="optRating"> <OPTION VALUE="0"></OPTION> <OPTION VALUE="1">G (General)</OPTION> <OPTION VALUE="2">PG (Parental Guidance)</OPTION> <OPTION VALUE="3">PG-13</OPTION> <OPTION VALUE="4">14A</OPTION> <OPTION VALUE="5">18A</OPTION> <OPTION VALUE="6">R (Restricted)</OPTION> <OPTION VALUE="7">XXX (Adult)</OPTION> </SELECT> </TD> </TR> <TR> <TH ALIGN="LEFT">Review:</TH> </TR> <TR> <TD COLSPAN="2"><TEXTAREA NAME="txtReview" COLS="35" ROWS="10"></TEXTAREA></TD> </TR> <TR> <TH ALIGN="LEFT">Codec:</TH> <TD><SELECT NAME="optCodec"> <OPTION VALUE="0"></OPTION> <OPTION VALUE="1">DivX</OPTION> <OPTION VALUE="2">XviD</OPTION> </SELECT> </TD> </TR> <TR> <TH ALIGN="LEFT">CDs:</TH> <TD> <INPUT TYPE="textbox" maxLength=20 NAME="txtCDs" SIZE="25" VALUE=""></TD> </TR> <TR> <TH ALIGN="LEFT">Notes:</TH> <TD> <INPUT TYPE="textbox" maxLength=20 NAME="txtNotes" SIZE="25" VALUE=""></TD> </TR> <TR> </TR> <TR> </TR> <TR> </TR> <TR> <TD ALIGN="middle" COLSPAN="2"><INPUT TYPE="submit" VALUE="Add Record" NAME="btnSubmit"><INPUT TYPE="reset" VALUE="Reset Form" NAME="btnReset"></TD> </TR> </TABLE> </FORM> </BODY> </HTML> ----ASP page to process the info recieved from HTML page---- <% 'Dimension variables Dim conn 'Holds the Database Connection Object Dim rsAddMovie 'Holds the recordset for the new record to be added Dim strSQL 'Holds the SQL query to query the database %> <% 'Create an ADO connection object Set conn = Server.CreateObject("ADODB.Connection") %> <% 'Set an active connection to the Connection object using a DSN-less connection conn.Open "DSN=movie" %> <% 'Create an ADO recordset object Set rsAddMovie = Server.CreateObject("ADODB.Recordset") %> <% 'Initialise the strSQL variable with an SQL statement to query the database strSQL = "INSERT INTO tblMovies, tblActors (MovieName, GenreID, Direcector, YearReleased, Length, Fname, Lname, RatingID, Review, CodecID, CDs, Notes)" strSQL = strSQL & " VALUES ('" & request.form("txtMovieName") & "'," & request.form("optGenre") & ",'" & request.form("txtDirector") & "','" & request.form("txtYearReleased") & "','" & request.form("txtLength") & "','" & request.form("txtFname") & "','" & request.form("txtLname") & "'," & request.form("optRating") & ",'" & request.form("txtReview") & "'," & request.form("optCodec") & ",'" & request.form("txtCds") & "','" & request.form("txtNotes") & "')" conn.Execute (strSQL) %> <% 'Set the cursor type we are using so we can navigate through the recordset rsAddMovie.CursorType = 2 %> <% 'Set the lock type so that the record is locked by ADO when it is updated rsAddMovie.LockType = 3 %> <% 'Open the recordset with the SQL query rsAddMovie.Open strSQL, conn %> <% 'Tell the recordset we are adding a new record to it rsAddMovie.AddNew %> <% 'Add a new record to the recordset rsAddMovie.Fields("MovieName") = Request.Form("txtMovieName") rsAddMovie.Fields("GenreID") = Request.Form("optGenre") rsAddMovie.Fields("Director") = Request.Form("txtDirector") rsAddMovie.Fields("YearReleased") = Request.Form("txtYearReleased") rsAddMovie.Fields("Length") = Request.Form("txtLength") rsAddMovie.Fields("Fname") = Request.Form("txtFname") rsAddMovie.Fields("Lname") = Request.Form("txtLname") rsAddMovie.Fields("RatingID") = Request.Form("optRating") rsAddMovie.Fields("Review") = Request.Form("txtReview") rsAddMovie.Fields("CodecID") = Request.Form("optCodec") rsAddMovie.Fields("CDs") = Request.Form("txtCds") rsAddMovie.Fields("Notes") = Request.Form("txtNotes") %> <% 'Write the updated recordset to the database rsAddMovie.Update %> <% 'Reset server objects rsAddMovie.Close Set rsAddMovie = Nothing Set conn = Nothing %> <% 'Redirect to the movie.asp page Response.Redirect "movie.asp" %> |
|
#2
|
||||
|
||||
|
In a parent/child relationship you must always enter data into the parent table first, then the child table.
|
|
#3
|
||||
|
||||
|
Thanks Memnoch. I rearranged my Field Names in the SQL so the Fname and the Lname from the Actors table would come last both in the INSERT INTO and the VALUES. I even rearranged it on the HTML form. Still no go. I keep getting the:
Error Type: Microsoft OLE DB Provider for ODBC Drivers (0x80040E14) [Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement. /add.asp, line 27 Here's a copy of the ASP page. again, but changed SQL if anyone won't kind helpping out. ---------------------------------ASP Page-------------------------------------- <% 'Dimension variables Dim conn 'Holds the Database Connection Object Dim rsAddMovie 'Holds the recordset for the new record to be added Dim strSQL 'Holds the SQL query to query the database %> <% 'Create an ADO connection object Set conn = Server.CreateObject("ADODB.Connection") %> <% 'Set an active connection to the Connection object using a DSN-less connection conn.Open "DSN=movie" %> <% 'Create an ADO recordset object Set rsAddMovie = Server.CreateObject("ADODB.Recordset") %> <% 'Initialise the strSQL variable with an SQL statement to query the database strSQL = "INSERT INTO tblMovies, tblActors (MovieName, GenreID, Director, YearReleased, Length, RatingID, Review, CodecID, CDs, Notes, Fname, Lname)" strSQL = strSQL & " VALUES ('" & request.form("txtMovieName") & "'," & request.form("optGenre") & ",'" & request.form("txtDirector") & "','" & request.form("txtYearReleased") & "','" & request.form("txtLength") & "'," & request.form("optRating") & ",'" & request.form("txtReview") & "'," & request.form("optCodec") & ",'" & request.form("txtCds") & "','" & request.form("txtNotes") & "','" & request.form("txtFname") & "','" & request.form("txtLname") & "')" conn.Execute (strSQL) %> <% 'Set the cursor type we are using so we can navigate through the recordset rsAddMovie.CursorType = 2 %> <% 'Set the lock type so that the record is locked by ADO when it is updated rsAddMovie.LockType = 3 %> <% 'Open the recordset with the SQL query rsAddMovie.Open strSQL, conn %> <% 'Tell the recordset we are adding a new record to it rsAddMovie.AddNew %> <% 'Add a new record to the recordset rsAddMovie.Fields("MovieName") = Request.Form("txtMovieName") rsAddMovie.Fields("GenreID") = Request.Form("optGenre") rsAddMovie.Fields("Director") = Request.Form("txtDirector") rsAddMovie.Fields("YearReleased") = Request.Form("txtYearReleased") rsAddMovie.Fields("Length") = Request.Form("txtLength") rsAddMovie.Fields("RatingID") = Request.Form("optRating") rsAddMovie.Fields("Review") = Request.Form("txtReview") rsAddMovie.Fields("CodecID") = Request.Form("optCodec") rsAddMovie.Fields("CDs") = Request.Form("txtCds") rsAddMovie.Fields("Notes") = Request.Form("txtNotes") rsAddMovie.Fields("Fname") = Request.Form("txtFname") rsAddMovie.Fields("Lname") = Request.Form("txtLname") %> <% 'Write the updated recordset to the database rsAddMovie.Update %> <% 'Reset server objects rsAddMovie.Close Set rsAddMovie = Nothing Set conn = Nothing %> <% 'Redirect to the movie.asp page Response.Redirect "movie.asp" %> |
|
#4
|
||||
|
||||
|
don't insert into both tables at the same time...insert into the first table, then insert into the second table, with 2 different insert sql statements.
|
![]() |
| Viewing: ASP Free Forums > Database > SQL Development > Multiple Tables |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|