<cfparam name="FORM.id" type="numeric" default="0" />
<cfparam name="FORM.name" type="string" default="" />
<cfparam name="FORM.hotness" type="string" default="" />
<cfparam name="FORM.photo" type="string" default="" />
<cfparam name="FORM.submitted" type="boolean" default="false" />
<cfif FORM.submitted>
<cfif FORM.id>
<cfquery name="qUpdate" datasource="#REQUEST.DSN#">
UPDATE
hottie
SET
name =
<cfqueryparam
value="#FORM.name#"
cfsqltype="cf_sql_varchar"
/>,
hotness =
<cfqueryparam
value="#FORM.hotness#"
cfsqltype="cf_sql_decimal"
scale="1"
/>
WHERE
id =
<cfqueryparam
value="#FORM.id#"
cfsqltype="cf_sql_integer"
/>
</cfquery>
<cfelse>
<cfquery name="qInsert" datasource="#REQUEST.DSN#">
INSERT INTO hottie
(
name,
hotness
) VALUES (
<cfqueryparam
value="#FORM.name#"
cfsqltype="cf_sql_varchar"
/>,
<cfqueryparam
value="#FORM.hotness#"
cfsqltype="cf_sql_decimal"
scale="1"
/>
);
SELECT
( LAST_INSERT_ID() ) AS id
;
</cfquery>
<cfset FORM.id = qInsert.id />
</cfif>
<cfif Len( FORM.photo )>
<cffile
action="upload"
filefield="photo"
destination="#ExpandPath( './' )#"
nameconflict="makeunique"
/>
<cffile
action="readbinary"
file="#ExpandPath( './' )##CFFILE.ServerFile#"
variable="binPhoto"
/>
<cffile
action="delete"
file="#ExpandPath( './' )##CFFILE.ServerFile#"
/>
<cfquery name="qInsert" datasource="#REQUEST.DSN#">
UPDATE
hottie
SET
photo =
<cfqueryparam
value="#binPhoto#"
cfsqltype="cf_sql_blob"
/>
WHERE
id =
<cfqueryparam
value="#FORM.id#"
cfsqltype="cf_sql_integer"
/>
</cfquery>
</cfif>
</cfif>
<cfquery name="qHottie" datasource="#REQUEST.DSN#">
SELECT
h.id,
h.name,
h.hotness,
(
CASE
WHEN
h.photo IS NULL
THEN
0
ELSE
1
END
) AS has_photo
FROM
hottie h
WHERE
h.id =
<cfqueryparam
value="#FORM.id#"
cfsqltype="cf_sql_integer"
/>
</cfquery>
<cfoutput>
<!DOCTYPE html PUBLIC "- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>ColdFusion / MySQL Blob Demo</title>
</head>
<body>
<h1>
ColdFusion / MySQL Blob Demo
</h1>
<form
action="#CGI.script_name#"
method="post"
enctype="multipart/form-data">
<input type="hidden" name="submitted" value="true" />
<input type="hidden" name="id" value="#FORM.id#" />
<p>
Name:<br />
<input
type="text"
name="name"
value="#FORM.name#"
size="20"
maxlength="40"
/>
</p>
<p>
Hotness:<br />
<input
type="text"
name="hotness"
value="#FORM.hotness#"
size="10"
maxlength="4"
/>
</p>
<p>
Photo:<br />
<input
type="file"
name="photo"
size="50"
/>
</p>
<cfif (
qHottie.RecordCount AND
qHottie.has_photo
)>
<p>
<img
src="./photo.cfm?id=#qHottie.id#"
title="#HtmlEditFormat( qHottie.name )#"
/>
</p>
</cfif>
<p>
<input type="submit" value="Submit" />
</p>
</form>
</body>
</html>
</cfoutput>