homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Input Form ?

 5:14 am on Jul 1, 2010 (gmt 0)


Right now i have no problem adding data to any tables that i have in my database. But everytime i have to changed the Post Value and the SQL insert command in my add.php for the specific table. For eg i have to change the field value 'item and 'date' below to be included in add.php.
And if i have other table that have more than 3 or 4 field i have to make another add.php

So i was wondering is there any possibility to have one form input and add.php that can track the numbers of fields in any tables we created without manually changed the field in the add.php

<table width="400" border="0" align="center" cellpadding="0" cellspacing="1">
<td class="tdadd_bg"><form name="form1" method="post" action="add.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<td><font class="fontadd">Product </font></td>
<td><input name="item" type="text" id="item" value=""></td>
<td><font class="fontadd">Date</font></td>
<td><input name="date" type="text" id="date" value=""></td>

<td align="center"><input type="submit" name="Submit" value="ADD SUBMIT"></td>




 6:07 am on Jul 1, 2010 (gmt 0)

If the database is already configured you could read the tables from DB and output this to create the actual form...

The same would apply for add.php where it does the actual insert.

Or have I misunderstood?



 9:16 am on Jul 1, 2010 (gmt 0)


Indeed the add.php i have to output the sql query for insert to my Table productlist,
eg $sql="INSERT INTO productlist (item, date) VALUES ('$item', '$time')";

But if i have other Table Customer with field like 'clientname' 'address' shipping etc, i still need to change my add.php to different sql statement

eg: $sql="INSERT INTO customer(client, address, shipping) VALUES ('$client', '$address', '$shipping')";

Basically i have to create 2 scripts of add.php and 2 type of form submission for the relevant fields

Or can we use one form submission and add.php for the 2 or 3 tables?


 10:13 am on Jul 1, 2010 (gmt 0)

I was thinking about using php list tables [php.net...] to get the table names, then dynamicaly generate your form using the table names.. that way if there is an aditional table the form could be built based on what tables are in the actual DB?



 10:41 am on Jul 1, 2010 (gmt 0)

hi Mack,

I can output all the tables fields alright on my submission form like the sample code below but i have no idea how to post the value to my add.php script. Can you help me out by showing me coding the correct format the add.php should be?

for ($i = 0; $i < mysql_num_fields($result); ++$i) {
$field = mysql_field_name($result, $i);

echo "<td>";

echo "Add $field</td>";
echo "<td>:</td>";
echo "<td><input name=\"$field\" type=\"text\"id=\"$field\" size=\"120\" value=\"\"></td>";
echo "</tr>";




 5:05 pm on Jul 1, 2010 (gmt 0)

So i was wondering is there any possibility to have one form input and add.php that can track the numbers of fields in any tables we created without manually changed the field in the add.php

I love this line of thinking. :-) There's **probably** an implementation for it in the Zend framework, but I do this in Perl and PHP for any system that will need to expand and evolve without having to recode it. But in my approach it requires a bit of preparation.

Your database has a joined table for data type that corresponds to any table. You can approach this a number of ways, but a decent one is something like this:

id|active|fname|lname|email|comments (etc.)


One might say you can just extract the mySQL data type (varchar, boolean, text, etc.) and use that as a trigger for what form field to generate, but there are cases where that's not enough info. A boolean field can be for a set of radio buttons OR a checkbox, for example.

The second thought is an extra table is a bit of overhead, why not just include the data type field in the customers table? The data table is only queried when you need a form to modify records. The actual data itself may be used publicly and more frequently, it's to your advantage to keep it lean and mean.

So to generate your form you have

- Via a session variable or some other mechanism, determine which table you're inserting or updating.

- A function to get field names of the table. For utmost security, it's a **really good idea** to not use the actual field names as input form names, these should be aliased. Solution: another table, or store the alias in the data_types table (which explains "field_alias".)

- A function to get the data types for each field and return an array of strings for each form field. Using the sequence field in data_types, store the form items in the array in the order you want them. Example, the field "active" is a boolean or tinyint(1), it's supposed to be a radio array and at the top of the form, so your function generates a string containing the radio buttons and it is in position 0 of the array.

- Foreach through the array, output the form.

Take it to the next level: you use this same kind of anonymous approach in doing your updates and inserts, no more hard coded select statements. Though the example below is not working code, and demonstrates no variable cleansing (hint: clean $_POST on input so you don't need to do variable naming) it will give you an idea:

// Associative array of aliases => actual fields.
$fields = get_field_names($tablename);
foreach ($fields as $alias=>$tablefield) {
if (isset($_POST[$alias])) {
$flds .= "$tablefield,";
$vals .= "'" . $_POST[$alias] . "',";
if ($vals and $flds) {
$flds = preg_replace('/,$/','',$flds); // or other chop
$vals = preg_replace('/,$/','',$vals);
$query = "insert into $tablename ($flds) values($vals)";

There are many challenges to this approach, but they're all workable; for example, what if the radio is not boolean, it has more than two possible multiple values, and which one is checked by default? It may seem like more trouble than it's worth, but down the road when you add a new field or a new table, it becomes a very sound investment.


 11:57 pm on Jul 1, 2010 (gmt 0)

Thanks Rocknbil for your explanation to my issue here. Unfortunately i'm a php newbie and ain't good enough on my coding so i'm very lost how to go about them. Would you be kind to show me the working code please?



Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved