Hello All, A very newbie question. I have a number of forms with over 100 fields which need to go into database. Is there a way I could insert the data into the table without giving individual fields. Thanks
where the user going to input the value of the field then? -.-" there's no information. unless you don't need all 100 then sure.. :-/
you shouldnt have created such a large form then (i'd hate to fill that one out) but to answer your question, yes you need to address each one individually.
I'm not sure your question is that clear... Are you asking... 1) In my insert query do I have to mention every single field by name? No, providing you are inserting all the fields in the table, you can skip the name of them and just say the values. In other words, rather than: Insert into Table ('field 1','field 2','field 3'.....) VALUES ('value 1','value 2','value 3'....); You can just say: Insert into Table VALUES ('value 1','value 2','value 3'.....); 2) If you were asking do you have to mention all 100 if only inserting a few of them, then no you don't but you do, in that case, need to reference the individual columns.... Insert into Table ('field 5','field 10','field 15'.....) VALUES ('value 5','value 10','value 15'....); Of course, that assumes the columns are nullable. However, in reality, you'd write a loop to build the SQL syntax rather than type it all out, no?
thanks ecentricNick. what i am looking for is an easier way to insert the data into table than using Insert into Table ('field 1','field 2','field 3'.....) VALUES ('value 1','value 2','value 3'....); or Insert into Table VALUES ('value 1','value 2','value 3'.....); "However, in reality, you'd write a loop to build the SQL syntax rather than type it all out, no?" How do i do this ..
Ok, off the top of my head (so you'll have to debug the syntax!) something like.... /* ** Initialise the column and values variables */ $myColumns=""; $myValues=""; /* ** Loop thru all 100 possible form entries ** Where the form fields are named ** entryField0 thru entryField99 ** And the database fields are named Field0 thru Field99 */ for(int i=0;i<100;i++){ /* ** If the user filled out field "i"... */ if ($_POST['entryField'.i]!=''){ /* ** If first one, don't need a comma at the start */ if(i==0){ $myColumns.='Field'.i; $myValues.="'".$_POST['entryField'.i]."'"; }else{ $myColumns.=',Field'.i; $myValues.=",'".$_POST['entryField'.i]."'"; } } } $myInsertQuery="Insert INTO myTable (".$myColumns.") VALUES (".$myValues.");"; PHP: That's a bit rough and ready but it should build your insert statement. It can be done in less lines of code by using some array functions but that wouldn't have been as readable.