Jump to content


Photo

Dealing with alot of form fields


  • Please log in to reply
12 replies to this topic

#1 devofash

devofash
  • Members
  • PipPipPip
  • Advanced Member
  • 81 posts

Posted 18 April 2006 - 03:28 PM

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

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

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

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

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



#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 18 April 2006 - 03:57 PM

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:
<?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());
?>

Ken

#3 devofash

devofash
  • Members
  • PipPipPip
  • Advanced Member
  • 81 posts

Posted 18 April 2006 - 04:34 PM

thanx..........
ermm could you plz explain some thing though .... what's the switch and all those cases for ... do i need em ??

#4 devofash

devofash
  • Members
  • PipPipPip
  • Advanced Member
  • 81 posts

Posted 18 April 2006 - 08:35 PM

does anyone also have a link to tutorails or something which could explain this method !!

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 18 April 2006 - 08:59 PM

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

#6 devofash

devofash
  • Members
  • PipPipPip
  • Advanced Member
  • 81 posts

Posted 18 April 2006 - 09:24 PM

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

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


#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 April 2006 - 01:42 AM

I put a period instead of a comma on this line
<?php $q = 'insert into tablename set ' . implode(', ' . $qtmp);?>
It shoud be
<?php$q = 'insert into tablename set ' . implode(', ' , $qtmp); ?>

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:
<?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());
?>

Ken


#8 devofash

devofash
  • Members
  • PipPipPip
  • Advanced Member
  • 81 posts

Posted 19 April 2006 - 11:17 AM

thanx for the explination ermm....... still getting an error though

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

normally in insert u have
$q = "SELECT INTO table VALUES ....";  

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 ..

#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 April 2006 - 02:06 PM

The problem is the word "Array" in the insert command.

My mistake.

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

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

Ken

#10 devofash

devofash
  • Members
  • PipPipPip
  • Advanced Member
  • 81 posts

Posted 19 April 2006 - 10:36 PM

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:

// 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...

is that right ???

#11 devofash

devofash
  • Members
  • PipPipPip
  • Advanced Member
  • 81 posts

Posted 20 April 2006 - 01:02 AM

...

#12 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 April 2006 - 01:28 AM

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

#13 devofash

devofash
  • Members
  • PipPipPip
  • Advanced Member
  • 81 posts

Posted 20 April 2006 - 01:34 AM

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 ??




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users