I want to import the excel sheet data into mysql table with php but i am getting these errors someone please get me out from this.. please look on php code only ignore html stuff.
<?php
include ("connection.php");
if(isset($_POST["submit"]))
{
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
$name = $filesop[1];
$email = $filesop[2];
$sql = mysql_query("INSERT INTO co (name, email) VALUES ('$name','$email')");
$c = $c + 1;
}
fcose($file);
if($sql){
echo "You database has imported successfully. You have inserted ". $c ." recoreds";
}else{
echo "Sorry! There is some problem.";
}
}
?>
this is excel sheet
in database it is showing different format
There are libraries excellibrary/php-excel-reader/excel_reader2.php and excellibrary/SpreadsheetReader.php
you can use these libraries to read your content and convert it into array.
Once you convert your spreadsheet data into an array, you can easily insert into database using iterations.
Related
I am trying to import data from Excel to MySQL using PHP. The code I am using imports the data but the language is strange.
Below is the PHP code:
<?php
include_once("conn.php");
$filename= "Financial Sample.xlsx";
$file = fopen($filename, "r");
$count = 0; // add this line
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
//print_r($emapData[0]);
//exit();
$count++; // add this line
if($count>1){ // add this line
//echo html_entity_decode("žūų");
//echo json_encode($emapData[0]);
$insert_q = "INSERT into questions(q_describe) values ('$emapData[0]')";
if($query_q=$mysqli->query($insert_q))
{
$final=array();
$final['status']="success";
$final['message']="Inserted Successfully";
}
else
{
$er = $mysqli->error;
$final['status']=$er;
$final['message']="Error";
}
echo json_encode($final);
} // add this line
}
fclose($file);
?>
Once imported, data is seen like this in phpMyAdmin:
Actual Excel is shown below:
Where am I going wrong?
Edit
Showing the structure of my table:
If you look at the structure of your table, you're looking for two fields: encoding and Collation.
Make sure they're set to cp1252 West European and latin1_swedish_ci respectively.
I tried to upload text file to insert into mysql. It works fine with a small number of rows but when rows more than 1000 it's hanging.
My probem is:
code is very slow and hanging with large text file
process bar not working
how it works with text and cvs, xls, xlsx files
The code:
//Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");
$t = "0";
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$mobile=$data[0];
$mobile = clean_value(mobile_filter($mobile));
if (preg_match('/^[0-9]{12,12}$/i',$mobile)) {
$check_mob = mysql_query("SELECT * FROM book WHERE mobile = '$mobile' AND owner = '$cp_username' AND groupname = '$groupname'");
if(mysql_num_rows($check_mob) == 0) {
$import="INSERT into book(nickname, mobile, groupname, owner) values('$nickname','$mobile','$groupname','$cp_username')";
mysql_query($import) or die(mysql_error());
$t = $t+1;
echo "<div class=\"progress\">
<div class=\"progress-bar\" role=\"progressbar\" aria-valuenow=\"$t\" aria-valuemin=\"0\" aria-valuemax=\"100\" style=\"width:$t%\">
$t%
</div>
</div>";
}
}
}
You actually can do this with using only PHP too.
This is a example code.
$sql="INSERT INTO .....";
$count++;
if($count==500){
// wait for 5 seconds
usleep(5000000);
$count=0;
}
You will need to put this code in the loop that you save line by line into database. When the system saves first 500 record, it will sleep for 5 seconds and continue.
here is my code to import csv data to my database
if (isset($_POST["submit"])) {
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
**while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
$name = $filesop[0];
$email = $filesop[1];
$sql = mysql_query("INSERT INTO selleruser (emaili) VALUES ('$name')");
$c = $c + 1;
}**
if ($sql) {
echo "You database has imported successfully. You have inserted ". $c ." recoreds";
} else {
echo "Sorry! There is some problem.";
}
}?>
</div>
I have a csv file where there is a column which contains emails
the import is done suvcessfull but the issue is that it just imports the value in another format like in other language or encripted or something un readable
Try this query to import csv from phpmyadmin or any other mysql client
Before firing query, please make sure that table matching the fields is already created into db.
LOAD DATA LOCAL INFILE 'local machine path to csv file' INTO TABLE `selleruser`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(emaili);
I have a problem when I upload csv file to mysql.
I used this code:
if(isset($_POST["submit"])) {
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
$row=1;
while(($filesop = fgetcsv($handle, 1000, ",")) !== false) {
if ($row ==1) {
$row++;
continue;
}
$name = $filesop[0];
$phone = $filesop[1];
$sql = runsql("INSERT INTO messages_recipient (user_id,full_name, phone,date_added) VALUES ($SVARS[user_id],'$name','$phone',NOW())");
}
if($sql) {
echo "You database has imported successfully";
} else {
echo "Sorry! There is some problem.";
}
}
it's worked fine except when i upload csv file with hebrew values i get blank values in mysql.
i tried to change the type to text/long text without success.
the collation is: utf8_general_ci.
thanks for the help
You may be looking for:
mysqli_query("SET NAMES 'utf8'");
and
mysqli_query("SET CHARACTER SET utf8 ");
Use it before the query.
mysqli.set-charset:
http://php.net/manual/en/mysqli.set-charset.php
I have created a website which requires me to upload a csv file into the mysql database (Wamp server). Since I have never done this before a detailed answer with steps will be really helpful. I need the user to upload file using html input file option and then a php code to upload this file to mysql database. Iam using this code
<?php
$con=mysql_connect("localhost","","");
mysql_select_db("sg",$con);
define('CSV_PATH','C:/Users/mkutbudd/Desktop/');
$csv_file = CSV_PATH . "dum.csv";
if (($getfile = fopen($csv_file, "r")) !== FALSE) {
$data = fgetcsv($getfile, 1000, ",");
while (($data = fgetcsv($getfile, 1000, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
$result = $data;
$str = implode(",", $result);
$slice = explode(",", $str);
$col1 = $slice[0];
$col2 = $slice[1];
$col3 = $slice[2];
$query = "INSERT INTO dummy(dum1,dum2,dum3)
VALUES('".$col1."','".$col2."','".$col3."')";
$s=mysql_query($query, $con );
}
}
}
echo "File data successfully imported to database!!";
mysql_close($con);
?>
Your first step is to allow user to upload a file, and then you need to handle the errors if there are, validate the file, and if everything is correct, then move the uploaded file to its place: http://php.net/manual/en/features.file-upload.php
Then you need to parse your file, with a php function for it: http://hu1.php.net/manual/en/function.fgetcsv.php
After this, you need to create a connection to the database: http://hu1.php.net/manual/en/book.mysqli.php
And then you need to insert your data into your table with a loop, like foreach on your array, what you created from fgetcsv: http://hu1.php.net/manual/en/control-structures.foreach.php
There are an alternative way, if you have right to run system or execute functions and you have right to run mysql command, then you can use the mysql load data command: http://dev.mysql.com/doc/refman/5.1/en/load-data.html
Done.