Back to examples |
Allows you to build a quick and simple form and then automatically format a SQL query to insert the data into a specified table. All fields within the form need to be named with the prefix INPUT_ . The rest of the field names relate to the name of the table field in the database.
|
<?php
if(getenv("REQUEST_METHOD")=="POST") { $table_name = "example";
$query = "insert into $table_name "; $vars = NULL; $vals = NULL; while (list ($key, $val) = each ($HTTP_POST_VARS)) { if(substr($key,0,6)=="input_") { $vals = $vals.(!empty($vars)?", ":"")."'".addslashes($val)."'"; $vars = $vars.(!empty($vars)?", ":"").substr($key,6); } } $query.= " ($vars) values ($vals)";
// Use the variable $query within pg_exec } ?>
<form name="form1" method="post" action=""> Name <input name="input_name" type="text" maxlength="15"> Address 1 <input name="input_add1" type="text"> Address 2 <input name="input_add2" type="text"> Town / City <input name="input_town" type="text"> State <input name="input_state" type="text"> ZIP <input name="input_zip"> <input name="input_more" type="checkbox" value="1"> Wants futher communication <input name="add" type="submit" class="PHPBODY" value="Add"> <?php print($query);?> </form>
|
|
|
postgres_3.zip
|