+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 16

Thread: INSERT in 2 different tables?

  1. #1
    Contracted Slave Centurion is on a distinguished road Centurion's Avatar
    Join Date
    Dec 2008
    Posts
    533
    Rep Power
    4

    INSERT in 2 different tables?

    Below is an ASP SQL statement:

    Code:
    sql="INSERT INTO bookings (performanceID, customerID, bookingDate)"
    sql=sql & " VALUES "
    sql=sql & "('" & Request.Form("performanceID") & "',"
    sql=sql & "'" & Request.Form("customerID") & "',"
    sql=sql & "'" & Request.Form("bookingDate") & "')"
    
    on error resume next
    conn.Execute sql,recaffected
    
    But straight after this i want to insert "Booked" into the performances table, column name - Status.

    Do i just concatenate the SQL statements?

    Sorry im soooo noobish with my questions!!

    Hope you can help!

  2. #2
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    You can execute multiple SQL statements in one string by using a ; to seperate them

    So, you could have, for example:-
    Code:
    sql="INSERT INTO bookings (performanceID, customerID, bookingDate)"
    sql=sql & " VALUES "
    sql=sql & "('" & Request.Form("performanceID") & "',"
    sql=sql & "'" & Request.Form("customerID") & "',"
    sql=sql & "'" & Request.Form("bookingDate") & "');"
    sql=sql & "INSERT INTO performances(status) VALUES('Booked');"
    
    That will execute both statements.

    Alternatively, I'd suggest using Stored Procedures.

    You should never place form values directly into an SQL statement. Leaves you open for SQL injection attacks. You should validate all the values from your form. Using Stored Procedures would also help prevent injection attacks.

  3. #3
    Contracted Slave Centurion is on a distinguished road Centurion's Avatar
    Join Date
    Dec 2008
    Posts
    533
    Rep Power
    4

    Quote Originally Posted by richyrich View Post
    You can execute multiple SQL statements in one string by using a ; to seperate them

    So, you could have, for example:-
    Code:
    sql="INSERT INTO bookings (performanceID, customerID, bookingDate)"
    sql=sql & " VALUES "
    sql=sql & "('" & Request.Form("performanceID") & "',"
    sql=sql & "'" & Request.Form("customerID") & "',"
    sql=sql & "'" & Request.Form("bookingDate") & "');"
    sql=sql & "INSERT INTO performances(status) VALUES('Booked');"
    
    That will execute both statements.

    Alternatively, I'd suggest using Stored Procedures.

    You should never place form values directly into an SQL statement. Leaves you open for SQL injection attacks. You should validate all the values from your form. Using Stored Procedures would also help prevent injection attacks.
    my page seems to insert the record 5 times when i run that code..

  4. #4
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Is it inside some sort of loop?

    Executing that SQL query itself wouldn't insert 5 records.

  5. #5
    Contracted Slave Centurion is on a distinguished road Centurion's Avatar
    Join Date
    Dec 2008
    Posts
    533
    Rep Power
    4

    Quote Originally Posted by richyrich View Post
    Is it inside some sort of loop?

    Executing that SQL query itself wouldn't insert 5 records.
    Sorry Rich, I do have a Loop but its at the end of the page, didnt think it would matter? Would it be possible to have a quick look at my code?

    Code:
    <html>
    <body>
    
    
    <%
    showName=request.form("showName")
    performanceID=request.form("performanceID")
    performance_time=request.form("performance_time")
    email=request.form("email")
    
    DIM conn
    
    set conn=Server.CreateObject("ADODB.Connection")
    conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0"
    conn.open server.mappath("../db/database.mdb")
    
    set rs = Server.CreateObject("ADODB.recordset")
    'validating the values on the form
    sql= "SELECT shows.[showID], shows.[showName], performances.[performance_time] FROM shows INNER JOIN performances ON shows.[showID] = performances.[showID] WHERE shows.[showName]='"& showName & "' AND performances.[performance_time]='"&performance_time & "'"
    rs.Open sql, conn
    
    'response.write sql
    
    if rs.EOF Then
    Response.Write "<h4 >Oops! No records found!</h4>" 
    
    else 
    do while not rs.EOF
    %>
    
    <%
    sql="INSERT INTO bookings (performanceID, customerID, bookingDate)"
    sql=sql & " VALUES "
    sql=sql & "('" & Request.Form("performanceID") & "',"
    sql=sql & "'" & Request.Form("customerID") & "',"
    sql=sql & "'" & Request.Form("bookingDate") & "');"
    sql=sql & "INSERT INTO performances(Status) VALUES('Booked');"
    
    on error resume next
    conn.Execute sql,recaffected 
    
    'debug the sql statement
    response.write sql
    
    
    'create a new recordset to hold the new data, as we need to run a seperate query to find the bookingID
    
    set rs2 = Server.CreateObject("ADODB.recordset")
    sql2 = "SELECT @@Identity"
    set rs2 = conn.execute(sql2)
    bookingID = "Your Booking Number is: " & rs2(0)
    response.write rs2(0)
    
    if err<>0 then
      Response.Write("No update permissions!")
    else
      Response.Write("<h3>" & recaffected & " record added and email sent</h3>")
    end if
    %>
    
    <%
    rs.movenext
    											
    loop
    
    rs.close
    rs2.close
    conn.close
    end if
    %>
    
    </body>
    </html>
    

  6. #6
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Hmmm...I can only presume this query
    Code:
    sql= "SELECT shows.[showID], shows.[showName], performances.[performance_time] FROM shows INNER JOIN performances ON shows.[showID] = performances.[showID] WHERE shows.[showName]='"& showName & "' AND performances.[performance_time]='"&performance_time & "'"
    
    is returning 5 records. I also presume, it should only produce 1 based on whatever performance they're booking for? I'm not entirely sure what this process should be doing. Perhaps you could elaborate? Could it return 5 records and why would it?

  7. #7
    Contracted Slave Centurion is on a distinguished road Centurion's Avatar
    Join Date
    Dec 2008
    Posts
    533
    Rep Power
    4

    Quote Originally Posted by richyrich View Post
    Hmmm...I can only presume this query
    Code:
    sql= "SELECT shows.[showID], shows.[showName], performances.[performance_time] FROM shows INNER JOIN performances ON shows.[showID] = performances.[showID] WHERE shows.[showName]='"& showName & "' AND performances.[performance_time]='"&performance_time & "'"
    
    is returning 5 records. I also presume, it should only produce 1 based on whatever performance they're booking for? I'm not entirely sure what this process should be doing. Perhaps you could elaborate? Could it return 5 records and why would it?
    the idea is check if the data is valid on the form.. if it is, then proceed to inserting the data.

  8. #8
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Yep, I get that bit.

    So, in what circumstance would it return more than one record. Surely it should be checking for a specific performance at a specific time, of which they'd only be one, wouldn't there?

    I'm not sure I understand the reason for it being in a loop.

  9. #9
    Contracted Slave Centurion is on a distinguished road Centurion's Avatar
    Join Date
    Dec 2008
    Posts
    533
    Rep Power
    4

    Quote Originally Posted by richyrich View Post
    Yep, I get that bit.

    So, in what circumstance would it return more than one record. Surely it should be checking for a specific performance at a specific time, of which they'd only be one, wouldn't there?

    I'm not sure I understand the reason for it being in a loop.
    nor do i...

    so the best thing to do is, remove the loop and run the query on its own.
    then my inserts should work fine rite?

    my dev server has gone down... fast2host say all windows servers are down!

  10. #10
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    I think the best thing to do is work out why the query is returning more than 1 record as that's the only reason I can see why you'd get 5 inserts.

    Even if you kept the loop, it would only loop through 1 record then.

    Are my presumptions on the performance correct? Should it only be 1 record?

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. Simple Insert Page Help?
    By Centurion in forum ASP Development
    Replies: 7
    Last Post: October 20th, 2009, 10:51 AM
  2. Bulk Insert with other requirements..
    By yangski in forum Microsoft SQL Server
    Replies: 2
    Last Post: August 12th, 2009, 08:55 PM
  3. How to insert variable html into php
    By evdv in forum PHP Development
    Replies: 1
    Last Post: January 14th, 2009, 08:54 PM
  4. Help with handling sql insert error
    By Rebelle in forum ASP Development
    Replies: 11
    Last Post: August 29th, 2008, 02:20 PM
  5. can not insert duplicate key
    By guddu in forum Microsoft SQL Server
    Replies: 1
    Last Post: August 20th, 2008, 10:27 AM

Bookmarks

Posting Permissions

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

SEO by vBSEO