Page 1 of 1

insert xls data to sql

Posted: Thu Dec 30, 2010 5:27 am
by madu
hi friends ,,
i am trying to insert ms excel data to my sql,,,my job is job is partially done,,but i have error in my coding,,,pls help me

Code: Select all

<?PHP
$link=mysql_connect("localhost","root","");
if(!$link)
 {
   die('Failed to connect to server: ' . mysql_error());
 }
$db=mysql_select_db("my_db");
if(!$db)
 {
   die("Unable to select database");
 }
$fcontents = file ('http://localhost/fgh.csv');
for($i=0; $i<sizeof($fcontents); $i++)
  {
$line = trim($fcontents[$i]);
	echo "$line<BR>";
	$arr = explode("','", $line,5);
	echo "$arr";
	$sql = "insert into mobi_dat values (". implode($arr).")";
	mysql_query($sql);
	echo $sql ."<br>\n";
	if(mysql_error()) 
	 {
		echo mysql_error() ."<br>\n";
	 }
}
?>
ERROR:
fname,lname,mnum,hnum,email
Arrayinsert into mobi_dat values (fname,lname,mnum,hnum,email)
kkk,hhh,9898675432,234567,'asdf@fgh.com'
Arrayinsert into mobi_dat values (kkk,hhh,9898675432,234567,'asdf@fgh.com')
Unknown column 'kkk' in 'field list'
bbb,dddd,9442678908,456577,'wer@hjk.com'
Arrayinsert into mobi_dat values (bbb,dddd,9442678908,456577,'wer@hjk.com')
Unknown column 'bbb' in 'field list'

Re: insert xls data to sql

Posted: Thu Dec 30, 2010 5:40 am
by Darhazer

Code: Select all

$sql = "insert into mobi_dat values (". implode($arr).")";
have to be:

Code: Select all

$sql = "insert into mobi_dat values ('". implode(",'", $arr)."')";