Insert Array Into Database

I’m trying to insert all the values of the array into the database. Every time I try it I don’t get any errors but the information is never entered into MySQL.

<?


//connect to mySQL
$db = mysql_connect("localhost");

//connect to the database test
$doh = mysql_select_db('madden_league');

//array of teams
$team[0] = "Eagles";
$team[1] = "Giants";
$team[2] = "Rams";
$team[3] = "Titans";
$team[4] = "49ers";
$team[5] = "Browns";
$team[6] = "Packers";
$team[7] = "Bears";



//insert array values 0-7 into database		

for($test = 0; $test <= 7; $test++)
{
	$sql = mysql_query("INSERT INTO 'schedule'
		(home)
		VALUES
		($team[$test])");

$result = mysql_query($sql);
}

?>

for($test = 0; $test <= count($team); $test++)
{
    $sql = mysql_query("INSERT INTO 'schedule' (home) VALUES  ('" . $team[$test]) . "')");

$result = mysql_query($sql);
}

It’s much more effective to insert them all at once, with single sql statement.


// connect stuff skipped

//array of teams
$team[0] = "Eagles";
$team[1] = "Giants";
$team[2] = "Rams";
$team[3] = "Titans";
$team[4] = "49ers";
$team[5] = "Browns";
$team[6] = "Packers";
$team[7] = "Bears";

// prepare mysql statement

$data = array();

foreach($teams as $team)
    $data[] = "(" . addslashes($team) . ")";

$data = implode("," , $data);

$sql = "INSERT INTO schedule (home) VALUES $data";

// here we go

mysql_query($sql);

I’ve also corrected some minor issues (quoted table name, ‘for’ loop etc ).

It comes up with “Warning: Invalid argument supplied for foreach()”

array must be called “teams” not “team”.

rofl, what up Bwest? :stuck_out_tongue: (assuming you’re the same one from SCMB)

as for your question, you’re putting values into an array called $team, but you’re using the array $teams within your foreach function.


foreach($team as $one) { // either that or change all the $team[#] to $teams[#]