TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How to Insert Multiple Record of Excel Sheet using PHP MySQL
Bhushan Singh
Jan 15
2015
Code
10.8
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
import.zip
<?php
define (
"DB_HOST"
,
"localhost"
);
// set database host
define (
"DB_USER"
,
"root"
);
// set database user
define (
"DB_PASS"
,
""
);
// set database password
define (
"DB_NAME"
,
"api_db"
);
// set database name
$link
= mysql_connect(DB_HOST, DB_USER, DB_PASS)
or
die
(
"Couldn't make connection."
);
$db
= mysql_select_db(DB_NAME,
$link
)
or
die
(
"Couldn't select database"
);
set_include_path(get_include_path() . PATH_SEPARATOR .
'Classes/'
);
include
'PHPExcel/IOFactory.php'
;
$uploadedStatus
= 0;
if
( isset(
$_POST
[
"submit"
]) ) {
if
( isset(
$_FILES
[
"file"
])) {
//if there was an error uploading the file
if
(
$_FILES
[
"file"
][
"error"
] > 0) {
echo
"Return Code: "
.
$_FILES
[
"file"
][
"error"
] .
"<br />"
;
}
else
{
$imagename
=
$_FILES
[
"file"
][
"name"
] ;
move_uploaded_file(
$_FILES
[
"file"
][
"tmp_name"
],
$imagename
);
try {
$objPHPExcel
= PHPExcel_IOFactory::load(
$imagename
);
} catch(Exception
$e
) {
die
(
'Error loading file "'
.
pathinfo
(
$imagename
,PATHINFO_BASENAME).
'": '
.
$e
->getMessage());
}
$allDataInSheet
=
$objPHPExcel
->getActiveSheet()->toArray(null,true,true,true);
$arrayCount
=
count
(
$allDataInSheet
);
// Here get total count of row in that Excel sheet
for
(
$i
=2;
$i
<=
$arrayCount
;
$i
++){
$userName
= trim(
$allDataInSheet
[
$i
][
"A"
]);
$userMobile
= trim(
$allDataInSheet
[
$i
][
"B"
]);
insertTable
= mysql_query(
"insert into tbl_users (usr_name, usr_email) values('"
.
$userName
.
"', '"
.
$userMobile
.
"');"
);
}
echo
"Please check your database "
;
}
}
else
{
echo
"No file selected <br />"
;
}
}
?>
<form action=
"<?php echo $_SERVER["
PHP_SELF
"]; ?>"
method=
"post"
enctype=
"multipart/form-data"
>
<table width=
"600"
style=
"margin:115px auto; background:#f8f8f8; border:1px solid #eee; padding:10px;"
>
<tr><td colspan=
"2"
style=
"font:bold 21px arial; text-align:center; border-bottom:1px solid #eee; padding:5px 0 10px 0;"
>Demo
for
multiple data store using execel sheet</t
<tr><td colspan=
"2"
style=
"font:bold 15px arial; text-align:center; padding:0 0 5px 0;"
>Data Uploading System</td></tr>
<tr>
<td width=
"50%"
style=
"font:bold 12px tahoma, arial, sans-serif; text-align:right; border-bottom:1px solid #eee; padding:5px 10px 5px 0px; border-right:1px solid #eee;"
>Select file</td>
<td width=
"50%"
style=
"border-bottom:1px solid #eee; padding:5px;"
>
<input type=
"file"
name=
"file"
id=
"file"
/></td>
</tr>
</tr>
<td style=
"font:bold 12px tahoma, arial, sans-serif; text-align:right; padding:5px 10px 5px 0px; border-right:1px solid #eee;"
>Submit</td>
<td width=
"50%"
style=
" padding:5px;"
><input type=
"submit"
name=
"submit"
/></td>
</tr>
</table>
</form>
how to insert multiple record of excel sheet using php mysql