Jump to content

Archived

This topic is now archived and is closed to further replies.

devofash

Dealing with alot of form fields

Recommended Posts

hey ppl.... got another question ... hope you can help me out.

here it goes.. say if i got a form with 40 fields and i want to validate and insert them into the database... Right now the only way i know is i got to do

[code]
<form action='whatever.php' method='post'>
  <input type='text' name='fname'>
  <input type='text' name='lname'>
  .....
  .....
  <input type='submit' value='submit'>
</form>
[/code]

[code]
<?
  ....
  ...
  // do error checking, if everything is ok, insert data

   $sql = "INSERT INTO table_name ($fname, $lname ......)
              VALUES ('$_POST[fname]', '$_POST[lname]' ....)";
[/code]

now 40 form fields and inserting/validating is gonna be long ..... there's gotta be a simpler way to do this. maybe get form data into an array or something validate it and then enter them into the db...

but i'm a newbie dont really know how to do it......... can anyone provide me with an snipplet (with explaination) on how i can go about doing this

thanx in advance

Share this post


Link to post
Share on other sites
As long as the names of the fields in you form match the names of the fields in your database you can do something like:
[code]<?php
if (isset($_POST)) {
   $qtmp = array();
   foreach ($_POST as $k => $v)
       switch($k) {
            case 'submit':
                break; // do nothing
//
//  group together all fields that can contain text
//
            case 'lname':
            case 'fname':
            case 'address':
            case 'city':
            case 'state':
            case 'emailaddr':
            case 'phone':
            case 'fax':
                $qtmp[] = $k . "='" . mysql_real_escape_string(trim(stripslashes($v))) . "'";
                break;
//
//  keep listing fields
//
           case 'zipcode':
           case 'someothernumber':
                $qtmp[] = $k . "='" trim($v) . "'";
                break;
//
//  etc...
//
       }
       $q = 'insert into tablename set ' . implode(', ' . $qtmp);
       $rs = mysql_query($q) or die('Error with query: ' . $q . '<br />' . mysql_error());
?>[/code]

Ken

Share this post


Link to post
Share on other sites
thanx..........
ermm could you plz explain some thing though .... what's the switch and all those cases for ... do i need em ??

Share this post


Link to post
Share on other sites
does anyone also have a link to tutorails or something which could explain this method !!

Share this post


Link to post
Share on other sites
Look at the section in the manual for the [a href=\"http://www.php.net/switch\" target=\"_blank\"]switch[/a] statement.

The "case" lines are part of the switch syntax.

You need one case line for each field in your form.

Ken

Share this post


Link to post
Share on other sites
ooo i know about switch and case ........ i just wanted to know why i need it in this method......

and you code gives and error

[code]Warning: implode() [function.implode]: Argument to implode must be an array. on line 48 [/code]

Share this post


Link to post
Share on other sites
I put a period instead of a comma on this line [code]<?php $q = 'insert into tablename set ' . implode(', ' . $qtmp);?>[/code] It shoud be [code]<?php$q = 'insert into tablename set ' . implode(', ' , $qtmp); ?>[/code]

I use the "foreach" and "switch" statements here when the form fields have differenct attributes like text, numbers, must be entered, dates, etc. If all of your fields in the form are of one type you don't need the switch statement, just do something like:
[code]<?php
$qtmp[] = array();
foreach($_POST as $k => $v)
   if ($k != 'submit') // don't do anything with submit button
      $qtmp[] = $k . " = '" . mysql_real_escape_string(trim(stripslashes($v))) . "'";
$q = 'insert into tablename set ' . implode(', ' , $qtmp);
$rs = mysql_query($q) or die('Error with query: ' . $q . '<br />' . mysql_error());
?>[/code]

Ken

Share this post


Link to post
Share on other sites
thanx for the explination ermm....... still getting an error though

[code] Error with query: insert into test set Array, fname = 'john', lname = 'smith', address = 'none', email = 'me@me.com'
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' fname = 'john', lname = 'smith', address = 'none', email = 'me@me.com'' at line 1 [/code]

[code] normally in insert u have
$q = "SELECT INTO table VALUES ....";  
[/code]

but the code above doesnt could that be the reason ?? soz this method is very new to me ..... i cant really spot wht's wrong with the insert..statement ..

Share this post


Link to post
Share on other sites
The problem is the word "Array" in the insert command.

My mistake.

Change the line [code]<?php $qtmp[] = array(); ?>[/code] to [code]<?php $qtmp = array(); ?>[/code]

The format of the insert command that this code is generating is an alternative format.

Ken

Share this post


Link to post
Share on other sites
works perfectly :D thanx.........

got another question regarding the same topic ofcourse

so for e.g. .... if i have a form with "10 combo boxs", "10 text boxes", "10 hidden fields", and "10 radio buttons" ...... so that would mean i have to use the "switch case" thing....... and write em out seperately like so:

[code]
// some code
switch($k) {
            case 'submit':
                break; // do nothing
            // 10 text boxes
            case 'lname':
            case 'fname':
            case 'address':
               $qtmp[] = // the rest
            break;
           // 10 combo boxes
          case 'department':
          case 'jobs':
              $qtmp[] = // the rest
         break;
         // etc ... etc...
[/code]

is that right ???

Share this post


Link to post
Share on other sites
What are "combo boxes"?

You need a set of case statements for each different type of value expected, so if all the fields send strings, you can use the same case block, it doesn't matter how the form field are defined.

One of the exceptions to this are fields where you expect a date to be entered in free format and you need to store it in the database in the YYYY-MM-DD format, then you would need a special case statement and special processing.

Ken

Share this post


Link to post
Share on other sites
combo boxes = drop down list....

so if i had 10 checkboxes, 10 radio boxes.... 10 text boxes... and 10 hidden fields ...... then i'll need to specify .... a case for each form field ??

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.