Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2006
    Posts
    4

    Unanswered: How to save array of records into the DB

    Guyz... i really need your help so badly.This thing is really complicated for me.

    I have these codes below for you to refer on what is going on with it but i think my problem is during saving only... Well anyway, basically I had a row with 3 elements or columns(Partname,Partnumber,version). Everytime I clicked the "Add Part" button, it will call the javascript addPart() function in which it will add another row of elements. The values on each element of every row is stored in an array variable. When Im going to click submit button, all the data inputted to every columns should be saved into the database. The problem is, it doesn't work.... maybe there something wrong with my code during saving.


    This my Javascript code:
    Code:
    <script language="JavaScript" type="text/javascript">
    
            function addPart()
            {
                    var tbl = document.getElementById('myTable')
                    var nRow = tbl.rows.length - 13
                    var row = tbl.insertRow(nRow)
                    var blankCell = row.insertCell(0)
                    var addCell = row.insertCell(1)
                    var PartText = new Array(3)
                    var Append = addCell.appendChild
                    var Caption = document.createTextNode
    
                    for(var i=1;i<=3;i++)
                    {
                            PartText[i] = document.createElement('input')
                            PartText[i].type = 'text'
                            PartText[i].id = 'addPart[]'
                            PartText[i].name = 'addPart[]'
                    }
    
                    PartText[1].value ='<?=$addpartname?>'
                    PartText[2].value ='<?=$addpartnumber?>'
                    PartText[3].value ='<?=$addversion?>'
    
                    blankCell.bgColor="#f1f1f1"
                    addCell.bgColor="#f1f1f1"
    
                    Append(Caption('       Part Name: '))
                    Append(PartText[1])
                    Append(Caption('       Part Number: '))
                    Append(PartText[2])
                    Append(Caption('       Version: '))
                    Append(PartText[3])
            }
    My HTML code:
    Code:
    <tr>
    <td bgcolor='#f1f1f1'></td>
    <td bgcolor='#f1f1f1' align='left'>
    
    Part Name: <input type="text" class='bginput'name="addPart[]"  id='addPart[]' value='<?=$addpartname?>
    
    Part Number: <input type="text" class='bginput' name="addPart[]" id='addPart[]' value='<?=$addpartnumber?>'/>
                            
    Version: <input type="text" class='bginput' name="addPart[]"  id='addPart[]' value='<?=$addversion?>'/>
    
    </td>
    </tr>
    
    <tr>
                    <td bgcolor='#f1f1f1' align='right'>
                            <input type="button" name='add' value="Add Part" onclick=addPart() />
                    </td>
    </tr>
    
    <tr>
           <td>
                    <input type='submit' name="submit" value="Submit">
                    <input type='reset' value="Reset">
           </td>
    </tr>
    My code during saving...
    Code:
    for($i=1;$i<=3;$i++)
    {
          $addpartname=$_POST['addPart'][$i];
          $addpartnumber=$_POST['addPart'][$i];
          $addversion=$_POST['addPart'][$i];
    
          $q= "INSERT INTO PARTS (partname, partnumber, version) VALUES ('$addpartname','$addpartnumber','$addversion')";
          
           mysql_query($q);
    }

  2. #2
    Join Date
    Aug 2006
    Location
    San Francisco, CA
    Posts
    136
    The first thing I would do is after:

    PHP Code:
    for($i=1;$i<=3;$i++)
    {
          
    $addpartname=$_POST['addPart'][$i];
          
    $addpartnumber=$_POST['addPart'][$i];
          
    $addversion=$_POST['addPart'][$i]; 
    add the following for testing:

    PHP Code:
    print( $addpartname);
    print( 
    $addpartnumber);
    print( 
    $addversion); 
    This should display the info perviously inserted. If nothing is displayed, then you know that the variables where never assigned correctly. I think they might not be assigned correctly cause I think you might need double quotes instead of single quotes in the $_POST().


    PHP Code:
    $q"INSERT INTO PARTS (partname, partnumber, version) VALUES ('$addpartname','$addpartnumber','$addversion')";
      
        print(
    $q);

           
    mysql_query($q);

    you should also take the output from query and run it against the database in some other program like sql query analyzer just to check syntax.

    Hope this helps,
    Good luck,
    Reghardt

Posting Permissions

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