Example of ColdFusion inserting data into a database

Source: Internet
Author: User
Tags name database
Insert data into the database

 

Save the following code and use URL: http: // 127.0.0.1/insert. cfm to get the HTML content.
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> Insert new employee information </title>
<Style type = "text/CSS">
<! --
. T {background-color: # ffffff; Border: 1px dotted}
-->
</Style>
</Head>

<Body bgcolor = "# ffffff">
<SPAN class = "T"> <SPAN class = "T"> </span>
<Form method = "Post" Action = "insert_action.cfm">
<P>
Employee name
<Input type = "text" name = "name" size = "20" class = "T">
<Br>
Employee Salary
<Input type = "text" name = "salary" size = "20" class = "T">
<Br>
Home address
<Input type = "text" name = "Address" size = "20" class = "T">
</P>
<P> <input type = "Submit" value = "insert" name = "insert">
<Input type = "reset" value = "Override" name = "reset">
</P>
</Form>
</Body>
</Html>

It looks like the following

Employee name
Employee Salary
Home address

As you can see, this is an HTML form used to submit data to the database. To make the style look good, we have added some CSS code. <Form> the method in the label is the form submission method, which must be specified. Note: in CF, the form submission method must be post. If other submission methods are used, such as get, an error occurs when you submit the file. Action indicates which processing file should be used to process the data submitted in form. Here we use the insert_action.cfm file. You should name the names in each <input> label. Wait and you will know why you want to do so.

Then save the following code, named insert_action.cfm, that is, the file specified by the form action to process the submitted data.

<Cfif # parameterexists (Form. insert) #>
<Cfif not # Len (Form. Name) # Or not # Len (Form. Salary) # Or not # Len (Form. Address) #>
<Cflocation url = "insert. cfm">
</Cfif>
<Cfquery name = "selectemployee" datasource = "employee">
Select * From employee_info where employee_name = '# form. name #'
</Cfquery>
<Cfif # selectemployee. recordcount # GT 0>
<Cfoutput> <center> EMPLOYEE # form. name # information already exists. Please verify the information before entering </center> </cfoutput>
<Cfelse>

<Cfquery name = "ABC" datasource = "employee">
Insert into employee_info (employee_name, employee_salary, employee_address)
Values ('# form. name #', # form. Salary #, '# form. Address #')
</Cfquery>

<Cfquery name = "ABC" datasource = "employee">
Select * From employee_info
</Cfquery>

<HTML>
<Head>
<Title> employee payroll </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>

<Body bgcolor = "# ffffff" text = "#000000">

<Table width = "50%" border = "1" bordercolor = "#000000">
<Tr>
<TD> NO. </TD>
<TD> employee name </TD>
<TD> salary </TD>
<TD> address </TD>
</Tr>
<Cfoutput query = "ABC">
<Tr>
<TD> # employee_id # </TD>
<TD> # employee_name # </TD>
<TD> # employee_salary # </TD>
<TD> # employee_address # </TD>
</Tr>
</Cfoutput>
</Table>
</Body>
</Html>
</Cfif>

<Cfelse>
<Cflocation url = "insert. cfm">
</Cfif>

Here, we use a CF Tag: <cfif>, which is used to determine the condition and then perform corresponding processing. # Parameterexists (Form. insert) #> in <cfif # parameterexists (Form. insert )#
... (The intermediate code is omitted)
<Cfelse>
<Cflocation url = "insert. cfm">
</Cfif>

Is to judge form. whether the Insert variable exists. Now you know why we need to write <input type = "Submit" value = "insert" name = "insert"> in the previous form file, name submit as insert. If no data is submitted, skip insert directly. CFM, we don't want to see this happen, so we wrote this <cfif> judgment statement. If the variable does not exist, execute <cflocation url = "insert. CFM ">, return to insert. CFM.
Parameterexists () is a function of CF to determine whether a variable exists.

<Cfif not # Len (Form. Name) # Or not # Len (Form. Salary) # Or not # Len (Form. Address) #>
<Cflocation url = "insert. cfm">
</Cfif> This code serves the same purpose as the above Code. If you do not have to enter complete data and some boxes are not entered, run <cflocation url = "insert. CFM ">, remain in the same place until all data is fully input. You can also write some prompt information. Len () is a function used to determine whether the form variable is null.

<Cfquery name = "selectemployee" datasource = "employee">
Select * From employee_info where employee_name = '# form. name #'
</Cfquery>
<Cfif # selectemployee. recordcount # GT 0>
<Cfoutput> <center> EMPLOYEE # form. name # information already exists. Please verify the information before entering </center> </cfoutput>
As I said, the <cfquery> label uses standard SQL statements to perform database operations. Here we determine that the insert. whether the name of the person submitted by CFM already exists in the Database. Select * From employee_info where employee_name = '# form. name # '. Note that if the variable is a string, it must be enclosed by single quotation marks around the variable name. <Cfif # selectemployee. recordcount # GT 0>
<Cfoutput> <center> EMPLOYEE # form. name # information already exists. Please verify the information before entering </center> </cfoutput>
If the query result is greater than 0, an error message is displayed. # Selectemployee. recordcount # indicates the total number of queries. gt indicates a greater value.

<Cfelse>
<Cfquery name = "ABC" datasource = "employee">
Insert into employee_info (employee_name, employee_salary, employee_address)
Values ('# form. name #', # form. Salary #, '# form. Address #')
</Cfquery>
These codes are the focus of this lesson: If the submitted name database does not exist, it will be inserted into the database to become a new record. The <cfquery> label is still used. The inserted SQL statement is written as follows: insert into Table Name (field names in the table, separated by commas) values (variable name or specific data, if multiple values are separated by commas). Note that if the variable is of the string type, use single quotation marks to enclose it around the variable name. You can understand other codes.

Okay. This course is over. We will continue in the next lesson. In the next course, we will show you how to update existing data to the database and delete existing data. Do you have any comments or suggestions, please mail to me, JavaProgramers@hotmail.com thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.