Saving multiple array in mysql

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


 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/>';
   }

Assuming these are indexed arrays (posted back from a form?) then something like:


for($i = 0; $i < count($accnum); $i++)
{
  $accnums = $accnum[$i];
  $cop = $copy[$i];
  etc.
  then do a regular insert
}

If you really, really want to save it in a single row, do so by:

$accnumfield = "'" . implode('|', $accnum) . "'";

Then to retrieve them and split them up back into an array:

$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 - 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.

Either version of the question is plausible, as both get asked. So I’ll let the OP clarify their question.

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.

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.

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.


for($i = 0; $i < count($accnum); $i++)
{
  $accnums = $accnum[$i];
  $cop = $copy[$i];
  echo "$accnums $cop\
";
} 

Run the above and you should get the idea.

You do know how to insert a record right?

The value inserted is the name ‘Array’, how is that?
I put the insert query inside the loop.

Post your current code. Looks like you’re trying to treat an array as a string.

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());
}

Your naming convention can use some work. But basically

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:


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.


$aCode = $aCodes[$i];

Also, when you post code to site point, surround it with [PHP ][/PHP ] to get the pretty colors.

thank you and sorry for that. thank you sir.

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.


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.

Thank you for the responses, code below works for me.


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());

				
}