944,028 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Marked Solved
  • Views: 5705
  • JSP RSS
Oct 17th, 2006
0

Unsure how to save to database dynamic Textfield values

Expand Post »
I'm trying to create a list to record student grades into the database. I'm able to pull out the names from the database and create a table with textfields in it to input the data into.

This is the code i'm using

for(int i = 0; rst2.next(); i++){
.......
<th scope="col"><input name="ca1_<%=i%>" type="text" id="exam1" size="7" /></th>
...

I'm using jsp and mysql

My database fields are exam1, exam2, exam3, exam4

Thank you so much for your help.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shaun09 is offline Offline
15 posts
since Oct 2006
Oct 18th, 2006
0

Re: Unsure how to save to database dynamic Textfield values


This is what i've tried.

for(int i = 0; rst2.next(); i++){
String ca1 = request.getParameter("ca1_<%=i%>");
String query2 = "INSERT into results(ca1) values ('"+ca1+"')";
stm2.executeUpdate(query2);
}


The error as follows..

String not terminated at end of line.
String ca1 = request.getParameter("ca1_<%=i
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shaun09 is offline Offline
15 posts
since Oct 2006
Oct 18th, 2006
0

Re: Unsure how to save to database dynamic Textfield values

JSP Syntax (Toggle Plain Text)
  1. String ca1 = request.getParameter("ca1_<%=i%>");
should this not be
JSP Syntax (Toggle Plain Text)
  1. String ca1 = request.getParameter("ca1_" + i);
Assuming the parameter names are ca1_0, ca1_1, ca1_2, etc.

At this point in the code you are already inside a scriptlet tag (at least I assume you are), so additional scriptlet tags are nothing but syntax errors.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 18th, 2006
0

Re: Unsure how to save to database dynamic Textfield values

Thank you it worked. Now i have another problem. Lets say i want to save all the information as a single string in the database instead of it going into another row. Here is my code. I'm using checkbox now.


for(int i = 0; rst2.next(); i++){
if(request.getParameter("ca1_"+i) !=null){
Childic = rst2.getString("Childic") ;
String query2 = "INSERT into results(ca1) values ('"+Childic+"')";
stm2.executeUpdate(query2);
}
else{

}
}

Thank you so very much for your help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shaun09 is offline Offline
15 posts
since Oct 2006
Oct 18th, 2006
0

Re: Unsure how to save to database dynamic Textfield values

If what you mean, is that you want to concatenate all values into one string and then insert that then change
JSP Syntax (Toggle Plain Text)
  1. for(int i = 0; rst2.next(); i++){
  2. if(request.getParameter("ca1_"+i) !=null){
  3. Childic = rst2.getString("Childic") ;
  4. String query2 = "INSERT into results(ca1) values ('"+Childic+"')";
  5. stm2.executeUpdate(query2);
  6. } else {
  7. // whatever it is you want here
  8. }
  9. }
to
JSP Syntax (Toggle Plain Text)
  1. for(int i = 0; rst2.next(); i++){
  2. if(request.getParameter("ca1_"+i) !=null){
  3. Childic = Childic + rst2.getString("Childic") ;
  4. } else {
  5. // whatever it is you want here
  6. }
  7. }
  8. String query2 = "INSERT into results(ca1) values ('"+Childic+"')";
  9. stm2.executeUpdate(query2);
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 18th, 2006
0

Re: Unsure how to save to database dynamic Textfield values

Thank you for your help. But there seems to be a problem. It saves to database as

Right now it saves as
A - 1st record
AB - 2nd record
ABC - 3rd record

I want it to be

A B C - 1st record

Thank you for your help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shaun09 is offline Offline
15 posts
since Oct 2006
Oct 18th, 2006
0

Re: Unsure how to save to database dynamic Textfield values

Got it working. This the code.


rst2 = stm1.executeQuery("select * from students");
String Childic = "";

for(int i = 0; rst2.next(); i++){
if(request.getParameter("ca1_"+i) !=null){
Childic += " " + rst2.getString("Childic") ;
}
String query2 = "INSERT into results(ca1) values ('"+Childic+"')";
stm2.executeUpdate(query2);


Thank you to all who viewed and helped.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shaun09 is offline Offline
15 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: Regular Expression
Next Thread in JSP Forum Timeline: Problem with update data to database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC