insert xls data to sql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
madu
Forum Commoner
Posts: 32
Joined: Sat Dec 25, 2010 3:19 am
Location: india

insert xls data to sql

Post 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'
Last edited by Benjamin on Thu Dec 30, 2010 6:48 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: insert xls data to sql

Post 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)."')";
Post Reply