#1
  1. No Profile Picture
    Contributing User
    ASP Discoverer (100 - 499 posts)

    Join Date
    May 2005
    Posts
    342
    Rep Power
    42

    Loop(?) and insert in database


    Hi All ^^

    I am trying to create a multi upload image form and I am kinda confused in the database part of it. I have a form with 3 file browser as below (this is just an example):

    Code:
    <form method="POST" enctype="multipart/form-data" action="lalal.asp">
    	<p><input type="file" name="F1" size="20"><br>
    	<br>
    	<input type="file" name="F2" size="20"><br>
    	<br>
    	<input type="file" name="F3" size="20"></p>
    </form>
    and than the lalal.asp has the insert command, but with this code, I need 3 columns in the database:
    Code:
    <%
    Dim data_source, con, sql_insert, f1, f2, f3
    
    f1 = Request.Form("f1")
    f2 = Request.Form("f2")
    f3 = Request.Form("f3")
    
    data_source = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=servername; DATABASE=abc; UID=abc; PWD=abc; OPTION=35;"
    
    sql_insert = "insert into tablename (f1, f2, f3) values ('" & f1 & "', '" & f2 & "', '" & f3 & "')"
    
    Set con = Server.CreateObject("ADODB.Connection")
    con.Open data_source
    con.Execute sql_insert
    
    con.Close
    Set con = Nothing
    %>
    this insert data in this way:

    id f1 f2 f3
    1 a.jpg b.jpg c.jpg


    so the way I am trying to insert data is in this way:

    id file
    1 a.jpg
    2 b.jpg
    3 c.jpg


    but I dunno how to do this way of insert. Can someone give me some light?

    Thanks in advanced for any info on this.
  2. #2
  3. Contributing User
    ASP High Scholar (3500 - 3999 posts)

    Join Date
    Mar 2006
    Location
    South Wales
    Posts
    3,674
    Rep Power
    2046
    If I understand you correctly: Assuming your table has two columns ID and FileName, you could simply have three insert statements:
    Code:
    <%
    Dim data_source, con, sql_insert, f1, f2, f3
    
    f1 = Request.Form("f1")
    f2 = Request.Form("f2")
    f3 = Request.Form("f3")
    
    data_source = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=servername; DATABASE=abc; UID=abc; PWD=abc; OPTION=35;"
    
    sql_insert = "insert into tablename (FileName) values ('" & f1 & "')"
    con.Execute(sql_insert)
    sql_insert = "insert into tablename (FileName) values ('" & f2 & "')"
    con.Execute(sql_insert)
    sql_insert = "insert into tablename (FileName) values ('" & f3 & "')"
    con.Execute(sql_insert)
    
    con.Close
    Set con = Nothing
    %>
  4. #3
  5. Contributing User
    ASP High Scholar (3500 - 3999 posts)

    Join Date
    Mar 2006
    Location
    South Wales
    Posts
    3,674
    Rep Power
    2046
    Sorry, that was a bit clunky. The best way would probably be to loop through all elements in the forms collection:
    Code:
    <%
    Dim data_source, con, sql_insert, f1, f2, f3
    data_source = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=servername; DATABASE=abc; UID=abc; PWD=abc; OPTION=35;"
    
    For x = 1 to Request.Form.Count
        If Left(Request.Form.Key(x), 1) = "f" Then
             sql_insert = "insert into tablename (FileName) values ('" & Request.Form.Item(x)  & "')"
             con.Execute(sql_insert)
        End If
    Next 
    con.Close
    Set con = Nothing
    %>
  6. #4
  7. No Profile Picture
    Contributing User
    ASP Discoverer (100 - 499 posts)

    Join Date
    May 2005
    Posts
    342
    Rep Power
    42
    [edit]
    adding
    Code:
    Set con = Server.CreateObject("ADODB.Connection")
    con.Open data_source
    before the first con.Execute(sql_insert) solves the situation. This is the best way?

    Thanks again dude ^^
    [/edit]

    Originally Posted by sync_or_swim
    If I understand you correctly: Assuming your table has two columns ID and FileName, you could simply have three insert statements:
    Code:
    <%
    Dim data_source, con, sql_insert, f1, f2, f3
    
    f1 = Request.Form("f1")
    f2 = Request.Form("f2")
    f3 = Request.Form("f3")
    
    data_source = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=servername; DATABASE=abc; UID=abc; PWD=abc; OPTION=35;"
    
    sql_insert = "insert into tablename (FileName) values ('" & f1 & "')"
    con.Execute(sql_insert)
    sql_insert = "insert into tablename (FileName) values ('" & f2 & "')"
    con.Execute(sql_insert)
    sql_insert = "insert into tablename (FileName) values ('" & f3 & "')"
    con.Execute(sql_insert)
    
    con.Close
    Set con = Nothing
    %>
    hi sync_or_swim,

    Thanks for help me... I have others fields in the form, so I am testing this example, but when I run it I receive the error:

    Code:
    Error Type:
    Microsoft VBScript runtime (0x800A01A8)
    Object required: ''
    in the first con.Execute(sql_insert)
    Last edited by Khrysller; October 17th, 2007 at 07:08 AM.
  8. #5
  9. Contributing User
    ASP High Scholar (3500 - 3999 posts)

    Join Date
    Jul 2005
    Location
    Oxford UK
    Posts
    3,897
    Rep Power
    991
    before execute create connection object and open connection
    Nothing is Impossible bcoz IMPOSSIBLE itself says..
    I M POSSIBLE........................
    Be cool !!!!!!!!
  10. #6
  11. No Profile Picture
    Contributing User
    ASP Discoverer (100 - 499 posts)

    Join Date
    May 2005
    Posts
    342
    Rep Power
    42
    Thanks for the help sync_or_swim and Guddu : )

    sync_or_swim, i never thought the insert could be done this way... now it´s simple, but without your light, it will never be. thanks again ^^

    Now I need to find a way to find a way to store just the name of the file in the database, instead of the fullpath+filename. gonna search a bit ^^

    huggs.
  12. #7
  13. Contributing User
    ASP High Scholar (3500 - 3999 posts)

    Join Date
    Mar 2006
    Location
    South Wales
    Posts
    3,674
    Rep Power
    2046
    No Problem, glad to help!!!

    With regards just wanting to extract the filename, you could use split:
    Code:
    Dim  myFileName
    myFileName = Split(Request.Form("f1"), "/")
    Dim justTheFileName
    justTheFileName = myFileName(Ubound(myFileName))
  14. #8
  15. No Profile Picture
    Contributing User
    ASP Discoverer (100 - 499 posts)

    Join Date
    May 2005
    Posts
    342
    Rep Power
    42
    dude, thanks to keep help me

    I tried the code:

    Code:
    Dim  myFileName
    myFileName = Split(Request.Form("f1"), "/")
    Dim justTheFileName
    justTheFileName = myFileName(Ubound(myFileName))
    Response.Write justTheFileName
    Response.End
    and with the response.write it is showing the fullpath:

    C:\Documents and Settings\All Users\Documentos\Minhas imagens\Amostras de imagens\Ninféias.jpg

    i would like to output Ninféias.jpg
    did I miss something?

    Thanks again for the help.
  16. #9
  17. No Profile Picture
    Contributing User
    ASP High Scholar (3500 - 3999 posts)

    Join Date
    Jan 2007
    Location
    Indianapolis, USA
    Posts
    3,559
    Rep Power
    700
    Try this. It will check for the "\" in the file path:


    Code:
    filePath = Request.Form("f1")
    If InStr(filePath, "\") > 0 Then
    	fileNameArr = Split(filePath, "\")
    	strFileName = fileNameArr(UBound(fileNameArr))
    End If
    response.write "File Name: " & strFileName
  18. #10
  19. Contributing User
    ASP High Scholar (3500 - 3999 posts)

    Join Date
    Mar 2006
    Location
    South Wales
    Posts
    3,674
    Rep Power
    2046
    In your path you have backslashes (\) but in the split statement it is tying to split the string using a forward slash (/) the delimiter. Just change the forward slash to a backslash:
    Code:
    myFileName = Split(Request.Form("f1"), "\")
  20. #11
  21. No Profile Picture
    Contributing User
    ASP Discoverer (100 - 499 posts)

    Join Date
    May 2005
    Posts
    342
    Rep Power
    42
    thanks BLarche and sync_or_swim,

    sync_or_swim thanks again dude... sorry my auto of experiencie. big hugss ^^
  22. #12
  23. No Profile Picture
    Contributing User
    ASP High Scholar (3500 - 3999 posts)

    Join Date
    Jan 2007
    Location
    Indianapolis, USA
    Posts
    3,559
    Rep Power
    700
    Originally Posted by sync_or_swim
    In your path you have backslashes (\) but in the split statement it is tying to split the string using a forward slash (/) the delimiter. Just change the forward slash to a backslash:
    Code:
    myFileName = Split(Request.Form("f1"), "\")
    I would recommend using my code. If he has an empty file path, it will error out when trying to access the array.
  24. #13
  25. Contributing User
    ASP High Scholar (3500 - 3999 posts)

    Join Date
    Mar 2006
    Location
    South Wales
    Posts
    3,674
    Rep Power
    2046
    I agree, I posted that but didn't include any error checking. As you say, my code would fall over if the filename didn't contain any slashes.

Similar Threads

  1. ASP Insert and JS form loop
    By ecoviva in forum ASP Development
    Replies: 5
    Last Post: February 19th, 2007, 05:26 AM
  2. Debugging of a stored Procedure
    By zwieback89 in forum Microsoft SQL Server
    Replies: 0
    Last Post: January 11th, 2007, 01:01 PM
  3. Parse csv text file using ASP and insert it into Database
    By drif any in forum ASP Development
    Replies: 6
    Last Post: January 5th, 2007, 04:18 AM
  4. Insert into mdb from selected data from SQL database
    By Darius in forum Microsoft Access Help
    Replies: 1
    Last Post: September 14th, 2004, 05:08 PM

IMN logo majestic logo threadwatch logo seochat tools logo