Hi all!

I am trying to insert multiple arrays values into a MySQL table. All I can manage is to echo in the browser.

I have the following arrays:

$array1 [0] = "Arsenal";
$array1 [1] = "Barcelona";
$array1 [2] = "Bayern";

$array2 [0] = "England Premier League";
$array2 [1] = "Spain Premiera Division";
$array2 [2] = "Germany Bundesliga";

What code should I write to send them to my MySQL database?

Table: teams;
Column 1: team;
Column 2: league.

For 2 days I am trying to figure it out and I give up. PLEASE HELP!!!

Member Avatar for diafol

Something like?

$cteam = count($array1);
$cleague = count($array2);

if($cteam == $cleague){
  for($x = 0; $x < $cteam; $x++){
    $str[] = "('{$array1[$x]}','{$array2[$x]}')";
  }
  $s = implode(',',$str);
  $sql = "INSERT INTO table (field1,field2) VALUES $s;
}

Hi,

I can't write a mysql tutorial here due to the limited space, and besides Internet has already got bunches of them that it is almost redundant to post it here. Please follow this tutorial Here , and then come back to this thread if you bump into troubles. When you do come back, make sure to include your codes.

I can't write a mysql tutorial here due to the limited space

What do you mean due to the limited space?

Thank you for reply and the info. I have the folloing PHP code in my page now:

<?php

$array1 [0] = "Manchester United";
$array1 [1] = "Manchester City";
$array1 [2] = "Arsenal";

$array2 [0] = "England Premier League";
$array2 [1] = "England Premier League";
$array2 [2] = "England Premier League";

include ("blocks/db.php");

$cteam = count($array1);
$cleague = count($array2);
if($cteam == $cleague)
  {  
  for($x = 0; $x < $cteam; $x++)
     {    
     $str[] = "('{$array1[$x]}','{$array2[$x]}')";  
     }  
  $s = implode(',',$str);  
  $sql = "INSERT INTO teams (team,league) VALUES $s";
  echo $sql;
  }

?>

In the browser appear:

INSERT INTO teams (team,league) VALUES ('Manchester United','England Premier League'),('Manchester City','England Premier League'),('Arsenal','England Premier League')

I guess something is missing, as in the database nothing appears (maybe mysql_query function...).

Member Avatar for diafol

I created a new table and pasted this to the sql window and run it:

INSERT INTO league (team,league) VALUES ('Manchester United','England Premier League'),('Manchester City','England Premier League'),('Arsenal','England Premier League')

Works fine for me.

LIMITED SPACE, because I don't do cut and paste most of the time, and I type everything on this little textarea. Please take a look at your response textarea console.

Ardav, thank you so much!!! Your code helped me a lot!!!

I need to say that I am a beginner at HTML, CSS, PHP, MySQL and at the moment I study them by myself (it is very difficult so...). I am creating a webpage while learning, and at the moment I am working on Admin Page, the one that inserts new teams into the DB filling a form. I need to use an array to complete this task, because the number of new teams to be inserted differs from one time to another. Your 2 lines were non-familiar to me and they are really useful:

$str[] = "('{$array1[$x]}','{$array2[$x]}')";
$s = implode(',',$str);

One change I've made to make it work:

  1. $cteam = count($array1);
  2. $cleague = count($array2);
  3. if($cteam == $cleague)
  4. {
  5. for($x = 0; $x < $cteam; $x++)
  6. {
  7. $str[] = "('{$array1[$x]}','{$array2[$x]}')";
  8. }
  9. $s = implode(',',$str);
  10. //$sql = "INSERT INTO teams (team,league) VALUES $s";
  11. mysql_query ("INSERT INTO teams (team,league) VALUES $s",$db);
  12. }

Daniweb is a really great site, people can find a lot of useful information here. Thank you!

I appologize, I am a new user and I do not know how to format the code otherwise. Please advise.

Member Avatar for diafol

OK. Mark the thread as solved.

i have a form which is having hobby checkbox field named hobby[] when i m going to insert it in database using INSERT query with $_POST['hobby'] ..in db it shows string 'Array' instead of checkbox name..Does anyone plz help me to resolve it..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.