I'm working on a project that requires (secure) insertion of data arrays into a MySQL database. My code currently iterates through an XML document using the foreach
construct, parsing all the data I've chosen to extract from it.
Here's the code...
foreach ($xml->town as $towns) {
# -> player information
$player_id = mysqli_real_escape_string($dbconnect,$towns->player->playername['id']);
$player_name = mysqli_real_escape_string($dbconnect,$towns->player->playername);
# -> town data information
$town_name = mysqli_real_escape_string($dbconnect,$towns->towndata->townname);
$town_id = mysqli_real_escape_string($dbconnect,$towns->towndata->townname['id']);
$founded_on = mysqli_real_escape_string($dbconnect,$towns->towndata->foundeddatetime);
$population = mysqli_real_escape_string($dbconnect,$towns->towndata->population);
$capital = mysqli_real_escape_string($dbconnect,$towns->towndata->iscapitalcity);
# -> alliance info
$alliance_cap = mysqli_real_escape_string($dbconnect,$towns->towndata->isalliancecapitalcity);
# -> location information
$map_x = mysqli_real_escape_string($dbconnect,$towns->location->mapx);
$map_y = mysqli_real_escape_string($dbconnect,$towns->location->mapy);
# -> terrain information
$terrain_type_id = mysqli_real_escape_string($dbconnect,$towns->location->terraintype['id']);
$terrain_type = mysqli_real_escape_string($dbconnect,$towns->location->terraintype);
$terrain_overall_id = mysqli_real_escape_string($dbconnect,$towns->location->terrainoveralltype['id']);
$terrain_overall_type = mysqli_real_escape_string($dbconnect,$towns->location->terrainoveralltype);
In a question I recently asked about XML parsing, someone commented that I'm not using mysqli_real_escape_string
correctly. He mentioned that I should first extract the data, then later call mysqli_real_escape_string
in preparation of the database insert operation.
MySQL query
I've written a straightforward MySQL query which works; though for some reason its only inserting the first row of the array, rather than the entire array of data.
mysqli_query($dbconnect,
"INSERT INTO towndata (player_id, playername, town_name, town_id, founded_on, population, capital, alliance_cap, map_x, map_y, terrain_type_id, terrain_type, terrain_overall_id, terrain_overall_type)
VALUES ('$player_id', '$player_name', '$town_name', '$town_id', '$founded_on', '$population', '$capital', '$alliance_cap', '$map_x', '$map_y', '$terrain_type_id', '$terrain_type', '$terrain_overall_id', '$terrain_overall_type')"
); # end mysql statement
} # end of _foreach_ loop
Now for the question/problem portion of things...
Why is only the first row being inserted instead of the entire data array?
As mentioned above, the
INSERT INTO
statement works. However I can't seem to figure out why my code is only inserting the first row. Other portions of the application use the same code, but are not exhibiting this behavior, which leaves me entirely clueless as to the cause.What is the correct/effective way to use
mysqli_real_escape_string
to prepare data arrays (and strings) for secure database insertion?I realize this may be a question of preference; but I can't help but wonder if there's a pattern or paradigm that I'm not familiar with.
Thank you in advance for your time, help and advice.