Insert query

Discussion in 'PHP' started by kichus, Jul 10, 2007.

  1. #1
    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
     
    kichus, Jul 10, 2007 IP
  2. ProgrammersTalk

    ProgrammersTalk Peon

    Messages:
    684
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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.. :-/
     
    ProgrammersTalk, Jul 10, 2007 IP
  3. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #3
    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.
     
    ansi, Jul 11, 2007 IP
  4. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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?
     
    ecentricNick, Jul 11, 2007 IP
  5. kichus

    kichus Peon

    Messages:
    188
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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 ..
     
    kichus, Jul 11, 2007 IP
  6. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
     
    ecentricNick, Jul 11, 2007 IP