0

hi,
i want to make a program where the csv file directly insert into the mysql database table.

i found the code:

<?php
include 'mysql-connect.php';
$rec=0;
$handle = fopen ('datafile1.csv', 'r');
		while (($data = fgetcsv($handle, 1000, ',', '"')) !== FALSE)
		{
			$rec++;
			if($rec==1)
			{
			continue;
			}
			else
			{
			$query = "INSERT INTO Book_details VALUES ('". implode("','", $data)."')";
 			$query = @mysql_query($query);
			}
		}


?>

it working fine .

but i have to face problem when i specify the columns in the mysql table like

while (($data = fgetcsv($handle, 1000, ',', '"')) !== FALSE)
		{
			$query = "INSERT INTO Book_details(idt_no,book_name,author_name,publisher_name,condition,condition_note,price,cur,quantity) VALUES ('". implode("','", $data)."')";
 			$query = @mysql_query($query);
		}

no value is insert in the data base???

mention that database have 12 columns and csv have 9.
i have to insert all 9 to selected 9 columns in the database.
plz help me....

3
Contributors
6
Replies
9
Views
8 Years
Discussion Span
Last Post by sujitri
0

Clear @ before mysql_query and see if it throws any error. Also, you can have

$result = mysql_query($query) or die(mysql_error());

to print the error when the query fails.

P.S. @ suppresses the errors, so you shouldn't use it in testing scenarios. That will make your life hard to know where exactly the error is.

Edited by nav33n: n/a

0

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'condition,condition_note,price,cur,quantity) VALUES ('989003000000','sandipans a' at line 1

this error will occur i cant understand the problem

0

in my csv file there is 9 columns of 5 different tables. whet i want to insert perticuler columns inti perticuler table then it gives error.
any solution???

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.