Results 1 to 7 of 7

Thread: Multiple Row Insert

  1. #1
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Multiple Row Insert

    I've created a Database schema that will allow the admin to easily update thier form. To do this I am using the data from the DB to dynamically create the form. For example, if the admin wants to add a new input to the form, they can do that by giving it a name and a type, such as text or radio.

    I've written code that displays the form based on the data. I'm stuck on getting the results from the form inserted back to the DB. Hopefully I can explain this well enough without having to post all the details.

    I have one table that is the responses from the form. Each row contains 5 columns that represent the request, the question, the option chosen, any details from the option chosen (text from a text field) and a customer id.

    I need to do an insert that will create a new row for the user's response to each question. I'm having some difficulty creating this mutiple row insert. Here is what I have so far. I'm hoping it's just a syntax issue...but it could be more. Thanks in advance.

    PHP Code:
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) 
    {
    foreach (
    $_POST as $var => $value

        if (
    $value != NULL)
        {
      
    $insertSQL sprintf("INSERT INTO response (request_ID, questions_ID, options_ID, details, customer_ID) VALUES (%s, %s, %s, %s, %s)"
                            
    GetSQLValueString($_POST['rid'], "int"), 
                           
    GetSQLValueString($var"int"), 
                           
    GetSQLValueString($value"int"), 
                           
    GetSQLValueString($value"text"), 
                           
    GetSQLValueString($_POST['cid'], "int"));
                           
      
    mysql_select_db($database_connDBName$connDB);
      
    $Result1 mysql_query($insertSQL$connDB) or die(mysql_error());
         }
    }


  2. #2
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    I didn't really explain the result. Nothing is inserted into the DB and everything that appeared on the page before adding the insert now no longer appears.

  3. #3
    Join Date
    Jul 2007
    Location
    Irmo, SC
    Posts
    96
    Thanks
    10
    Thanked 7 Times in 7 Posts

    Default

    nothing is showing up period, white screen? ie php syntax error? or you just are not getting the results you are looking for

    shouldnt this
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1"))

    be this

    if ((isset($_POST["MM_insert"]) && ($_POST["MM_insert"] == "form1"))

  4. #4
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Correct. Nothing is showing up, blank screen.

    I'm pretty sure the original is correct:
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1"))

    Still haven't found the error.

  5. #5
    Join Date
    Jul 2007
    Location
    Irmo, SC
    Posts
    96
    Thanks
    10
    Thanked 7 Times in 7 Posts

    Default

    you are correct on the if, the structure is right... I may have been looking out of only 1 eye before

    I placed the code you have above in a small test.php file and I am able to echo out test statements so I think your code is syntactically ok.

    any chance you can put the whole script up? url to the page?

  6. #6
    Join Date
    Jul 2007
    Location
    Irmo, SC
    Posts
    96
    Thanks
    10
    Thanked 7 Times in 7 Posts

    Default

    also is that your entire post or submit section once the form is submitted? no header redirect or output of anything?

  7. #7
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by gpigate View Post
    you are correct on the if, the structure is right... I may have been looking out of only 1 eye before

    I placed the code you have above in a small test.php file and I am able to echo out test statements so I think your code is syntactically ok.

    any chance you can put the whole script up? url to the page?
    Thanks for the help.

    The form is very dynamic and so I think the whole script wouldn't be very helpful. I found an error I had and have made some changes to the original code. If a solution isn't apparent after this, I will do my best to add some details to the code and post the rest of the script.

    Quote Originally Posted by gpigate View Post
    also is that your entire post or submit section once the form is submitted? no header redirect or output of anything?
    After insert it should redirect. Here is the new code along with the redirect.

    PHP Code:
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) 
    {
    foreach (
    $_POST as $var => $value

        if (
    $value != NULL)
        {
            
            
    $combinedID explode("-",$var)); 
    // $var is the input name, for text inputs the name is questions_ID followed by a dash followed by options_ID so we must split them apart to get the values. Example $var is 10-101 for text inputs
            
    if ($combinedID[1] != "" && $combinedID[1] != NULL)
            {
                
    $insertSQL "INSERT INTO response (request_ID, questions_ID, options_ID, details, customer_ID) VALUES ('".mysql_real_escape_string($_POST['rid'])."', '".(int)$combinedID[0]."', '".(int)$combinedID[1]."','".$value."','".mysql_real_escape_string($_POST['cid'])."')";
            }
            else 
    // $var is the input name, $value is the input value, for radio inputs the name is the questions_ID and the value is the options_ID, radio inputs have no details. Example $var is 10, $value is 101 for radio inputs
            
    {
              
    $insertSQL "INSERT INTO response (request_ID, questions_ID, options_ID, details, customer_ID) VALUES ('".mysql_real_escape_string($_POST['rid'])."', '".(int)$var."', '".(int)$value."','".NULL."','".mysql_real_escape_string($_POST['cid'])."')";
            } 
            
            
    mysql_select_db($database_connDBName$connDB); 
      
    $Result1 mysql_query($insertSQL$connDB) or die(mysql_error()); 
         }
    }


    $insertGoTo "thanks.php";
      if (isset(
    $_SERVER['QUERY_STRING'])) {
        
    $insertGoTo .= (strpos($insertGoTo'?')) ? "&" "?";
        
    $insertGoTo .= $_SERVER['QUERY_STRING'];
      
    header(sprintf("Location: %s"$insertGoTo)); 
    Last edited by jgifford; 01-15-2009 at 08:50 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •