|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hey,
I'm uploading a csv file into a page called(naughty.php) where I have a text box with option select the file and browse button and finally the import button. Now How do I parse the csv file which is loaded in the text box and then store the contents in a DB using php on clicking the submit button ( where value= import ) P.S: I want to parse only the csv file at time when it's uploaded in the text box. I've already worked with parse of csv file with static location, but this is something I need desperate help! Help me! |
|
#2
|
|||
|
|||
|
1. Upload the file to the server
2. Read the file into an array with file() 3. loop over the array and use explode() to get the values within each line 4. insert exploded values to the db. |
|
#3
|
|||
|
|||
|
Hey Matthew,
Thanks for the reply. Is the below code right? PHP Code:
The places where I had highlighted as bold text is where I have a doubt. For name and tmp_name what should I give. Also in the $line var where I need to place my csv file, what should I give there too. Hope the db part is right. Help me! |
|
#4
|
|||
|
|||
|
Okay, I changed the fields etc. to one of my test tables so make sure you change them back to the values you need.
I commented the file pretty well, so it should make sense. One thing, if your "ID" field in the database is an autoincrement (which it usually should be) field, you don't have to pass it in the query, you just pass the datafields and mysql knows to increment the ID. PHP Code:
the sample file I used simply contained three fields Code:
Data for field 1, some data for field 2, and a bit of data for field 3 Just some test stuff, Mary had a little lamb, roses are red violets are blue, blah blah blah, and final field Last edited by MatthewJ : October 14th, 2009 at 03:05 PM. |
|
#5
|
|||
|
|||
|
Hey Mathew,
I tried the below code, but the output is blank, but when I disable the PHP part, I can see the HTML output. But with PHP and HTML part together, I'm unable to see the HTML part I saved the file as sample.php ( C:/Apache2.2/htdocs ) Why is that, how to fix this? |
|
#6
|
|||
|
|||
|
PHP Code:
|
|
#7
|
|||
|
|||
|
This is my entire code, just to check to whether html and php work together
PHP Code:
So I should see the html part on running http://localhost/sample.php. But all I see is just a blank page. Why |
|
#8
|
|||
|
|||
|
add error_reporting(E_ALL); as the first line under the first <?php tag. looks like there is a parse/fatal error that is not allowing the script to run and your server is set to not show errors.
|
|
#9
|
|||
|
|||
|
Change:
$connect = mysql_connect("localhost", "root", ""); or die("Unable to connect"); to: $connect = mysql_connect("localhost", "root", "") or die("Unable to connect");
__________________
Sir, a desire of knowledge is the natural feeling of mankind; and every human being, whose mind is not debauched, will be willing to give all that he has to get knowledge. |
|
#10
|
|||
|
|||
|
I tried changing mysql_connect and also added
error_reporting(E_ALL); Still no luck. FYI, if I try to run any php page my browser is blank, not sure why. Maybe I didn't configure php in right way to make it work with Apache web server. |
|
#11
|
|||
|
|||
|
try making a php file named phpinfo.php and putting nothing but:
PHP Code:
in the file. if you don't have php installed/configured correctly, you will see nothing. if you do have php installed, you will get a bunch of details about your php setup. And when installing php, you should just get the installer and let it auto-configure the server for you. You shouldn't have to do pretty much any configuration to get it to work at least a little. |
|
#12
|
|||
|
|||
|
I did see the php info page.
But all the files with php extension remains blank on the web browser. Not sure WHY |
|
#13
|
|||
|
|||
|
For now I'm not worried about DB part, coz I'm planning to store as whole csv file rather than parsing and then storing.
But I want to explode or parse the csv which is uploaded on my page1.php to display on page2.php. I tried below code but still no luck: PHP Code:
I want to parse csv the file which is been uploaded in page1.php( Upload a file ) and display the content of csv file without comma or table format format on page2.php ( the php part above ) |
|
#14
|
|||
|
|||
|
How to upload files:
http://www.w3schools.com/PHP/php_file_upload.asp How to use fopen (while file is on the server): http://www.php.net/manual/en/function.fopen.php Use fgetcsv, after using fopen: http://php.net/manual/en/function.fgetcsv.php |
|
#15
|
|||
|
|||
|
Hey James,
PHP Code:
The output: Code:
Data for field 1 some data for field 2 and a bit of data for field 3 Just some test stuff Mary had a little lamb roses are red violets are blue blah blah blah and final field This works fine if I run http://localhost/page2.php. But if I edit as below, I don't get the above output. PHP Code:
Which I presume on submit, read the file uploaded in field datafile( name parameter for upload the file on my HTML form ) Any help! |
![]() |
| Viewing: Codewalkers Forums > PHP Related > PHP Coding > Parse a csv file and then store it in db |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|