Forum Moderators: coopster

Message Too Old, No Replies

upload csv file into mysql

script error on this

         

catherine78

3:07 am on Nov 10, 2005 (gmt 0)

10+ Year Member



hi, i am writing a script for uploading csv file and insert data into mysql table.

this is my script

<?php

function display_menu() {?>

<form enctype ="multipart/form-data">
CSV Import: <input type=file name=csvfile>
<input type=submit name=action value= " CSV Import ">
</form>

<? }

function action_csv_import(){
global $csvfile;

$csvfile = stripslashes($csvfile);

if ($csvfile!= ""){
$nr = csv_import($csvfile);
if ($nr > 0) {
echo "import successful - $nr entries imported.<p>";
}
else if ($nr == 0) {
echo "Import did not find any entries.<p>";
}
else {
echo "import failed.<p>";
}

} else {
echo "Please select a file and try again.<p>";
}
}

function csv_import($path){
global $dbpath, $dbtype, $vars;

$db = dba_open($dbpath, "c", $dbtype, "0644");

if (!$db){
echo "db open failed.<p>";
return -1;
}

$fp = fopen($path, "r");

if (!$fp) {
dba_close($db);
echo "Cannot open CSV file ($path);
return -1;
}

$id = get_next_id($db);
$nr_entries = 0;

while(!feof($fp)){
$data = fgetcsv($fp, 4096);

if (is_array($data) && count($data) > 0){
$new = array();
for ($i = 0; $i < count($data); $i++){
$new['$vars[$i]'] = $data[$i];
}
$new["id"] = $id;
dba_replace($id, serialize($new), $db);
$id++;
$nr_entries++;
}
}

fclose($fp);
dba_sync($db);
dba_close($db);
return $nr_entries;
}

$dbpath = "upproduct.db";

$dbtype = "db2";

$data = array();
$vars = array("uppID", "userid", "prodNames", "prodPrices");

for ($i = 0; $i < count($vars); $i++){
$data[$vars[$i]] = ${$vars[$i]};
}

display_menu();

$action = strtolower(trim($action));

switch($action){
case "search":
action_search($word);
break;
case "csv import":
action_csv_import();
break;
}

?>

but it returned error on this part $new['$vars[$i]'] = $data[$i];. the error message is Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in line 61.

anyone please help me out on this issue?

thank you very much.

jatar_k

3:43 am on Nov 10, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld catherine78,

['$vars[$i]']

try removing the single quotes from in there

catherine78

3:58 am on Nov 10, 2005 (gmt 0)

10+ Year Member



hi, after i removed the ' quote, it returned this error Parse error: parse error, unexpected '[', expecting ']' in line 61

thanks.

jatar_k

5:26 am on Nov 10, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



must be trickle down, something isn't closed somewhere before that line then

echo "Cannot open CSV file ($path);

that line is missing a closing double quote

catherine78

6:13 am on Nov 10, 2005 (gmt 0)

10+ Year Member



thanks ya.