I have an array from a csv with a similar structure to this:
$array = array(
array(\'name\', \'age\', \'gender\'),
array(\'Ian\', 24, \'male\'),
a
Assuming that the value in the array are TRUSTED and SECURE.
$count = count($array);
$keys = $array[0];
for($i = 1; $i < $count; $i++)
{
$query = "INSERT INTO tablename (" . implode(",", $keys) . ") VALUES ('" . implode ("','", $array[$i]) . "');";
$query = str_replace(',)', ')', $query);
mysql_query($query);
}
$fields = implode(',', array_shift($array)); // take the field names off the start of the array
$data = array()
foreach($array as $row) {
$name = mysql_real_escape_string($row[0]);
$age = (int) $row[1];
$gender = mysql_real_escape_string($row[2]);
$data[] = "('$name', $age, '$gender')";
}
$values = implode(',', $data);
$sql = "INSERT INTO yourtable ($fields) VALUES $values";
$result = mysql_query($sql) or die(mysql_error());
That should produce a query string like:
INSERT INTO yourtable (name, age, gender) VALUES ('Ian', 24, 'male'), ('Janice', 21, 'female'), etc....