0

So I have a multidimensional array $fillarray and its over 1000 lines long so $fillarray[0] - $fillarray[1000], each element has 22 sections. I'm not concerned with [0][0] on any branch map.

There are several posts regarding pulling from a MySQL table and populating an array, I'm trying to go the other way.

I have tried this several ways. Any help will be greatly appreciated.

for($i = 1; $i <= $cnt; ++$i) {
    if($i != 1)
        $values .= ',(NULL,';
    else
        $values = '(NULL,';
    for($t = 1; $t <= 21; ++$t){
        if($t != 21)
            $value .= "'".$fillarray[$i][$t]."',";
        else
            $values .= "'".$fillarray[$i][$t]."')";
        //echo $fillarray[$i][$t]."<br>";
    if($i != $start)
        $values .= ",";
    }
    mysql_query("insert into `data` VALUES(NULL, '$fillarray[$i][1]' , '$fillarray[$i][2]', '$fillarray[$i][3]', '$fillarray[$i][4]', '$fillarray[$i][5]','$fillarray[$i][6]','$fillarray[$i][7]','$fillarray[$i][8]','$fillarray[$i][9]','$fillarray[$i][10]','$fillarray[$i][11]','$fillarray[$i][12]','$fillarray[$i][13]','$fillarray[$i][14]','$fillarray[$i][15]','$fillarray[$i][16]','$fillarray[$i][17]','$fillarray[$i][18]','$fillarray[$i][19]','$fillarray[$i][20]','$fillarray[$i][21]')") or die(mysql_error());

}
kupendra
  • 1,002
  • 1
  • 15
  • 37
MoJo
  • 1
  • And what is your problem? We should guess? Have you echoed result query? HAve you checked errors? Or.. what have you done? – u_mulder Nov 11 '14 at 19:24
  • problem is that it doesn't insert the array data into the table. There are not errors and when I echo the values it just has (NULL,,,,,,,,,,,,,,,,,,,,,), and repeats. Sorry for not including that before. – MoJo Nov 12 '14 at 00:09

2 Answers2

0

try to use foreach loop for multidimensional arra ...

foreach($value as $values){

 echo $values;
}

or

foreach($values as $key=>$values){

echo $key=>$values;


} 
syedsafir
  • 72
  • 1
  • 9
0

Wasn't able to test it, but would something like this work?

foreach ($fillarray as $row) {
    $query = "INSERT INTO `data` VALUES (" . implode(',', array_map('mysql_real_escape_string', array_unshift($row, NULL))) . ")";
}

Also, inb4 "Don't use mysql_*, use PDO/mysqli"!

Community
  • 1
  • 1
Curtis Mattoon
  • 4,642
  • 2
  • 27
  • 34
  • As for changing the connection string from MySQL to MySQLi or PDO, that's isn't an issue, how would you write the query to input the data into each row. – MoJo Nov 11 '14 at 23:41
  • So I added in the MySQL_query($query); and it put all NULLS in the table, then I added in an echo $query and it was pumping in INSERT INTO `data` VALUES (). I'm fine with using PDO or Mysqli, is the syntax the same? Thank you for your help, I think its closer to what I need. – MoJo Nov 11 '14 at 23:45
  • I'm not sure how your data is structured, but it looks like your arrays may not be set up to work with that function exactly. You'll probably have to tweak either the array or the function. If you can `var_dump` a few elements of the array, I can see if it needs adjusted. As far as PDO or mysqli, the syntax is the same for queries, but they need prepared differently. Check out the tutorial: http://code.tutsplus.com/tutorials/php-database-access-are-you-doing-it-correctly--net-25338 – Curtis Mattoon Nov 12 '14 at 13:21