SitePoint Sponsor

User Tag List

Results 1 to 16 of 16

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    Saving multiple array in mysql

    Greetings! Which is the simplest way to save this multiple arrays in one row?

    Code:
     foreach($accnum as $accnums){ 
       echo $accnums.'<br/>';
       }foreach($copy as $cop){ 
        echo $cop.'<br/>';
       }foreach($location as $loc){ 
        echo $loc.'<br/>';
       }foreach($fund as $funds){ 
        echo $funds.'<br/>';
       }foreach($stat as  $stat){ 
        echo $stat.'<br/>';
       }foreach($remarks as $marks){ 
        echo $marks.'<br/>';
       }

  2. #2
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Assuming these are indexed arrays (posted back from a form?) then something like:
    PHP Code:
    for($i 0$i count($accnum); $i++)
    {
      
    $accnums $accnum[$i];
      
    $cop $copy[$i];
      
    etc.
      
    then do a regular insert


  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If you really, really want to save it in a single row, do so by:
    PHP Code:
    $accnumfield "'" implode('|'$accnum) . "'"
    Then to retrieve them and split them up back into an array:
    PHP Code:
    $accnum explode('|'$row['accnum']); 
    However I would strongly recommend against storing it in one row. Doing that is like banging a nail into wood with a piece of glass - it's possible but you're going to make a lot of mess doing so, and chances are somethings going to break at some point.

    It would make so much more sense to have a different table for each of those arrays, all with a common foreign-ID linking to some base table. That way things would be linkable, so much more consistent and you can make much more sense of the data. Search queries would be faster. By throwing it all into one box you also lose all ability to calculate any statistics, and maintenance is likely to be hell.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Jake - claro's wording wasn't the best but he is basically trying to post a table of account records. So if you slice straight across the arrays then you end up with one record. Of course he will have to make some adjustments once he starts using check boxes and he will have to change a few things in order to modify the records but he is on the right track.

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Either version of the question is plausible, as both get asked. So I'll let the OP clarify their question.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    thank you for your responses, you're right sir ahundiak, my variables are in a different fields. I have two pages, A.php and B.php, the first page is compose of the book details [authors(array), subject, etc.], and when the user click the next button he will directed to my B.php which compose of the acquisition details[acc.no (array), price, date recieved, supplier, etc. ]

    Now I'm having trouble in saving it, since both pages have arrays, and it bothers me also if I'll save A.php right after the user hit the next button or I'll just save it together with B.php at the same time (in different tables).

    I hope I made myself clear. Thank you.

  7. #7
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Sir Hundiak. I like that. Can you recommend me to the Queen?

    But focus one problem at a time. Did you understand the gist of my post #2 on how to pull individual records from multiple arrays?

    Jake - I sort of cheated. Claro has another thread talking about the html for a table based form.

  8. #8
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Ok you got me. I found a solution on my past problem and now I'm continuing. (I just don't know how to declare that post is already been solved and shame to tell that the problem is in my js which I told that the is not problem my js part, there's an issue of naming my textbox)

    And I am here now.

    Did you understand the gist of my post #2 on how to pull individual records from multiple arrays?
    I'm not sure if I got it.

  9. #9
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    for($i 0$i count($accnum); $i++)
    {
      
    $accnums $accnum[$i];
      
    $cop $copy[$i];
      echo 
    "$accnums $cop\n";

    Run the above and you should get the idea.

    You do know how to insert a record right?

  10. #10
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    The value inserted is the name 'Array', how is that?
    I put the insert query inside the loop.

  11. #11
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Post your current code. Looks like you're trying to treat an array as a string.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  12. #12
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    I used the other part of my array insertion, names are changed.

    for($i = 0; $i < count($Fname); $i++)
    {
    $Acod = $Acode[$i];
    $Fnam = $Fname[$i];
    $Lnam = $Lname[$i];
    $Mnam = $Mname[$i];
    echo "$Acod $Fnam $Lnam $Mnam <br/>";

    mysql_query("INSERT INTO tbl_author (author_Id, author_Fname, author_Lname, author_Mname)
    VALUES ('$Acode', '$Fname', '$Lname', '$Mname' )") or die (mysql_error());
    }

  13. #13
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Your naming convention can use some work. But basically
    PHP Code:
    mysql_query("INSERT INTO tbl_author (author_Id, author_Fname, author_Lname, author_Mname)
    VALUES ('
    $Acode', '$Fname', '$Lname', '$Mname' )") or die (mysql_error()); 
    shoulde be:
    PHP Code:
    mysql_query("INSERT INTO tbl_author (author_Id, author_Fname, author_Lname, author_Mname)
    VALUES ('
    $Acod', '$Fnam', '$Lnam', '$Mnam' )") or die (mysql_error()); 
    Consider suffixing your arrays with 's' and then name single items the same without the s.
    i.e.
    PHP Code:
    $aCode $aCodes[$i]; 
    Also, when you post code to site point, surround it with [PHP ][/PHP ] to get the pretty colors.

  14. #14
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    thank you and sorry for that. thank you sir.

  15. #15
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    But, I still have to save the book detail, I separated the tbl_bdetail and tbl_author, that in my tbl_bdetail I want to save all the authors and in my tbl_author I want to save the books detail written by a common author.

    PHP Code:
    for($i 0$i count($Fname); $i++)
    {
      
    $Acod $Acode[$i];
      
    $Fnam $Fname[$i];
      
    $Lnam $Lname[$i];
      
    $Mnam $Mname[$i];
      echo 
    "$Acod $Fnam $Lnam $Mnam <br/>";
      
      
    mysql_query("INSERT INTO tbl_author (author_Id, author_Fname, author_Lname, author_Mname)
                    VALUES ('
    $Acod', '$Fnam', '$Lnam', '$Mnam' )") or die (mysql_error());

            for (
    $x 0$x count($Acode); $x++)
            {
                            
              
    $Title $Titles[$x]    ;            
              
    $Sub $Subs[$x]    ;            
              
    $Pub $Pubs[$x]    ;            
              
    $Pubname $Pubnames[$x]    ;            
              
    $Bnum $Bnums[$x]    ;
              echo 
    "$Title $Sub $Pub $Pubname $Bnum $Acod $Fnam $Lnam $Mnam <br/>";

       
    mysql_query("INSERT INTO tbl_bdetail (bdetail_Title, bdetail_Subject, bdetail_Pubname, bdetail_Pubplace, bdetail_Classnum, author_Id)
        VALUES ('
    $Title', '$Sub', '$Pub', '$Pub', '$Pubname', '$Bnum', '$Acod')") or die (mysql_error());
            }                
                    

    Not tested.

  16. #16
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the responses, code below works for me.
    PHP Code:
    foreach ($Acode as $code)
    {
    echo 
    $code$Titles$Subs ,$Pubs$Pubnames ,$Bnums.'<br/>';
    mysql_query("INSERT INTO tbl_bdetail (bdetail_Title, bdetail_Subject, bdetail_Pubname, bdetail_Pubplace, bdetail_Classnum, author_Id)
                        VALUES ('
    $Titles', '$Subs', '$Pubs', '$Pubnames', '$Bnums', '$code')") or die (mysql_error()); 
    }

    for(
    $i 0$i count($Acode); $i++)
    {
     
    $Acod $Acode[$i];
      
    $Fnam $Fname[$i];
      
    $Lnam $Lname[$i];
      
    $Mnam $Mname[$i];
      
      
    $Title $Titles[$i];            
      
    $Sub $Subs[$i];            
      
    $Pub $Pubs[$i];            
      
    $Pubname $Pubnames[$i];            
      
    $Bnum $Bnums[$i];
     
      echo 
    "$Acod $Fnam $Lnam $Mnam <br/>";
      
      
    mysql_query("INSERT INTO tbl_author (author_Id, author_Fname, author_Lname, author_Mname)
                VALUES ('
    $Acod', '$Fnam', '$Lnam', '$Mnam' )") or die (mysql_error());

                    



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
  •