Learning & Integrating web technology and code help directory

How to use PHP & Upload and Convert/Import CSV To MySQL

No comments
How to use PHP & Upload and Convert/Import CSV To MySQL Learn / tutorial php programming how to using  PHP Upload and Convert/Import CSV To MySQL
ShotDev Focus:
- PHP Upload and Convert/Import CSV To MySQL.
Example
php_csv_mysql_upload1.php
  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <form action="php_csv_mysql_upload2.php" method="post" enctype="multipart/form-data" name="form1">  
  7. <input name="fileCSV" type="file" id="fileCSV">  
  8. <input name="btnSubmit" type="submit" id="btnSubmit" value="Submit">  
  9. </form>  
  10. </body>  
  11. </html>  
php_csv_mysql_upload2.php
  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <?  
  7. copy($_FILES["fileCSV"]["tmp_name"],"shotdev/".$_FILES["fileCSV"]["name"]); // Copy/Upload CSV  
  8.   
  9. $objConnect = mysql_connect("localhost","root","root"or die(mysql_error()); // Conect to MySQL  
  10. $objDB = mysql_select_db("mydatabase");  
  11.   
  12. $objCSV = fopen("shotdev/".$_FILES["fileCSV"]["name"], "r");  
  13. while (($objArr = fgetcsv($objCSV, 1000, ",")) !== FALSE) {  
  14. $strSQL = "INSERT INTO customer ";  
  15. $strSQL .="(CustomerID,Name,Email,CountryCode,Budget,Used) ";  
  16. $strSQL .="VALUES ";  
  17. $strSQL .="('".$objArr[0]."','".$objArr[1]."','".$objArr[2]."' ";  
  18. $strSQL .=",'".$objArr[3]."','".$objArr[4]."','".$objArr[5]."') ";  
  19. $objQuery = mysql_query($strSQL);  
  20. }  
  21. fclose($objCSV);  
  22.   
  23. echo "Import completed.";  
  24. ?>  
  25. </body>  
  26. </html>  
Create a php file and save to path root-path/myphp/
Screenshot
PHP & Upload and Convert/Import CSV To MySQL
PHP & Upload and Convert/Import CSV To MySQL
.

No comments :

Post a Comment