Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2007
    Location
    Washington
    Posts
    86
    Thanks
    3
    Thanked 2 Times in 2 Posts

    saving fields in database using php

    The code below saves top portion of the order form. What I need is to save 240 other fields, they are named and shown as:

    each row is textbox_r1_c1, textbox_r1_c2, textbox_r1_c3, textbox_r1_c4, textbox_r1_c5, textbox_r1_c6, textbox_r1_c7, textbox_r1_c8, textbox_r1_c9
    next row is textbox_r2_c1 thru textbox_r2_c9
    3rd row is textbox_r3_c1 thru textbox_r3_c9
    4th row is textbox_r4_c1 thru textbox_r4_c9
    5th...
    6th...
    and so on to a total of 240 fields....

    How would I go about saving those specific fields into the database as well as the fields noted below? Or maybe I can save all fields and not the hidden ones or the ones I dont want saved?

    Code:
    $fieldstoinsert=array('purchaser', 'orderno', 'contact', 'shipto', 'street', 'city', 'state', 'zip', 'orderdate', 'reqshipdate', 'shipviaUPS', 'shipviaTC', 'shiptotel', 'dealertel', 'mobiletel', 'specinstr', 'blumotion', 'casetape', 'caseconst', 'drwbox', 'state', 'RosBox', 't2', 't3', 't4', 't5', 't6', 't7', 't8');
        
    $query = "INSERT INTO formdata (userid, fieldid, value, saveas) VALUES";
    
    foreach($_POST as $fieldid => $value){
        if(in_array($fieldid,$fieldstoinsert)){
            if(trim($value)!=""){
               $query.= "('$userid', '$fieldid', '$value', '$saveas'),";
    
             }
        }
    }  
    
    $query=substr($query,0,-1);
    "The one closer to the truth is the one still learning"

  • #2
    New Coder
    Join Date
    Oct 2007
    Location
    Washington
    Posts
    86
    Thanks
    3
    Thanked 2 Times in 2 Posts
    ok I figured it would be a nested loop.
    now, I just need to figure out how to implement it to save

    1st concern) Currently I have and is still not saving the 1 thru 240 fields, what am I doing wrong?

    2nd concern) Is there a way to check to see if value is already there for a particular user and save over it instead of adding to the database?

    PHP Code:
    for ($i=0$i<=240$i++) {
         for (
    $j=0$j<=9$j++) {

    $fieldstoinsert=array('purchaser''orderno''contact''shipto''street''city''state''zip''orderdate''reqshipdate''shipviaUPS''shipviaTC''shiptotel''dealertel''mobiletel''specinstr''blumotion''casetape''caseconst''drwbox''state''RosBox''t2''t3''t4''t5''t6''t7''t8',  '"textbox_r".$i."_c".$j');
        

    $query "INSERT INTO formdata (userid, fieldid, value, saveas) VALUES";

    foreach(
    $_POST as $fieldid => $value){
        if(
    in_array($fieldid,$fieldstoinsert)){
            if(
    trim($value)!=""){
               
    $query.= "('$userid', '$fieldid', '$value', '$saveas'),";

             }
        }
    }  

         }

    "The one closer to the truth is the one still learning"

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,856
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    1st concern) Currently I have and is still not saving the 1 thru 240 fields, what am I doing wrong?
    Have you added proper error checks to your query statement?
    2nd concern) Is there a way to check to see if value is already there for a particular user and save over it instead of adding to the database?
    You might need to look into "INSERT IGNORE" or "ON DUPLICATE KEY UPDATE"
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    New Coder
    Join Date
    Oct 2007
    Location
    Washington
    Posts
    86
    Thanks
    3
    Thanked 2 Times in 2 Posts
    1st concern is solved!

    2nd concern: Its still not updating, basically I want it to save if there is a new saveas and update if there is already an existing saveas


    example
    PHP Code:
    $fieldstoinsert=array('purchaser''orderno''contact''shipto''street''city''state''zip''orderdate''reqshipdate''shipviaUPS''shipviaTC''shiptotel''dealertel''mobiletel''specinstr''blumotion''casetape''caseconst''drwbox''state''RosBox''t2''t3''t4''t5''t6''t7''t8');
        

    $query "INSERT INTO formdata (userid, fieldid, value, saveas) VALUES ON DUPLICATE KEY UPDATE WHERE saveas=saveas";

    foreach(
    $_POST as $fieldid => $value){
        if(
    in_array($fieldid,$fieldstoinsert)){
            if(
    trim($value)!=""){
               
    $query.= "('$userid', '$fieldid', '$value', '$saveas'),";

             }
        }

    "The one closer to the truth is the one still learning"


  •  

    Posting Permissions

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