Hail all from Folsom, Cali...
I have a small problem that I am hoping can be solved...
I have a bit of code that works wonders for me in ASP, and one of my clients has asked that it be changed to PHP... this is plexing me as i can not find any documention on how to do this...
The part that i am finding difficult is this, I dont know what form veriables will be passed in form the form POST, and I dont know what colums will exist in the table. Thus, the code I currently use checks for each column in the database to see if there is an existing form POST variable with a value other then "" <blank> and if there IS a value, then it will be added to the insert od update SQL script that is being built.
So here is the ASP code... perhaps you can make sence of it and tell me if it is possable in PHP/MySQL
function saveFormData(strTable, strDBName)
//returns identity
on error resume next
saveFormData = 0
set objCn = server.CreateObject("ADODB.Connection")
set objCmd = server.CreateObject("ADODB.Command")
set objRS = server.CreateObject("ADODB.Recordset")
strSQL = "select top 1 * from " & strTable
set objRS = getRS(strSQL, strDBName)
for each iField in objRS.Fields
if Request.Form(iField.name) <> "" then
strComma = ""
If strCols <> "" Then strCols = strCols & ", "
If strVals <> "" Then strVals = strVals & ", "
strCols = strCols & iField.name
strVals = strVals & "'" & replace(Request.Form(iField.name),"'","''") & "'"
end if
next
objRS.Close
//Add Date info, These colums SHOULD exist on all tables
If strCols <> "" Then strCols = strCols & ", "
If strVals <> "" Then strVals = strVals & ", "
strCols = strCols & "CREATE_BY, CREATE_DTE"
strVals = strVals & "'" & session("User") & "', '" & now() & "'"
//Execute Insert
strSQL = "insert into " & strTable & " (" & strCols & ") Values (" & strVals & ")"
'Response.Write strSQL
objCn.Open application(strDBName & "_ConnectionString")
objCmd.ActiveConnection = objCn
objCmd.CommandTimeout=999
objCmd.Commandtext = strSQL
objCmd.Execute
if instr(1,err.Description,"Cannot insert duplicate key")>0 then
Response.Write "<h1>CAN NOT ENTER DUPLICATE RECORD</h1>"
Response.Write "<h3>Please ues your browsers Back button to return to the previous page.</h3>"
Response.End
elseif err.number <> 0 then
Response.Write "<h1>SQL Error: " & err.number & " </h1>"
Response.Write "<h3>" & err.Description & "</h3>"
Response.End
end if
//Get Identity
objCmd.ActiveConnection = objCn
objCmd.CommandTimeout=999
objCmd.Commandtext = "SELECT @@IDENTITY AS 'Identity'"
set objRS = objCmd.Execute
saveFormData = objRS("Identity")
objRS.Close