|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
newbie - problem with insert
Hello,
I have a form with two tables. The first table is a bunch of drop down fields that the user selects and sends the info to an action page that inserts the data into a table. The second table has four input boxes, each with a hidden variable. The info is sent to a different action page where there are a bunch of if statements that insert the data into different tables depending on the hidden variable. The first table/form/insert works great but the second one doesn't work at all. Here is the code for the second table: Code:
<cfform action="addData.cfm" method="POST">
<table align="center" width="400">
<tr>
<td colspan="2"><font color="#0000CC">You can add data to the
dropdown lists by typing in the textboxes below. NOTE: Submitting new
data to the list will refresh the form.</font>
</td>
</tr>
<tr>
<td>Refuge Abbreviation:</td>
<td>
<input type="text" name="add_reflist" value="" size="40">
<INPUT TYPE="hidden" NAME="submittype" VALUE="reflist">
</td>
</tr>
<tr>
<td>Requestor:</td>
<td>
<input type="text" name="add_requestor" value="" size="40">
<INPUT TYPE="hidden" NAME="submittype" VALUE="requestor">
</td>
</tr>
<tr>
<td>Requestor Office:</td>
<td>
<input type="text" name="add_reqoffice" value="" size="40">
<INPUT TYPE="hidden" NAME="submittype" VALUE="reqoffice">
</td>
</tr>
<tr>
<td>Project Type:</td>
<td>
<input type="text" name="add_projtype" value="" size="40">
<INPUT TYPE="hidden" NAME="submittype" VALUE="projtype">
</td>
</tr>
<tr>
<td>Map Type:</td>
<td>
<input type="text" name="add_maptype" value="" size="40">
<INPUT TYPE="hidden" NAME="submittype" VALUE="maptype">
</td>
</tr>
<td align="center" colspan="2"><input name="submit" type="submit" value="Add to List"></td>
</table>
</cfform>
and here is the code for the action page: Code:
<CFIF #form.submittype# IS "reflist">
<cfquery datasource="PLOG">
INSERT INTO data_admin.Status_Log (Literal) VALUES (
<cfif IsDefined("FORM.add_reflist") AND #FORM.add_reflist# NEQ "">
'#FORM.add_reflist#'
<cfelse>
NULL
</cfif>
)
</cfquery>
</CFIF>
<CFIF #form.submittype# IS "requestor">
<cfquery datasource="PLOG">
INSERT INTO data_admin.Requestor (Requestor) VALUES (
<cfif IsDefined("FORM.add_requestor") AND #FORM.add_requestor# NEQ "">
'#FORM.add_requestor#'
<cfelse>
NULL
</cfif>
)
</cfquery>
</CFIF>
<CFIF #form.submittype# IS "reqoffice">
<cfquery datasource="PLOG">
INSERT INTO data_admin.Requestor_Office (Requestor_Office) VALUES (
<cfif IsDefined("FORM.add_reqoffice") AND #FORM.add_reqoffice# NEQ "">
'#FORM.add_reqoffice#'
<cfelse>
NULL
</cfif>
)
</cfquery>
</CFIF>
<CFIF #form.submittype# IS "projtype">
<cfquery datasource="PLOG">
INSERT INTO data_admin.Project_Type (Project_Type) VALUES (
<cfif IsDefined("FORM.add_projtype") AND #FORM.add_projtype# NEQ "">
'#FORM.add_projtype#'
<cfelse>
NULL
</cfif>
)
</cfquery>
</CFIF>
<CFIF #form.submittype# IS "maptype">
<cfquery datasource="PLOG">
INSERT INTO data_admin.Map_Type (Map_Type) VALUES (
<cfif IsDefined("FORM.add_maptype") AND #FORM.add_maptype# NEQ "">
'#FORM.add_maptype#'
<cfelse>
NULL
</cfif>
)
</cfquery>
</CFIF>
<body>
<cfoutput>
<p><b><div align="center"> The following data has been added: </div>
</b></p>
<table align="center">
<tr>
<td>Refuge Literal:</td>
<td>#FORM.add_reflist#</td>
</tr>
<tr>
<td>Requestor:</td>
<td>#FORM.add_requestor#</td>
</tr>
<tr>
<td>Requestor Office:</td>
<td>#FORM.add_reqoffice#</td>
</tr>
<tr>
<td>Project Type:</td>
<td>#FORM.add_projtype#</td>
</tr>
<tr>
<td>Map Type:</td>
<td>#FORM.add_maptype#</td>
</tr></table>
</cfoutput>
<div align="center"><a href="main.cfm?page_name=addJob">Return to Project Insert</a> | <a href="main.cfm">Return to Main</a></div></p>
</body>
Can anyone see what I am doing wrong? The code for the action page for the first table is virtually the same but all the data is inserted into one table instead of using if statements to determine which table to inset into. Thanks for any help!! melissa |
|
#2
|
|||
|
|||
|
Are you getting an error message? Can you distill this down to just a test situation using one or two fields to narrow down where the problem is? Honestly that's just too much code for me to sift through in a reasonable time.
__________________
Ask if you have a question, but also help answer questions that you have knowledge of! Thanks, Brian. How to Post a Question in the Forums |
|
#3
|
|||
|
|||
|
Sorry about that.
No, no error message, just no new data inserted into the tables. I know the info is being sent from the form page to the action page because the info I type in to the input boxes are displayed, as in the following does work: Code:
<p><b><div align="center"> The following data has been added:
<td>Refuge Literal:</td>
<td>#FORM.add_reflist#</td>
but when I return to the first form nothing new appears in the dropdown boxes and when I look at the tables themselves no new data appears. Is there a way to check if the INSERT statement is working, like through some sort of trace? Thanks for your time! melissa |
|
#4
|
|||
|
|||
|
If you turn on debugging you can set it to show all the SQL code that executed during the request. I'd start by making sure that your tables are set to take null for all the columns. Also, you can copy the SQL code from the debugging output and attempt to run it within your database's IDE to track down the problem.
Actually, taking another look at your code I think I might see the problem. You're using a hidden form field called "submittype", and you're setting it to multiple values within the same form. If you do this and you post the form, CF will treat "form.submittype" as a comma-delimited list of ALL of these values. In other words, I don't think CF is ever getting just one value, it's getting a list of ALL the values of submittype every time you submit the form. You can confirm this by outputting "form.submittype" on the action page to see what it's getting. You'll either need to make each option a separate form, or use some other way to determine what the submittype was (possibly checking the text boxes for which one has text in it, etc.). |
|
#5
|
|||
|
|||
|
Hey you are right I AM getting a comma delimted list of all them! The weird thing is this setup worked for another situation, slightly similar.
In that case I had two different forms submitting to the same action page. One form was to add a job and another form (separate cfm page) to edit a job. Both had a hidden variable (submittype = insert for one and submittype = update for the other). There is a similar if statement setup that checks the value of the submittype and then either does and INSERT or an UPDATE to the same table. When I output the submittype variable in that situation it outputs only one value (the correct one thankfully!). So I thought I could use a similar setup in this new situation but maybe not? Do I have to have a separate action page for each one then? Thanks again! melissa |
|
#6
|
|||
|
|||
|
No you don't need separate action pages for each one, though that would probably be much easier to maintain than having all the insert statements in one file. The more programming you do the more you'll appreciate encapsulation and cohesiveness: the idea that any one file should have only 1 task, and should do that 1 task in the most efficient way. When a file (or object or method or whatever) starts taking on multiple responsibilities, complexity and maintenance headaches are not far behind...
Once you understand CFML in general, I'd recommend looking at a framework like Fusebox or Mach-II to help organize your code. Regards, Brian |
|
#7
|
|||
|
|||
|
Quote:
Hi all, I am still banging my head on this problem and can't figure out a solution to make it work. I would like to avoid having one action page for every insert. Is it possible to create one form that checks to see which text box has text in it, and if it has text do an insert into it's corresponding table? There are four text boxes for four tables. Couldn't I use a series of cfif statements to check for a value in the box? Thanks for any help! melissa |
|
#8
|
|||
|
|||
|
Sure, just use <cfif len( trim( form.someField ) )> which will be true if the field has anything in it.
|
|
#9
|
|||
|
|||
|
Great! But then how can I make my form page work with the four separate INSERT statements? Do I just use the logic of "if the textbox from the form has text in it then insert that into whichever table" ? And so forget having the hidden form variable that has the same name but different values depending on the box and just use the input box names straight up?
Anyone else confused yet? lol . . . thanks, melissa **EDIT** Nevermind! I got it! I don't know why I was trying to make it so hard when it was so simple! Thanks Brian, you are awesome!! ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ColdFusion Development > newbie - problem with insert |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|