<%
' Get and validate all inputs
Dim modelnumber, vendorid, notes, documentationpath
Dim newvendorname, isprinter, ispc, ismachine
Dim modelisprinter, modelispc, modelismachine
modelnumber = Trim(Request.Form("modelnumber"))
vendorid = Trim(Request.Form("vendorid"))
notes = Trim(Request.Form("notes"))
documentationpath = Trim(Request.Form("documentationpath"))
' New vendor fields
newvendorname = Trim(Request.Form("newvendorname"))
isprinter = Request.Form("isprinter")
ispc = Request.Form("ispc")
ismachine = Request.Form("ismachine")
' Model type checkboxes
modelisprinter = Request.Form("modelisprinter")
modelispc = Request.Form("modelispc")
modelismachine = Request.Form("modelismachine")
' Validate required fields
If modelnumber = "" Then
Response.Write("
Error: Model number is required.
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
' Validate field lengths
If Len(modelnumber) > 255 Then
Response.Write("
Error: Model number too long.
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
If Len(notes) > 255 Then
Response.Write("
Error: Notes too long.
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
If Len(documentationpath) > 255 Then
Response.Write("
Error: Documentation path too long.
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
' Escape quotes
modelnumber = Replace(modelnumber, "'", "''")
notes = Replace(notes, "'", "''")
documentationpath = Replace(documentationpath, "'", "''")
newvendorname = Replace(newvendorname, "'", "''")
' Check if we need to create a new vendor first
If vendorid = "new" Then
If newvendorname = "" Then
Response.Write("
Error: Manufacturer name is required when adding a new manufacturer.
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
If Len(newvendorname) > 50 Then
Response.Write("
Error: Manufacturer name too long.
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
' Check if vendor already exists
Dim checkSQL, rsCheck
checkSQL = "SELECT COUNT(*) as cnt FROM vendors WHERE LOWER(vendor) = LOWER('" & newvendorname & "')"
Set rsCheck = objConn.Execute(checkSQL)
If rsCheck("cnt") > 0 Then
rsCheck.Close
Response.Write("
Error: Manufacturer '" & Server.HTMLEncode(Request.Form("newvendorname")) & "' already exists.
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
rsCheck.Close
' Convert vendor checkboxes
Dim iPrint, iPC, iMach
If isprinter = "1" Then iPrint = 1 Else iPrint = 0
If ispc = "1" Then iPC = 1 Else iPC = 0
If ismachine = "1" Then iMach = 1 Else iMach = 0
' Insert new vendor
Dim vendorSQL
vendorSQL = "INSERT INTO vendors (vendor, isactive, isprinter, ispc, ismachine) " & _
"VALUES ('" & newvendorname & "', 1, " & iPrint & ", " & iPC & ", " & iMach & ")"
On Error Resume Next
objConn.Execute vendorSQL
If Err.Number <> 0 Then
Response.Write("
Error creating manufacturer: " & Err.Description & "
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
' Get the new vendor ID
Set rsCheck = objConn.Execute("SELECT LAST_INSERT_ID() as newid")
vendorid = rsCheck("newid")
rsCheck.Close
Else
' Validate existing vendor ID
If Not IsNumeric(vendorid) Or CLng(vendorid) < 1 Then
Response.Write("
Error: Invalid manufacturer ID.
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
End If
' Update vendor's type flags based on model type selection
If modelisprinter = "1" OR modelispc = "1" OR modelismachine = "1" Then
Dim updateVendorSQL, updateParts
updateParts = ""
If modelisprinter = "1" Then
If updateParts <> "" Then updateParts = updateParts & ", "
updateParts = updateParts & "isprinter = 1"
End If
If modelispc = "1" Then
If updateParts <> "" Then updateParts = updateParts & ", "
updateParts = updateParts & "ispc = 1"
End If
If modelismachine = "1" Then
If updateParts <> "" Then updateParts = updateParts & ", "
updateParts = updateParts & "ismachine = 1"
End If
If updateParts <> "" Then
updateVendorSQL = "UPDATE vendors SET " & updateParts & " WHERE vendorid = " & vendorid
objConn.Execute updateVendorSQL
End If
End If
' Check if model already exists for this vendor
checkSQL = "SELECT COUNT(*) as cnt FROM models WHERE LOWER(modelnumber) = LOWER('" & modelnumber & "') AND vendorid = " & vendorid
Set rsCheck = objConn.Execute(checkSQL)
If rsCheck("cnt") > 0 Then
rsCheck.Close
Response.Write("
Error: Model '" & Server.HTMLEncode(Request.Form("modelnumber")) & "' already exists for this manufacturer.
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
rsCheck.Close
' Insert the new model
Dim modelSQL
modelSQL = "INSERT INTO models (modelnumber, vendorid, notes, documentationpath, isactive) " & _
"VALUES ('" & modelnumber & "', " & vendorid & ", '" & notes & "', '" & documentationpath & "', 1)"
On Error Resume Next
objConn.Execute modelSQL
If Err.Number <> 0 Then
Response.Write("
Error: " & Err.Description & "
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
' Get the new model ID
Dim newModelId
Set rsCheck = objConn.Execute("SELECT LAST_INSERT_ID() as newid")
newModelId = rsCheck("newid")
rsCheck.Close
objConn.Close
If newModelId > 0 Then
Response.Write("
Model added successfully!
")
Response.Write("
Model '" & Server.HTMLEncode(Request.Form("modelnumber")) & "' has been added.
")
Response.Write("
Add Another Model ")
Response.Write("Add Printer ")
Response.Write("Add Machine
")
Else
Response.Write("
Error: Model was not added successfully.
")
Response.Write("
Go back")
End If
%>