web 2.0

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

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


<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<form action="php_csv_mysql_upload2.php" method="post" enctype="multipart/form-data" name="form1">
<input name="fileCSV" type="file" id="fileCSV">
<input name="btnSubmit" type="submit" id="btnSubmit" value="Submit">
</form>
</body>
</html>

php_csv_mysql_upload2.php


<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<?
copy($_FILES["fileCSV"]["tmp_name"],"shotdev/".$_FILES["fileCSV"]["name"]); // Copy/Upload CSV

$objConnect = mysql_connect("localhost","root","root") or die(mysql_error()); // Conect to MySQL
$objDB = mysql_select_db("mydatabase");

$objCSV = fopen("shotdev/".$_FILES["fileCSV"]["name"], "r");
while (($objArr = fgetcsv($objCSV, 1000, ",")) !== FALSE) {
$strSQL = "INSERT INTO customer ";
$strSQL .="(CustomerID,Name,Email,CountryCode,Budget,Used) ";
$strSQL .="VALUES ";
$strSQL .="('".$objArr[0]."','".$objArr[1]."','".$objArr[2]."' ";
$strSQL .=",'".$objArr[3]."','".$objArr[4]."','".$objArr[5]."') ";
$objQuery = mysql_query($strSQL);
}
fclose($objCSV);

echo "Import completed.";
?>
</body>
</html>

Create a php file and save to path root-path/myphp/

Run
http://localhost/myphp/php_csv_mysql_upload1.php

Screenshot

PHP & Upload and Convert/Import CSV To MySQL

PHP & Upload and Convert/Import CSV To MySQL
.
.
.

Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (1 votes, average: 1.00 out of 10)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.