devofash Posted April 18, 2006 Share Posted April 18, 2006 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 thisthanx in advance Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 18, 2006 Share Posted April 18, 2006 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]<?phpif (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 Quote Link to comment Share on other sites More sharing options...
devofash Posted April 18, 2006 Author Share Posted April 18, 2006 thanx.......... ermm could you plz explain some thing though .... what's the switch and all those cases for ... do i need em ?? Quote Link to comment Share on other sites More sharing options...
devofash Posted April 18, 2006 Author Share Posted April 18, 2006 does anyone also have a link to tutorails or something which could explain this method !! Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 18, 2006 Share Posted April 18, 2006 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 Quote Link to comment Share on other sites More sharing options...
devofash Posted April 18, 2006 Author Share Posted April 18, 2006 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] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 19, 2006 Share Posted April 19, 2006 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 Quote Link to comment Share on other sites More sharing options...
devofash Posted April 19, 2006 Author Share Posted April 19, 2006 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 .. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 19, 2006 Share Posted April 19, 2006 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 Quote Link to comment Share on other sites More sharing options...
devofash Posted April 19, 2006 Author Share Posted April 19, 2006 works perfectly :D thanx.........got another question regarding the same topic ofcourseso 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 ??? Quote Link to comment Share on other sites More sharing options...
devofash Posted April 20, 2006 Author Share Posted April 20, 2006 ... Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 20, 2006 Share Posted April 20, 2006 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 Quote Link to comment Share on other sites More sharing options...
devofash Posted April 20, 2006 Author Share Posted April 20, 2006 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 ?? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.