0
votes

This is for a simple submit into a database. I have two tables. One is called "Authors", while the other is called "corpPosts".

The Form consist of a Title, and a Body value that will be inserted as a new entry into the corpPosts Table.

The form also consists of a drop down box which collects values from the Authors table. The user clicks the designated author to decide who posted the blog.

Once the form is submitted, it Creates a new record in the corpPosts Table, inserting the Title, Body, as well as the author into the corpposts table. The Author in the corppost table is represented by "UserID".

I do not remember how to associate the AuthorName to the UserID however. I know this is a relatively simple query.

<cfform action="AddCorp.cfm" method="post">

<cfinput type="text" reqired="yes" name="Title" id="Title">

<textarea style="width: 1000px; height: 600px;" name="CorpBody" id="Body"></textarea>
  <select Name="SelectAuthor">

<!--- Queries --->
  <cfquery name="Authors" datasource="corpposts">
  SELECT Name
  FROM Authors
  </cfquery>

<cfoutput QUERY="Authors"><option>#Name#</option></cfoutput>
</select>

  <cfinput type="Submit" name="Submit" id="Submit" value="Submit">

</cfform>

On the Next Page:

<!--- Query to Insert --->

<CFQUERY name="AddPosts" datasource="corpposts">

INSERT INTO CorpPosts (Title, CorpBody, UserID)

VALUES

('#Form.Title#', '#Form.CorpBody#', '#Form.UserID#')

</CFQUERY>
1
Please do not use that code in production. You really need to use cfqueryparam to send those values to the query. Don't use values directly from the form scope. At the very least use cfqueryparam, but you should really be manually validating them as well before you send them to the query. - Sean Coyne

1 Answers

4
votes

Simple it is; as you said.

Change your Authors query to also get the authorId (or the name of id column in Authors table). Pass it as the value to the option while the display text still remains the name of the author.

<cfform action="AddBlog2.cfm" method="post">

    <cfinput type="text" reqired="yes" name="Title" id="Title">

    <textarea style="width: 1000px; height: 600px;" name="BlogBody" id="Body"></textarea>

    <!--- Queries --->
      <cfquery name="Authors" datasource="prpblog">
          SELECT Name, authorId
          FROM Authors
      </cfquery>

    <select Name="SelectAuthor">
        <cfoutput QUERY="Authors">
            <option value="#authorId#">#Name#</option>
        </cfoutput>
    </select>

    <cfinput type="Submit" name="Submit" id="Submit" value="Submit">

</cfform>

on the next page, use the selectAuthor value as the userID

<!--- Query to Insert Blog --->

<cfquery name="AddBlog" datasource="prpblog">
    INSERT INTO BlogPosts (Title, BlogBody, UserID)
    VALUES
    (
        <cfqueryparam value='#Form.Title#' cfsqltype="cf_sql_varchar"/>, 
        <cfqueryparam value='#Form.BlogBody#' cfsqltype="cf_sql_varchar"/>, 
        <cfqueryparam value='#Form.SelectAuthor#' cfsqltype="cf_sql_numeric"/>
    )
</cfquery>