Forum Moderators: coopster
i had hard understanding this code but i don't get the logic
can anybody explain it ^^
csvfiles uploaded:
Array ( [age] => country [23] => manila [543] => cagayan [54] => davao [id] => ) 1
$table = test_upload;
$data = d1,d2,d3;function DB_insert($table, $data)
{
//echo $table; echo print_r($data);
$query = 'INSERT into '.$table. ' (';
$first_time = TRUE;
foreach ($data as $key => $value ) {
if (FALSE == $first_time) {
$query .= ', ';
} else {
$first_time = FALSE;
}
$query .= ''.$key.'';
}
$query .= ') VALUES (';
$first_time = TRUE;
foreach ($data as $key => $value ) {
if (FALSE == $first_time) {
$query .= ', ';
} else {
$first_time = FALSE;
}
$query .= '"'.$value.'"';
}
$query .= ');';
return mysql_query($query);}
table test_upload
TID, age, country;
[age] => country [table Name]
foreach ($data as $key => $value ) {
if (FALSE == $first_time) {
$query .= ', ';
} else {
$first_time = FALSE;
}
$query .= ''.$key.'';
}
$query .= ')
[23] => manila [543] => cagayan [54] => davao [fields]
';
$first_time = TRUE;
foreach ($data as $key => $value ) {
if (FALSE == $first_time) {
$query .= ', ';
} else {
$first_time = FALSE;
}
$query .= '"'.$value.'"';
}
$query .= ');
but doesn't insert to the database.. what wrong with this code ?
Also, it's always a good idea while troubleshooting to echo your query to the screen, so that you can eyeball it and see if it looks right:
echo $query;
mysql_query($query) or die('Bad query: ' . mysql_error().$query);
include("connect.inc.php");csv2DB('csvfile');
function csv2DB($filename) {
if (FALSE == empty($_FILES[$filename]['tmp_name'])) {
$row = 1;
$handle = fopen($_FILES[$filename]['tmp_name'], "r");
while (($data = fgetcsv($handle, 1000, ","))!== FALSE) {
$num = count($data);
//echo "<p> ".$num." fields in line ".$row." <br /></p>\n";
$row++;
for ($c = 0; $c < $num; $c = $c + 2) {$data[$c] = str_replace('.', '', $data[$c]);
$data[$c] = str_replace(' ', '_', $data[$c]);$disease_data[$data[$c]] = $data[$c + 1];
//echo $data[$c] . "<br />\n";
}
}
fclose($handle);
//$disease_data['id'] = '';
//$disease_data['page_id'] = $pageId;
DB_insert('test_upload', $disease_data);
}}
its seem the result is
INSERT into test_upload (age, 2, 34, 43) VALUES ("country", "fdgsd", "fdsf", "fdsfds");
how can i get the result:
$age=array(2,34,43);
$country=array('fdgsd','fdsf','fdsfds')
insert into test_upload(age,country)value('.$age.','.$country.');
anybody?