Skip to main content
Inspiring
June 1, 2007
Answered

Upload image to directory and data to database from the same form

  • June 1, 2007
  • 1 reply
  • 381 views
Hello all

I'm playing at building a shopping cart (never done one before) from a tutorial by Gordon Knapp at www.webthang.com.

I have created the site and it works perfectly. Now I want to extend it.

There is a form that allows the user to add data to the database. I would also like to be able to upload an image to a directory on the server and add the data to the database at the same time from the same form. It would be good if the input field "<input name="product_picture" type="text" id="product_picture" tabindex="6" size="50" />" could be populated automatically by the filename of the image being uploaded, but this isn't a neccessity.

I have looked at various ASP Upload packages and I can upload an image alone but none of them make it clear how to upload the image and add data to the database at the same time.

As this is a private project I need any suggestions to by free.

The tutorial shows how to create the site in Ultra Dev but worked perfectly well in DW. The page concerned looks like this. It is named 'admin_add.asp':

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="../Connections/con_ecom.asp" -->
<%
Dim MM_editAction
MM_editAction = CStr(Request.ServerVariables("SCRIPT_NAME"))
If (Request.QueryString <> "") Then
MM_editAction = MM_editAction & "?" & Server.HTMLEncode(Request.QueryString)
End If

' boolean to abort record edit
Dim MM_abortEdit
MM_abortEdit = false
%>
<%
' IIf implementation
Function MM_IIf(condition, ifTrue, ifFalse)
If condition = "" Then
MM_IIf = ifFalse
Else
MM_IIf = ifTrue
End If
End Function
%>
<%
If (CStr(Request("MM_insert")) = "add_form") Then
If (Not MM_abortEdit) Then
' execute the insert
Dim MM_editCmd

Set MM_editCmd = Server.CreateObject ("ADODB.Command")
MM_editCmd.ActiveConnection = MM_con_ecom_STRING
MM_editCmd.CommandText = "INSERT INTO products (product_category, product_name, product_price, product_briefdesc, product_fulldesc, product_picture) VALUES (?, ?, ?, ?, ?, ?)"
MM_editCmd.Prepared = true
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param1", 202, 1, 50, Request.Form("product_category")) ' adVarWChar
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param2", 202, 1, 50, Request.Form("product_name")) ' adVarWChar
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param3", 5, 1, -1, MM_IIF(Request.Form("product_price"), Request.Form("product_price"), null)) ' adDouble
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param4", 203, 1, 536870910, Request.Form("product_briefdesc")) ' adLongVarWChar
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param5", 203, 1, 536870910, Request.Form("product_fulldesc")) ' adLongVarWChar
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param6", 202, 1, 50, Request.Form("product_picture")) ' adVarWChar
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close

' append the query string to the redirect URL
Dim MM_editRedirectUrl
MM_editRedirectUrl = "admin_control.asp"
If (Request.QueryString <> "") Then
If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0) Then
MM_editRedirectUrl = MM_editRedirectUrl & "?" & Request.QueryString
Else
MM_editRedirectUrl = MM_editRedirectUrl & "&" & Request.QueryString
End If
End If
Response.Redirect(MM_editRedirectUrl)
End If
End If
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Administration - Add Product</title>
<link href="../scripts/admin.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../scripts/ecom.js"></script>
</head>

<body onLoad="adminTitle()">
<div id="wrapper">

<div id="pageHeading">ADMINISTRATION - ADD PRODUCT</div>

<div id="navBar"><a href="admin_control.asp">Control</a> | Add | <a href="admin_view.asp">View</a> | <a href="admin_update1.asp">Update</a> | <a href="admin_delete1.asp">Delete</a> | <a href="../Default.asp">Shop</a></div>

<br />

<form name="add_form" id="add_form" method="POST" action="<%=MM_editAction%>">
<table border="1" align="center" id="tblArea">
<tr>
<td class="tdLabel">Product Category</td>
<td colspan="2"><input name="product_category" type="text" id="product_category" tabindex="1" size="50" /></td>
</tr>
<tr>
<td class="tdLabel">Product Name</td>
<td colspan="2"><input name="product_name" type="text" id="product_name" tabindex="2" size="50" /></td>
</tr>
<tr>
<td class="tdLabel">Product Price</td>
<td class="tdContent" colspan="2"><input type="text" name="product_price" id="product_price" tabindex="3" size="50" /></td>
</tr>
<tr>
<td class="tdLabel">Product Short Desciption</td>
<td class="tdContent" colspan="2"><textarea name="product_briefdesc" id="product_briefdesc" cols="47" rows="4" tabindex="4"></textarea></td>
</tr>
<tr>
<td class="tdLabel">Product Full Description</td>
<td class="tdContent" colspan="2"><textarea name="product_fulldesc" id="product_fulldesc" cols="47" rows="5" tabindex="5"></textarea></td>
</tr>
<tr>
<td class="tdLabel">Product Image File Name</td>
<td class="tdContent" colspan="2"><input name="product_picture" type="text" id="product_picture" tabindex="6" size="50" /></td>
</tr>
<tr>
<td class="tdLabel"> </td>
<td align="center"><input type="reset" name="Reset" id="Reset" value="Reset Form" /></td>
<td align="center"><input type="submit" name="Submit" id="Submit" value="Add To Shop" /></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="add_form" />
</form>

</div>
</body>
</html>

Hope you can help.

Warm regards

Martin
This topic has been closed for replies.
Correct answer Pantyboy
Got it sorted.

Found an excellent tutorial at Webthang.

http://www.webthang.co.uk/tuts/tuts_dmx/rob15/rob15.asp

1 reply

PantyboyAuthorCorrect answer
Inspiring
June 3, 2007
Got it sorted.

Found an excellent tutorial at Webthang.

http://www.webthang.co.uk/tuts/tuts_dmx/rob15/rob15.asp