473,402 Members | 2,072 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,402 software developers and data experts.

how can i write my data from text file into database

hi..
i m trying to write data from txt file onto database but when i am executing my php code for it the empty record will be inserted in the database not the data written on txt file will be entered.plz help me out
Expand|Select|Wrap|Line Numbers
  1.  <html>
  2. <?
  3.  
  4. $con=mysql_connect("localhost","root","root");
  5. if (!$con)
  6.   {
  7.   die('Could not connect: ' . mysql_error());
  8.   }
  9.  
  10. mysql_select_db('test',$con);
  11. //(1) Read the text file into a variable
  12. $file = "testfile.txt";
  13. $fp = fopen($file, "r");
  14. $data = fread($fp, filesize($file));
  15. fclose($fp);
  16.  
  17.  
  18. //(2) Then we can get rid of the tabs in there:
  19.  
  20. $output = str_replace("\t|\t", "|", $data);
  21.  
  22.  
  23. //(3) Then we explode it at every line break
  24.  
  25. $output = explode("\n", $output);
  26.  
  27.  
  28. //(4) Then we loop through all the array elements and explode them again and insert them into mysql
  29.  
  30. foreach($output as $var)
  31. $tmp = explode("|", $var);
  32. $FirstName = $tmp[0];
  33. $LastName = $tmp[1];
  34. $Age = $tmp[2];
  35. $sql = "INSERT INTO student SET FirstName='$FirstName', LastName='$LastName',Age='$Age'";
  36. mysql_query($sql);
  37.  
  38.  
  39.  
  40. echo "Done!";
  41. mysql_close($con);
  42. ?>     </html>
  43.  
.
Jan 28 '10 #1
3 2499
Atli
5,058 Expert 4TB
Hey.

First of all, your foreach loop is - undoubtedly - not working as you want it to. Anything that should belong to a loop should be put inside brackets following the loop.

If the foreach is not followed by brackets ({}), it will assume only the very first line following it belongs to the loop. Therefore you should ALWAYS use brackets. Even if there is only one line. (For the sake of consistency)
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Some random values to loop through.
  3. $values = array("first", "second", "third");
  4.  
  5. // This only prints one value: "third"
  6. foreach($values as $_value)
  7.     $value = $_value; // This is inside the loop
  8.     echo $value; // This is OUTSIDE the loop.
  9.  
  10. // This prints all three values.
  11. foreach($values as $_value) {
  12.     $value = $_value;
  13.     echo $value;
  14. }
  15. ?>
Also, you should verify that the data is valid before using it in the query. Make sure the values you are using aren't empty, and that they contain the data you expect them to contain.

And as always, when dealing with MySQL queries: run ALL values through mysql_real_escape_string before putting it into the query. Otherwise your database is open for SQL Injection attacks. (If you think the data you are using is secure... you are wrong! :P)
Jan 28 '10 #2
dgreenhouse
250 Expert 100+
I see said the blind man... :-) I didn't notice the brackets... :-)

Is that really how the data is delimited?
i.e. with \t|\t

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // This works...
  3. $b = "\t|\t";
  4. $input = "John\t|\tSmith\nMary\t|\tJohnson";
  5. $output = str_replace($b,'|',$input);
  6. $output = explode("\n",$output);
  7. foreach($output as $var) {
  8.   $tmp = explode("|",$var);
  9.   echo $tmp[0] . ' ' . $tmp[1] . '<br>';
  10. }
  11.  
  12. ?>
  13.  
Also, if the file isn't very large, you can use the 'file()' command to create an array of lines. Then you can explode each line (as you're doing) on your replaced delimiter ('|').
Jan 28 '10 #3
thanx for d help..........
Feb 1 '10 #4

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

Similar topics

3
by: john | last post by:
I need to produce a RTF-document which is filled with data from a database. I've created a RTF-document in WordPad (a template, so to speak) which contains 'placeholders', for example '<dd01>',...
3
by: belange | last post by:
I try to create a little web application with c# and visual studio.net. With this application, a student can reserved a computer station, the day and the hour. I want to write these informations in...
4
by: marcmc | last post by:
Hi, I hope to create a XML file that will hold my Connection data to a SQL Db. I want to write an XML file from 4 Text Box named UserName, Password, Database & Server and later read from it. My...
3
by: sonu | last post by:
I am getting some data 'strData' from database through reader object. The data 'strData' is sent to a doc using FilesystemObject line by line in doc file located on a specific location. 1.Code...
0
by: Svenn Bjerkem | last post by:
Hi, Armed with Programming Python 3rd Edition and Learning Python 2nd edition I try to write an application which I at first thought was simple, at least until I was finished with the GUI and...
4
by: Billy | last post by:
Hi all, I'm building a text file from a database table using the ASP Write Method and would like to position the cursor in a specific column position before writing the fields. As I loop through...
12
by: =?Utf-8?B?Smlt?= | last post by:
I have code that reads and parses a text file using a schema.ini file. Works great. When I see the dataGrid it's exactly what I want. Dim CString As String = _...
4
by: Ross | last post by:
Hello, I am trying to Read and Write to a text file on a web server using Microsoft Visual Basic 2005 Express Edition. So far I have managed to complete my testing with a local text file using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
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.