I was emailed earlier this week and asked if I
could write a tutorial on how to insert data into a database with "query string"
variables.
[i.e. page.cfm?name=John&email=john@smith.com]
So I thought I would create a short tutorial
demonstrating the use of different variables within a query.
The first thing you must remember is that a
variable is a variable, whether it is being passes via a form or a URL query
string. With that in mind I'll show you different ways that you can access
variables.
Let's say you have a form that will allow a
user to add their name and email to a database. You have to ways for passing the
variables. The first example is the recommended way.
The first way to pass variables is via FORM
submission. As seen below.
<!--- Form Page --->
<form action="process.cfm"
method="post">
Name: <input type="text"
name="myname"><BR>
Email: <input type="text"
name="myemail"><BR>
<input type="submit"
name="Go"
value="Add Me">
</form>
What makes this form actually pass FORM
variables is the method="post" defined in the <form>
tag. So on the action page you would use the passed values and enter them into
your database as follows:
<!--- Insert user
--->
<cfquery name="qAddUser"
datasource="MyDSN">
INSERT INTO Users(
Name,
Email,
)
Values(
'#form.myname#',
'#form.myemail#',
)
</cfquery>
Notice that you are referring to the variables
a form.variables. If you would remove the METHOD="POST"
definition to the form page, then your form would be passed via URL query string
variables. so using the #form.name# method wouldn't work, simply because they
are no longer FORM variables, but instead URL Variables.
So, how do you reference URL variables? Well
using URL.name of course :)
So if you have a URL quesrystring form then how would the <cfquery>
above look then? Let's take a look:
<!--- Insert user
--->
<cfquery name="qAddUser"
datasource="MyDSN">
INSERT INTO Users(
Name,
Email,
)
Values(
'#url.myname#',
'#url.myemail#',
)
</cfquery>
It's that simple. Now keep in mind that I don't
personally suggest you use query string values. Simply because you'll have to
pass the values in URL Encoded Format (What
is URL Encoded Format?) which is just asking for trouble. Always use FORM
variables, they're easier to work with and are always much more effective, not
to mention they're also cross-browser [i.e Internet Explorer, Netscape, Opera,
etc.] friendly :)
Well, there's also much more to this method
(URL String values) such as URLEncodedFormat,
URLDecode
and more,
If you are seriously looking to use URL formats, I suggest you familiarize
yourself with these tags and their usage.
Date added: Sun. September 15, 2002
Posted by: Pablo Varando | Views: 33310 | Tested Platforms: CF5 | Difficulty: Intermediate
Databases
I need help Please
Hi I want to add URL address to open window method using JScript inside my XML but i got error messge related to reserved character in XML my URL like: xxx?id=1&id2=2 the problem related to & character how can i solve this issue please
Posted by: humam
Posted on: 12/27/2007 12:18 PM
|
|