hi all i need a script to upload csv to the server from local disk and then import data into mysql table i know its possible in phpmyadmin but i need a script to do that without login into cpanel thank you
Here's an article with well commented PHP code that should be easily adaptable to suit your purpose: http://www.codewalkers.com/c/a/Database-Code/Import-CSV-files-into-MySQL/ Good luck with your project. Chipzzz
thank you its very clear and straight but i still miss something i want to upload the file from local disk and this script needs that the file must be already found in the same directory with the same name of the table
Hi, Here is a tutorial from w3schools on uploading files with php: http://www.w3schools.com/PHP/php_file_upload.asp If you modify the first sections to accept csv files and replace the last section with a slightly modified version of the previous script, you should have no difficulty importing the uploaded file into your mysql database. I hope this helps, Chipzzz
i don`t know how to thank you but i try to do that and i failed , i am not php professional if you kindly join the two codes with needed modification this will be great Thank you in advance
Sorry, I thought you were trying to learn PHP. If you'd like the project written for you, Alberrambo will probably provide a tidier solution than the cobbled together code fragments, and his price seems quite reasonable. Sadly, there are demands on my time right now that would prevent me from doing it, and he did offer first. Best of luck with however you decide to pursue this, Chipzzz
thank you i tried and searched and got it thats the code if you have any improvments or comments and also to help any one searching for that in the future <form enctype="multipart/form-data" action="1up.php" method="post"> File to import:<br /> <input size='30' type='file' name='filename'> <input type="submit" name="submit" value="Upload"></form> <?php include "connect.php"; //Upload File if (isset($_POST['submit'])) { if (is_uploaded_file($_FILES['filename']['tmp_name'])) { echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>"; $handle = fopen($_FILES['filename']['tmp_name'], "r"); $data = fgetcsv($handle, 1000, ","); } //Import uploaded file to Database $row = 1; $handle = fopen($_FILES['filename']['tmp_name'], "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { if ( $row == 1){ $row++; } else { //Update Database Values $import="insert into aaaa(name, report1, report2 ) VALUES('".mysql_real_escape_string($data[0])."', '".mysql_real_escape_string($data[2])."', '".mysql_real_escape_string($data[3])."' )"; mysql_query($import) or die(mysql_error()); } } fclose($handle); } ?> PHP:
There are still some people in the world, who have got enough time to help other. Don't be so 'Sorry' and 'Sad' for these help seekers. You can try some Freelancer's site, where you get paid for your effort and time. Please don't make these free forums an Open market. Solution for the Thread Starter: <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> File to import:<br /> <input size='30' type='file' name='filename'> <input type="submit" name="submit" value="Upload"></form> <?php //Assuming that, your connect.php file contains the database related information, ie. hostname, username etc. include "connect.php"; //Upload File if (isset($_POST['submit'])) { if (is_uploaded_file($_FILES['filename']['tmp_name'])) { //Import uploaded file to Database $row = 1; $handle = fopen($_FILES['filename']['tmp_name'], "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { //Update Database Values $import="insert into test (name, data) VALUES('".mysql_real_escape_string($data[0])."', '".mysql_real_escape_string($data[1])."')"; mysql_query($import) or die(mysql_error()); } fclose($handle); } } ?> PHP: Remember to save the file with .PHP extension. As, you mentioned, you're not a professional, you may not understand the code completely. For that case, send me the exact mysql table schema. I'll modify the code accordingly.
techbongo Thank you very much and i am so pleased to know that there is somonr in this world who help others without asking for a price Chipzzz you make what you can do and helped as you can so thank you and welcome to this forum as new member techbongo its a chance for me and all other begainers to learn from you so, i see you make few changes on the code i posted please kindly can you explain for us the difference for example : you delete this line if ( $row == 1){ $row++; } else { why and what this line do exactly thank you very much all of you
I see that you've been waiting for an answer for a while so let me chime in here, if nobody minds. Often csv files begin with a header line that contains information that you don't want added to the database when the file is imported. The lines you asked about prevented that header from being imported. Good luck with your project Chipzzz
Chipzzz, Thank you for your understanding. I also never meant to hurt you, rather suggested some way that pays your effort. Cheers bro. @meldweny You're always welcome. Chipzzz explained exact reason for using the code snippet. I removed it because, in ideal cases, there won't be any column headers in your CSV. If you're sure that you've column headers in your csv, only then alter the code. However, you can customize the code to read the first row and identify if it's a column header or not. For that you may need to play with some regular expressions.(ie. to check, if the name field of the row contains the string, "Name").
Hi, I don't like coding for such an "simple" task of import a Excel file. maybe the PHP script www.dbtube.org fits your requirements. Greetings
Hi folks, i appreciate that this thread is quite old, but it answered most of what i needed, so rather than start a new thread .... Anyway, the above script, as amended by techbongo, is pretty much what i need, but i would like a small modification or direction as to how i may do it myself. I'm an absolute novice so please be gentle with me. The above script works well providing the table has already been created, but if it hasn't it obviously throws an error stating the table does not exist. What i would like is for it to look for the table and if it doesn't exist then create it .. so i had a search around and found this ... //Create Table CREATE TABLE IF NOT EXISTS users ( FirstName varchar(25), SecondName varchar(25), Department varchar(25) NULL ); Code (markup): I placed this snippet of code between //Upload File & //Import uploaded file to Database .. Obviously it doesn't work, but i have no idea where it is going wrong. I would appreciate any help along my tough learning curve. Thanks