// SQL Form Edit // // Written by Chris Davies // http://daviesinc.com/modules/ // v 0.1, 2002-02-20 int thread_safe=1; #include inherit "module"; inherit "caudiumlib"; constant module_type = MODULE_LOCATION; constant module_name = "SQL Form Edit: Form Data Saved to SQL"; constant module_doc = #"This MODULE_LOCATION allows you to do easy editing of SQL data via forms.

The hidden field unique defines the SQL field name in the table that defines the where clause in the SQL Update.
The hidden field tablename defines the SQL table that is being updated.
The hidden field successpage defines the page that the person will be sent to after a successful update.
The hidden field errorpage defines the page where a surfer will be sent if there is an error during the SQL Update.

An example form follows:

\n\n <form method=\"post\" action=\"/form/sqlsave\">
<input type=\"hidden\" name=\"unique\" value=\"sequence\">
<input type=\"hidden\" name=\"tablename\" value=\"links\">
<input type=\"hidden\" name=\"successpage\" value=\"/success.rxml\">
<input type=\"hidden\" name=\"errorpage\" value=\"/form.rxml\">
<sqloutput query=\"select * from links where sequence=3\">
<input type=\"hidden\" name=\"sequence\" value=\"#sequence#\">
<table>
<tr><td>title</td><td><input type=\"text\" name=\"title\" value=\"#title#\" size=80></td></tr>
<tr><td>descr</td><td><input type=\"text\" name=\"descr\" value=\"#descr#\" size=80></td></tr>
<tr><td>returnlink</td><td><input type=\"text\" name=\"returnlink\" value=\"#returnlink#\" size=80></td></tr>
</table>
</sqloutput>
<input type=\"submit\" value=\"save\">
</form>
"; // If module_unique is set to 1, you would not be able to have multiple // copies of this to process multiple forms. However, this module is // really designed to be data driven and not hardcoded to a particular // setup. constant module_unique = 1; void create() { // // Defining the location tells Caudium what URL to answer for // In this case, the URL that this module would respond to would be // // http://domain.com/form/sqlsave // // This would correspond to

// defvar("location", "/form/sqlsave", "Mount point", TYPE_LOCATION, "Location"); // This is a standard definition for the SQL Server that you will be // updating. Saving this information in the Config Interface keeps // certain information from being available on the web. defvar ("sqldb", "localhost", "SQL server", TYPE_STRING, "This is the host running the SQL server with the " "authentication information.
\n" "Specify an \"SQL-URL\":
\n" "Valid values for \"sqlserver\" depend on which " "sql-servers your pike has support for, but the following " "might exist: msql, mysql, odbc, oracle, postgres.\n", ); } mapping find_file(string f,object id) { // Create an object to talk with the SQL server. If there is a connection // already created by Caudium, this will create a new communications // thread as Caudium will maintain a persistent connection to the SQL // server object db; if (id->conf->sql_connect) db = id->conf->sql_connect(QUERY(sqldb)); else perror("REFER: Error: no connect

\n"); // This defines the field that is the 'auto_increment' or unique field // identifiers to make sure that we are editing and updating the correct // record in the SQL server database string unique = (string)id->variables->unique; // Get the list of field names from the SQL database that is being edited array fields = indices(db->query("select * from "+(string)id->variables->tablename+ " limit 1")[0]); // Walk through the indices to see what field names could be updated in the // edit. Then, remove the tablename.fieldname variables, see which // form values are defined in the form post and update only those fields // that exist in the form that is submitted. string update = ""; foreach (fields,string field) { if (field[0..(sizeof((string)id->variables->tablename))] != (string)id->variables->tablename+".") if (id->variables[field]) update += "," + field + " = '" + db->quote((string)id->variables[field]) + "'"; } // remove the leading , update = update[1..]; // This builds the query that needs to be sent to the SQL server and is // as data driven as possible update = "update " + (string)id->variables->tablename + " set " + update + " where " + (string)id->variables->unique + "='" + (string)id->variables[(string)id->variables->unique] + "'"; // Mysql returns 0 rows updated if there is no change, so the only thing // we can really do here is check to make sure there is no error when // the SQL statement is executed. catch { db->query(update); return http_redirect((string)id->variables->successpage,id); }; return http_redirect((string)id->variables->errorpage,id); }