Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting multiple values of the same db field into a table

Status
Not open for further replies.

tazzer

Technical User
May 3, 2001
11
0
0
GB

Hi folks,

I have created an application that will allow users to log visitors to our humble organisation. I am using an Access database back end that I currently input the visitor name into (one text field only). However I want to be able to insert not just one name at a time but rather 2 to 5 names (for delegations)in order to speed things up. In other words I want to be able to insert several records of the same field type with one hit of the Submit button.

My code is as follows:

asp page 1

<tr>
<td><b><font size=&quot;2&quot;>Visitor</font></b></td>
<td><input type=&quot;text&quot; name=&quot;date_today&quot; size=&quot;11&quot;><font size=&quot;2&quot;></font></td>
<td> <input type=&quot;submit&quot; value=&quot;Submit Form&quot; name=&quot;Submit&quot;></td>
</tr>

asp confirmation page

<%
dim objConn, strQuery
set objConn = Server.CreateObject(&quot;ADODB.Connection&quot;)
'objConn.Open(&quot;DSN=Visitors&quot;)
objConn.Open(&quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Microsoft Site Server\Data\Publishing\ininet\Databases\visitors.mdb;Persist Security Info=False&quot;)
strQuery = &quot;INSERT INTO daily_Visitors(visitor_name) &quot;
strQuery = strQuery & &quot;VALUES ('&quot;
strQuery = strQuery & Request.Form(&quot;visitor&quot;) & &quot;')&quot;
objConn.Execute(strQuery)

objConn.close
%>

Instead of inserting just the one value into the visitor_name field, I want to insert 5 values into the visitor_name field, meaning 5 new records.

Can anyone help?

Cheers,

Tazzer
 
you'll have to place your execute into a loop and change the insert statement on every new value passed in the request.
suedocode would be something like this
for count = 0 to request length
sql = &quot;INSERT INTO( ) VALUE(&quot;
sql = sql & request objects & &quot;)&quot;
rs.execute(sql)
next
_______________________________________________
[sub]{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }[/sub]
_______________________________________________
for the best results to your questions: FAQ333-2924
has you're questio
 
sorry if I made that a bit confusing with the rs. in there, that should be connection.execute.
trying to do to things at the same time.
for count = 0 to request length
sql = &quot;INSERT INTO( ) VALUE(&quot;
sql = sql & request objects & &quot;)&quot;
connectionObject.execute(sql)
next _______________________________________________
[sub]{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }[/sub]
_______________________________________________
for the best results to your questions: FAQ333-2924
has you're questio
 
is there an easy way to do this...depends on your page I guess.

I haven't tried it but I am sure it would work

You could have 5 input boxes all with the name &quot;date_today&quot;
Then when you do your Request(&quot;date_today&quot;) you will get all of the values entered comma delimited.

You could then say

For each item in Request(&quot;date_today&quot;)
do the insert
Next


or you have 1 text box, ask them to separate the names with comma's and parse through the string to find each of the names, then create a loop to do the insert.
 
In both of the above methods you need to check to make sure that the field has something in it before you insert...

formData = request(&quot;visitor&quot;)

if isArray(formData) then
for x = 0 to uBound(formData)
if trim(formData(x)) <> &quot;&quot; then
sql = &quot;INSERT INTO myTable VALUES ('&quot;& formData(x) &&quot;')&quot;
connectionObject.execute(sql)
end if
next
else
if trim(formData) <> &quot;&quot; then
sql = &quot;INSERT INTO myTable VALUES ('&quot;& formData &&quot;')&quot;
connectionObject.execute(sql)
end if
end if


a more elegant method would be to allow the user to click a button to add another form field (using javascript). If you're interested in the method, let me know... Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 
mwolf, good point but I really supplied no code. the suedocode was a mirror of you supplying the code basically. Wanted to make that clear in hope there was the possibity that suedocode was misinterpruted as it being working code. I would hope checking for a value would be given, but.... [wink] _______________________________________________
[sub]{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }[/sub]
_______________________________________________
for the best results to your questions: FAQ333-2924
has you're questio
 
I like the javascript add a row idea, that is always fun, I have a timecard that allows you to enter as many days as you want by dynamically adding new rows to the form. It comes off as really professional, or at least as relatively nifty :p

-Tarwn

ps - sorry, everyone else said something so I had to add a post :) ________________________________________________________________________________
Sometimes it is how you ask the question: faq333-2924
Many ASP questions have already been answered, please check faq333-3048 and use the search tool before posting
 
I had to do that one a few weeks ago too. I thought it turned out pretty cool. here's the function if interested in going that route
function addBox() {
var lng = frm.length + 1
var Nfield = &quot;numbers&quot; + lng
//alert(Nfield);
frm.innerHTML += '<input type=&quot;text&quot; name=&quot;' + Nfield +'&quot; class=txtbx><br><br>'
}

obviously trimmed down but working _______________________________________________
[sub]{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }[/sub]
_______________________________________________
for the best results to your questions: FAQ333-2924
 
onpnt - have you tried it with nodes? even niftier (javascript DOM rocks!) Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 
Yep, I did mine that way...Try doing it when the rows need select boxes that pull info from the db AND can handle someone pasting in info from excel by dynamically growing to fit the rows from excel and selecting the correct option from the select box...(that was a bit difficult...I mean...darnit, onpnt is going to get me again)

-Tarwn ________________________________________________________________________________
Sometimes it is how you ask the question: faq333-2924
Many ASP questions have already been answered, please check faq333-3048 and use the search tool before posting
 
Thanks for your help so far folks. Its much appreciated.

Used mwolf00's code from earlier and now can insert from several input fields into the same field in the db table. Unfortunately it puts all of the field inputs into the same record whereas I want the inputs to go in as separate records:

<form name=&quot;frmAddContacts&quot; method=&quot;post&quot; action=&quot;addcontact3.asp&quot;>
<tr><td><b>First Name:</b></td>
<td><input type=&quot;text&quot; name=&quot;txtFirstName&quot; width=30></td></tr>
<tr><td><b>First Name:</b></td>
<td><input type=&quot;text&quot; name=&quot;txtFirstName&quot; width=30></td></tr>
<tr><td><b>First Name:</b></td>
<td><input type=&quot;text&quot; name=&quot;txtFirstName&quot; width=30></td></tr>
<tr><td align=&quot;left&quot; colspan=&quot;2&quot;><input type=&quot;submit&quot; value=&quot;Add Contact&quot;></tr>

formData = Request.form(&quot;txtFirstName&quot;)
if isArray(formData) then
for x = 0 to uBound(formData)
if trim(formData(x)) <> &quot;&quot; then
sql = &quot;INSERT INTO contacts(firstname) VALUES ('&quot;& formData(x) &&quot;')&quot;
conn.execute(sql)
end if
next
else
if trim(formData) <> &quot;&quot; then
sql = &quot;INSERT INTO contacts(firstname) VALUES ('&quot;& formData &&quot;')&quot;
conn.execute(sql)
end if
end if

Help! Where am I going wrong?

 
well first off it was me, I wouldn't be using an insert.
I would instead do an update as a transaction intead.

SQL = &quot;select * from contacts&quot;
oConn.Open &quot;myDSN&quot;, &quot;username&quot;, &quot;password&quot;
rs.open sql, oConn, adOpenKeyset, adLockBatchPessimistic

then do your
for x=0 to Ubound(Formdata)
rs.addnew
rs(&quot;firstname&quot;) = formData(x)
next x
rs.updateBatch
rs.close
oConn.close


This way I do everything as a batch update, if one fails, none of them are input so I don't get incomplete data.
I don't have to worry about escaping quotes or other foolish things that users might input.

I just have to worry that my field type in the database supports the data that is being input into it.
 
A) How is the ability to do transactions limited to using the Recordset object's AddNew or Update function, and
B) What about efficiency?

-Tarwn ________________________________________________________________________________
Sometimes it is how you ask the question: faq333-2924
Many ASP questions have already been answered, please check faq333-3048 and use the search tool before posting
 
Oh, and the only problem above that I could see by glancing at mwolfs code is that the comma delimited string being received in the Request collection needs to be split on comma-space to turn it into an array:
Code:
formData = Split(request(&quot;visitor&quot;),&quot;, &quot;)

Oh, and heres something difficult for the single quotes that may appear in names like &quot;O'brian&quot;:
Code:
formData = Replace(request(&quot;visitor&quot;),&quot;'&quot;,&quot;''&quot;)
formData = Split(formData,&quot;, &quot;)


-Tarwn
________________________________________________________________________________
Sometimes it is how you ask the question: faq333-2924
Many ASP questions have already been answered, please check faq333-3048 and use the search tool before posting
 
sorry that would be AdLockBatchOptimistic not AdLockBatchPessimistic
 
&quot;How is the ability to do transactions limited to using the Recordset object's AddNew or Update function&quot;

Its not. No where did I say it was.

As for the performance difference, I don't know which is faster, however if performance is your biggest issue then you should consider doing it in ASP.NET and not ASP anyway.

I would suspect that a straight insert would be more efficient.
 
Hi guys,

Nearly doing what I want now; I used Tarwn's line :-
formData = Split(Request.form(&quot;txtFirstName&quot;),&quot;, &quot;)which did the trick.

However I want to insert a second field into the database as well eg.

<tr>
<td><b>First Name:</b></td>
<td><input type=&quot;text&quot; name=&quot;txtFirstName&quot; width=30></td>
<td><b>Last Name:</b></td>
<td><input type=&quot;text&quot; name=&quot;txtLastName&quot; width=30></td>
</tr>
<tr>
<td><b>First Name:</b></td>
<td><input type=&quot;text&quot; name=&quot;txtFirstName&quot; width=30></td>
<td><b>Last Name:</b></td>
<td><input type=&quot;text&quot; name=&quot;txtLastName&quot; width=30></td>
</tr>

I have tried without luck to insert the two fields together. How can I alter the array to ensure that the two fields are inserted as the same record for each?

Yours,

Tazzer
 
If you split both of them into seperate arrays, than as you are looping through one you can also use the loop counter as the index for the second. ie:
Code:
for x = 0 to uBound(formData)
    if trim(formData(x)) <> &quot;&quot; then
      sql = &quot;INSERT INTO contacts(firstname,lastname) VALUES ('&quot;& formData(x) &&quot;','&quot; & OtherFormData(x) & &quot;')&quot;
      conn.execute(sql)
    end if
  next
where otherFormData would be the array you split lastname into.

-Tarwn ________________________________________________________________________________
Sometimes it is how you ask the question: faq333-2924
Many ASP questions have already been answered, please check faq333-3048 and use the search tool before posting
 

Tarwn,

Thanks for the advice; I've got the code working a treat now!

And thanks again to all the guys who contributed to this thread.

Tazzer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top