a tricky little mysql_query
Posted: Fri Jan 08, 2010 6:45 pm
Hey Guys and Gals,
My Problem: I am building a system where users upload a csv file, on post that csv gets uploaded the server and then i need its contents to be put into a mysql_query function.
Currently i have everything working except for the mysql_query. I will post code and then explain it below.
so if you look in the for loop, i have that $var variable storing each loop of data and then i put var into the query. if i print var normally, it prints out all the fields properly. But when i put it in the query it gives me a syntax error or column count error. This leads me to belive that i'm almost there, i just need to format it differently. Any Suggestions?
My Problem: I am building a system where users upload a csv file, on post that csv gets uploaded the server and then i need its contents to be put into a mysql_query function.
Currently i have everything working except for the mysql_query. I will post code and then explain it below.
Code: Select all
if(isset($_POST['csv'])):
extract($_POST);
$random = rand(100000,999999);
$uploaddir = '../lib/uploads/file/products/';
$uploadfile = $uploaddir . $random . '_' . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
if (($handle = fopen("$uploadfile", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
for ($c=15; $c < $num; $c++) {
$var .= "'$data[$c]', ";
}//for
mysql_query("INSERT INTO $table VALUES (
'',
'$curUser',
$var
'$image'
)") or die(mysql_error());
}//while
fclose($handle);
}//if
} else {
print '<script type="text/javascript">alert("Error uploading file. Please try again.");</script>';
}
endif;