473,545 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

loop problem while the content of csv file is trying to insert into database

29 New Member
my code
Expand|Select|Wrap|Line Numbers
  1. <?
  2. //include("../mysqlconnect.php");
  3. $link = mysql_connect("localhost","root","")
  4.        or die("Could not connect: " . mysql_error());
  5.  mysql_select_db("suma");
  6.  
  7. include("include/upload.inc.php");
  8. if(isset($_POST['upload']))
  9. {
  10. $deli=$_POST['delimeter'];
  11.  
  12. $userfile=$_FILES['userfile']['name'];
  13. $userfile_tmp=$_FILES['userfile']['tmp_name'];
  14. $userfile_type=$_FILES['userfile']['type'];
  15. $userfile_size=$_FILES['userfile']['size'];
  16. $dir="upload";
  17. $filename=basename($userfile);
  18. move_uploaded_file("$userfile_tmp","$dir/$filename");
  19. $row= 1;
  20. $fp = fopen("$dir/$userfile", "r");
  21. $s="";
  22. $s1="";
  23. $s2="";
  24.  
  25. while (($data = fgetcsv($fp, 100000, "$deli")) !== FALSE) 
  26. {
  27.     $num = count($data);
  28.     $row++;
  29.     if($row==2)
  30.     {
  31.     for ($c=0; $c < $num; $c++) 
  32.     {
  33.         $s.=$data[$c]." VARCHAR (200) not null,";
  34.         $s2.=$data[$c].",";
  35.         $s1.="'$data[$c]',";
  36.     }
  37. echo      $sql="create table temp1(id int(20) not null auto_increment,".$s."extra varchar(200) not null, primary key(id))";
  38.     mysql_query($sql)or die("could not create");
  39.  
  40.      $sql1="insert into temp1 (".$s2."extra) values (".$s1."'choose')";
  41.     mysql_query($sql1)or die("could not insert");
  42.     }
  43.  
  44.     if($row > 2)
  45.     {
  46.     $s1="";
  47.     for ($c=0; $c < $num; $c++) 
  48.     {
  49.           $s1.="'$data[$c]',";
  50.     }
  51.      $sql2="insert into temp1 (".$s2."extra) values (".$s1."'choose')";
  52.     mysql_query($sql2)or die("could not insert");
  53.     }                
  54. }
  55.  
  56. fclose($fp);
  57. }
  58.  
  59. ?>
  60.  
  61. <table>
  62.  
  63. <form action="" method="post" enctype="multipart/form-data" name="form3" id="manu">
  64.  
  65.  <tr class="greyBG"> 
  66.       <td width="60%" height="33"> <div align="left">Please Enter the delimeter 
  67.         </div></td>
  68.       <td width="40%"><input type="text" name="delimeter"> </td>
  69.     </tr>
  70. <tr><td>Upload CSV file</td><td><input name="userfile" type="file"></td>
  71.  
  72.  
  73.  
  74.  
  75. </tr>
  76. <tr><td colspan="2">  <input type="submit" name="upload" value="upload"></td></tr>
  77. </form>
  78. </table>
[Please use CODE tags when posting source code. Thanks! --pbmods]
Jul 9 '07 #1
3 1827
pbmods
5,821 Recognized Expert Expert
Heya, sumaabey.

What do you want your code to do? Give an example.
What is your code doing that you don't want it to do? Give an example.
What is your code *not* doing that it is supposed to? Give an example.
Jul 9 '07 #2
sumaabey
29 New Member
i want to import csv file into mysql data can you help me?
Jul 10 '07 #3
pbmods
5,821 Recognized Expert Expert
Heya, sumaabey.

i want to import csv file into mysql data can you help me?
MySQL has a LOAD DATA INFILE command for that.
Jul 10 '07 #4

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

Similar topics

2
7796
by: Jim | last post by:
Im getting way too many rows retured..what its trying to do is insert a 0 for revenue for months 7 - 12 (aka July through December) for each of these cost centers for each payor type..Im getting a lot of repeats and the concatenation field date always comes back as January 2003 instead of the month and date its supposed to --Fiscal...
1
5963
by: ickypick | last post by:
I am writing a parser to read in ftp log files and insert into a databse. I have everything working fine, except the legacy billing system being used generates bills for each user instead of the master account. Therefore, as a bandaid, I need to attach an account field to each uid for each insert. I figured I could do this using more if statements...
52
6273
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible variations(combination of fields), - then act on each group in some way ...eg ProcessRs (oRs as RecordSet)... the following query will get me the...
8
6466
by: SaltyBoat | last post by:
Needing to import and parse data from a large PDF file into an Access 2002 table: I start by converted the PDF file to a html file. Then I read this html text file, line by line, into a table using a code loop and an INSERT INTO query. About 800,000 records of raw text. Later, I can then loop through and parse these 800,000 strings into...
2
3217
by: recordlovelife | last post by:
So I am trying to display a title, date, and content of a wordpress blog. Word press provides nice drop in functions to get the job done with simple names like "the_title", and the "the_content" But on the homepage of a site, i wanted to truncate the content to like the first 75 characters and then put "..." (a perfect use of the smarty "truncate"...
5
2213
by: =?Utf-8?B?V2lsbGlhbSBGb3N0ZXI=?= | last post by:
Good evening all, I am trying to write a process that uses a while loop to cycle multiple files from an array throught the StreamReader Process. The whole thing works using: Dim Import_File_Reader As System.IO.StreamReader While ...
1
1332
by: Semajthewise | last post by:
Hi all!! What I am trying to do today is write to a textfile then read from the file get some lines from that file and edit them. and re-write them to the file. Not replace I want the edited lines at the end of the file. This is all being done while the file is still open. Below is my code. I have notated it to show what I am doing. What is...
10
2624
by: cricket | last post by:
Hello, I am trying to create a loop in an MS Access application that will read in a list of iSeries system names from a file in my Access database, and then submit a command to each of the listed systems. I have the code working for submitting the commands to one iSeries system at a time, the problem I am encountering is when I try to us a...
4
1810
vikas251074
by: vikas251074 | last post by:
Loop runs indefinite times. suppose v_router = 5 is entered by user, then following code runs indefinite times i.e. record is inserted indefinite times though each have different i and v_ip_address. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta...
1
7416
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7752
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5969
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5325
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.