...

View Full Version : Inserting array data into MySQL



jsquadrilla
11-17-2011, 01:49 PM
Hello,

I currently have an array, which each element contains a string similiar to


1,Chicago,CEN,19,12,4,3,27,10,67,53,+14,7-1-2,5-3-1,2-2,7-2-1,Won 4
2,Pittsburgh,ATL,18,11,4,3,25,8,57,43,+14,6-1-1,5-3-2,3-2,7-2-1,Won 1

// 28 more lines like this, each in an array element

I also have an array containing the MySQL table headers that I want to input those values to:


$tableHeaders = array("Rank", "Team", "Division", "Games Played", "Wins", "Losses", "Overtime Losses", "Points", "Regulation Plus Overtime Wins", "Goals For", "Goals Against", "Goal Differential", "Home Record", "Away Record", "Shootout", "Last 10", "Streak");

I tried doing the INSERT statement as I would normally do, but I don't think it's working for these arrays.

I think it's a foreach statement that I have to do, but I can't find a good example. Can anyone point me in the right direction?

abduraooft
11-17-2011, 01:56 PM
I also have an array containing the MySQL table headers that I want to input those values to:


"Games Played", "Wins", "Losses", "Overtime Losses", "Points", "Regulation Plus Overtime Wins", "Goals For", "Goals Against"
Step 1: You can't have spaces in column names. So, you need to change them.
Step 2: You may use the function implode() (http://php.net/implode) to get a comma separated string (to make the query) out of an array.

jsquadrilla
11-17-2011, 02:05 PM
Step 1: You can't have spaces in column names. So, you need to change them.
Step 2: You may use the function implode() (http://php.net/implode) to get a comma separated string (to make the query) out of an array.


$tableHeaders = array("Rank", "Team", "Division", "GamesPlayed", "Wins", "Losses", "OvertimeLosses", "Points", "RegulationPlusOvertimeWins", "GoalsFor", "GoalsAgainst", "GoalDifferential", "HomeRecord", "AwayRecord", "Shootout", "Last10", "Streak");

Fixed that up.

For your Step 2, I may have said it wrong. Ex:

I created my array:


$rowArray = array();

$rowArray[0] = "1,Chicago,CEN,19,12,4,3,27,10,67,53,+14,7-1-2,5-3-1,2-2,7-2-1,Won 4";

$rowArray[1] = "2,Pittsburgh,ATL,18,11,4,3,25,8,57,43,+14,6-1-1,5-3-2,3-2,7-2-1,Won 1";



I looped it, but basically that's what my array contains. So they are already comma-seperated. Or is that the wrong way to go about this?

jsquadrilla
11-17-2011, 03:45 PM
Perfect! Got everything to work using implode and changing the way I saved the data. Thanks for the suggestion.

I have another question (of course!).

My code for inserting the data is

$result = mysql_query("INSERT INTO standings_20112012 VALUES (".$comma_separated.")");

Which works fine for inserting. But now that all the data is inserted, I need to either update/overwrite etc. as new data is available.

I'm guessing this is an easy question, but how can I go about doing that? Would it just be the UPDATE statement?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum