473,399 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

CSV File Data into Database

190 100+
i have a csv file in folloeing format

book_nam , author , publisher
abcd , zyz , hij


i want this csv file to put in data base in following format

Id_no , book_id , field_nm, value
xx , 1 , book_nam,abcd
xx , 1 , author,zyz
xx , 1 , publisher,hij

actually from csv file book_name will be picked up first , will be matched in another table from where book_id will be picked up and then i have to insert this code into next tabe in above format .


code i have written is this :


Expand|Select|Wrap|Line Numbers
  1. <?php
  2.    $f_name = array();
  3.  
  4.    $conn = mysql_connect("localhost","zzzzz","vvvvv");
  5.    $db = mysql_select_db("mmmmm");
  6.    $fh = fopen("Book1.csv", "r");
  7.    $count1 = 0 ;
  8.  
  9.     while($line = fgetcsv($fh, 1000, ","))
  10.     { 
  11.     $count1 = $count1+1;
  12.     count($line);
  13.     if($count1 == 1)
  14.     {
  15.  
  16.      for($i=0;$i<=count($line);$i++)
  17.         {
  18.  
  19.            $f_name[i] = $line[$i];
  20.  
  21.  
  22.  
  23.         }
  24.  
  25.      }
  26.  
  27.  
  28.         $pname = $line[0];
  29.         echo $qry = "select productid from isc_products where prodname = '".$pname."'";
  30.         $result = mysql_query($qry);
  31.         while($row = mysql_fetch_array($result))
  32.         {
  33.              $pid = $row["productid"];
  34.  
  35.         }
  36.  
  37.           for($i=1;$i<=(count($line)-1);$i++)
  38.           {
  39.            global $f_name; 
  40.            $k= $i-1;
  41.            $fld_nm = $f_name[$k];
  42.            $val = $line[$i]; 
  43.            echo $qry1 = "insert into isc_product_customfields values(,'$pid','$fld_nm','$val')"; 
  44.  
  45.            }
  46.            echo "\n";
  47.  
  48.  
  49.  
  50.  
  51.   }      
  52.    mysql_close();
  53. fclose($fh);
  54. ?>
  55.  
  56.  
till now i am just printing the query .

i am stucing at a point , i am getting query build but without field_nm in it .

pls help
Nov 25 '08 #1
8 1705
nathj
938 Expert 512MB
Shalini,

If I read the question correctly you want to know how to run the insert command so that it updates the database.

Expand|Select|Wrap|Line Numbers
  1. $lnNewID = mysql_query($qry1, $conn) or die("Error: ". mysql_error());
  2.  
This should sort you out and give you the new ID should you need for anything else.

If I've mis-understood or there's anything else I can help with just pot back.

Cheers
nathj
Nov 25 '08 #2
Shalini Bhalla
190 100+
its giving me ID but will insert data into another table for which i need to generate a query , which i am not getting :


Expand|Select|Wrap|Line Numbers
  1. for($i=1;$i<=(count($line)-1);$i++)
  2.           {
  3.            global $f_name; 
  4.            print_r($f_name);
  5.            $k= $i-1;
  6.            $fld_nm = $f_name[$k];
  7.            $val = $line[$i]; 
  8.            echo $qry1 = "insert into isc_product_customfields values(,'$pid','$fld_nm','$val')"; 
  9.  
  10.            }

has to insert data in converted format :


csv file has data like :

Prodname , author , publisher
xyz , hij , opq


but i need to put in a table whic is having feilds like


Record_no(auto gen),Prod_id,field_name,value
101 , I , Prodname , xyz
102 , I , author ,hij
103 , I , publisher ,opq



i think i am clear now .......












After taking ID ,
Nov 25 '08 #3
nathj
938 Expert 512MB
I'm sorry but I'm not sure I understand the problem. If you could answer a couple of questions for me I may be able to get a clearer picture of what is going on.

1. How many CSV files are?
2. If there are multiple CVS files are they connected in any way?

I think you may want to use loops to process the information, but as I don't fully understand (my bad) then I can't help further. I'll have another think when you have answered my questions.

Cheers
nathj
Nov 25 '08 #4
Shalini Bhalla
190 100+
i havi one csv file as follows :
Book1.csv

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. prod_name , author , publisher 
  4.    abcd       ,  hij      , opq
  5.  
  6.  
  7.  
  8.  

and 2 data bases :

prod_master and another is storing custom fields for pro_master called : pro_custom_field .



author , publisher are custom field name and
abcd , hij are values for custom fields.


which my client will give me in Book1.csv format.

and i have to insert this data in pro_custom_field like this
Expand|Select|Wrap|Line Numbers
  1.  
  2. rec_ID , Pro_ID , custom_fld_nam ,     value                 i.e
  3. 101     ,  1        , author      ,     abcd 
  4.  
  5.  

just try to understand the realtion ship of csv file and pro_master then you will be cleared.

if not still cleared , leave it i have changed the csv format and have solved it .




Thanks.
Nov 25 '08 #5
Shalini Bhalla
190 100+
pls tell me how to catch insert statement errors
http://bytes.com/answers/php/856393-...ip-error-msges
Nov 25 '08 #6
nathj
938 Expert 512MB
@Shalini Bhalla
Expand|Select|Wrap|Line Numbers
  1. $lnNewID = mysql_query($qry1, $conn) or die("Error: ". mysql_error());
  2.  
The 'or die' part of this statement would do what you are after.

As for the CSV format, I do now understand what you are after, so I'm sorry for being a bit slow to catch on. I'm glad you have solved the problem.

Cheers
nathj
Nov 25 '08 #7
Shalini Bhalla
190 100+
but if still you can tell me that why my global array in that CSV problem is not working , i will be glad ....


my global array is not working in that code which i posted earlier second time.....



thankyou
Nov 26 '08 #8
Shalini Bhalla
190 100+
but "die " will stop the execution also ....... i don't want to stop in between ....it should continue till eof
Nov 27 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Fran Tirimo | last post by:
I am developing a small website using ASP scripts to format data retrieved from an Access database. It will run on a Windows 2003 server supporting FrontPage extensions 2002 hosted by the company...
2
by: Fran Tirimo | last post by:
I am developing a small website using ASP scripts to format data retrieved from an Access database. It will run on a Windows 2003 server supporting FrontPage extensions 2002 hosted by the company...
6
by: varlagas | last post by:
We disabled the antivirus software but the problem persists. Any clues? Many thanks in advance! Panagiotis Varlagas ======================================================================= ...
0
by: Andrew Dowding | last post by:
Hi Everybody, I have been looking at problems with my Windows Forms C# application and it's little Jet 4 (Access) database for the last few days. The Windows Forms app implements a facade and...
60
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't...
5
by: Seok Bee | last post by:
Dear Experts, I currently trying to use the FileUpload control from asp.net 2.0 to upload files. The uploading of the file I would like to store it in the Access Database. Unfortunately, I've no...
2
by: Miro | last post by:
I will ask the question first then fumble thru trying to explain myself so i dont waste too much of your time. Question / Statement - Every mdb table needs a PrimaryKey ( or maybe an index - i...
5
by: B. Williams | last post by:
I need some assistance with random access file processing. I have a function that I would like to change from sequential file processing to random access. Thanks in advance. void...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.