#function add_php dbase_id table_id
#query select DatabaseName from `database` where DatabaseID=?dbase_id?
#query select TableID, Subject, Title, Checks, Session, TableName, IDfield from `tables` where TableID=?table_id?
#set orderfield='0'
#query select `FieldID` as orderfield, FieldName as ordername from `fields` where `TableID`=?table_id? and `Type`='ordering'
#print page_header 'add' dbase_id table_id
#print 'Create(\"' ^ DatabaseName ^ '\");\n'
#record select fieldID, `Add`, Name, FieldName, `Type`, DisplayName, DisplayLength, Help, areaheight, areawidth, LookupTable, OrderByField, LookupIDField, SessionVar, LookupDisplayField from `fields` where TableID=?table_id? and LookupTable<>'' and SessionVar<>'' and DisplayName='' order by `order`
#print '$sql->QueryItem(\"SELECT `' ^ LookupDisplayField ^ '` FROM `' ^ LookupTable ^ '` WHERE `' ^ LookupIDField ^ '` = $' ^ SessionVar ^ '\");\n'
#print '$' ^ Name ^ ' = htmlspecialchars($sql->data[0]);\n'
#end
#set firstfield='none'
#query select FieldName as firstfield from `fields` where TableID=?table_id? and Record<>'none' and FieldName<>'' order by `order` limit 1
#print 'if ($_POST[\'' ^ firstfield ^ '\']!=\'\') {\n'
#record select Name, FieldName, `Type`, DisplayName, DisplayLength, areaheight, areawidth, LookupTable, OrderByField, LookupIDField, LookupDisplayField from `fields` where TableID=?table_id? and Record<>'none' and FieldName<>'' order by `order`
#if Type='varchar' or Type='text'
#print ' $' ^ Name ^ ' = ($_POST[\'' ^ FieldName ^ '\']);\n'
#else
#if Type='date'
#print '$' ^ Name ^ ' = date(\'j/n/Y\', strtotime($_POST[\'' ^ FieldName ^ '\']));\n'
#else
#print '$' ^ Name ^ ' = $_POST[\'' ^ FieldName ^ '\'];\n'
#end
#end
#end
#print '}\n'
#print '?>\n'
#print '\n'
#print '
\n'
#print ' \n'
#print ' ' ^ Title ^ ' toevoegen\n'
#print '\n'
#print '\n'
#record select Code from actions where TableID=?table_id? and Action='add' and Part='header'
#print Code ^ '\n'
#end
#print '\n'
#record select Code from actions where TableID=?table_id? and Action='add' and Part='footer'
#print '\n'
#end
#print '\n'
#print '\n'
#end
#function postadd_php dbase_id table_id
#query select DatabaseName from `database` where DatabaseID=?dbase_id?
#query select TableName, TableID, Checks, Session, Subject, IDfield, Include, ListOrderByField from `tables` where TableID=?table_id?
#set orderfield='0'
#query select `FieldID` as orderfield, FieldName as ordername from `fields` where `TableID`=?table_id? and `Type`='ordering'
#print page_header 'postadd' dbase_id table_id
#print 'Create(\"' ^ DatabaseName ^ '\");\n'
# remove return form : better implementation needed with no $_POST inside input html code
# print 'echo \"\\n\";\n'
#record select `Add`, Name, FieldName, `Type`, DisplayName, `default`, DisplayLength, SessionVar, areaheight, areawidth, LookupTable, OrderByField, LookupIDField, LookupDisplayField from `fields` where TableID=?table_id? and SessionVar='' and FieldName<>'' and FieldName<>?IDfield? order by `order`
#print '$' ^ FieldName ^ '=quote($_POST[\'' ^ FieldName ^ '\']);\n'
#if LookupTable<>'' and FieldName=''
#if default<>''
#print 'if ($' ^ Name ^ '<>\'\\\'' ^ default ^ '\\\'\') {\n'
#end
#print '$sql->Query(\"SELECT '
#print queryvars 'T' LookupDisplayField '0' 1 ^ ', T.' ^ LookupIDField
#print ' FROM `' ^ LookupTable ^'` as T \n'
#set wcheck=''
#query select Checks as wcheck from `tables` where databaseID=?dbase_id? and TableName=?LookupTable?
#if wcheck<>''
#print 'WHERE ' ^ fieldreplace wcheck '0' ^ '\n'
#end
#print 'and `' ^ LookupIDField ^ '`=$' ^ Name
#print '\");\n'
#print 'if ($sql->rows==0) { die (\"No related record field: ' ^ Name ^ ' value: $' ^ Name ^ '\"); }\n'
#if default<>''
#print '}\n'
#end
#end
#end
#if orderfield<>'0'
#set where='1'
#record select FieldName, SessionVar from `fields` where TableID=?table_id? and SessionVar<>'' and FieldName<>?IDfield? and FieldName<>'' order by `order`
#if where='1'
#set where=''
#else
#set where=where ^ ' AND '
#end
#set where=where ^'`' ^ TableName ^ '`.`' ^ FieldName ^ '`=\'$' ^ SessionVar ^ '\''
#end
#print 'if ($' ^ ordername ^ '<>\'\') {\n'
#print ' $sql->Query(\"UPDATE `' ^ TableName ^ '` SET `' ^ ordername ^ '`=`' ^ ordername ^ '`+1 WHERE `' ^ ordername ^ '`>=$' ^ordername ^' AND ' ^ where ^'\");\n'
#print '} else {\n'
#print ' $sql->QueryItem(\"SELECT `' ^ ordername ^ '`+1 as next FROM `' ^ TableName ^ '` WHERE ' ^ where ^ ' ORDER BY `' ^ ordername ^ '` DESC LIMIT 1\");\n'
#print ' if (!$sql->data[next]) {\n'
#print ' $' ^ ordername ^'=1;\n'
#print ' } else {\n'
#print ' $' ^ ordername ^ '=$sql->data[next];\n'
#print ' }\n'
#print '}\n'
#end
#print '$sql->Insert(\"INSERT INTO `' ^ TableName ^ '`\n'
#print ' (\n'
#set first=true
#record select `Add`, FieldName, `Type`, DisplayName, DisplayLength, areaheight, areawidth, LookupTable, OrderByField, LookupIDField, LookupDisplayField, SessionVar from `fields` where TableID=?table_id? and FieldName<>'' and FieldName<>?IDfield? order by `order`
#if first
#set first=false
#else
#print ',\n'
#end
#print ' `' ^ FieldName ^ '`'
#end
#print '\n )\n'
#print ' VALUES (\n'
#set first=true
#record select `Add`, FieldName, `Type`, DisplayName, DisplayLength, areaheight, areawidth, LookupTable, OrderByField, LookupIDField, LookupDisplayField, SessionVar from `fields` where TableID=?table_id? and FieldName<>'' and FieldName<>?IDfield? order by `order`
#if first
#set first=false
#else
#print ',\n'
#end
#if SessionVar<>''
#print ' \'$' ^ SessionVar ^'\''
#else
#print ' $' ^ FieldName
#end
#end
#print '\n )\");\n'
#print '$affected_rows = $sql->a_rows;\n'
#print 'echo \"\";\n'
#print 'echo \"Er is/zijn $affected_rows records toegevoegd
\";\n'
#record select `Add`, FieldName, `Type`, DisplayName, DisplayLength, areaheight, areawidth, LookupTable, OrderByField, LookupIDField, LookupDisplayField from `fields` where TableID=?table_id? order by `order`
#if Add='true'
#print 'echo \"$' ^ FieldName ^ '
\";\n'
#end
#end
#print 'echo \"
\";\n'
#print 'if ($affected_rows>0) {\n'
#print ' echo \"\";\n'
#print ' echo \"\";\n'
#print '}\n'
#print '$_SESSION[\'last_'^TableID^'\']='
#record select `Add`, FieldName, `Type`, DisplayName, DisplayLength, areaheight, areawidth, LookupTable, OrderByField, LookupIDField, LookupDisplayField, SessionVar from `fields` where TableID=?table_id? and FieldName<>'' and FieldName<>?IDfield? and FieldName=?ListOrderByField?
#if SessionVar<>''
#print '\'$' ^ SessionVar ^'\';\n'
#else
#print '$' ^ FieldName ^ ';\n'
#end
#end
#print ';\n'
#print '?>\n'
#print '\n'
#end