1

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!!!

5
Contributors
10
Replies
28
Views
5 Years
Discussion Span
Last Post by Swayam.tejwani
Featured Replies
  • 1
    diafol 3,720   5 Years Ago

    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. Read More

0

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

Edited by diafol

0

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.

0

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

What do you mean due to the limited space?

0

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

Edited by Dani: Formatting

1

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.

Edited by diafol

0

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.

0

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!

0

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

-1

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.