ImaBeefWithPCs
Elite
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
100 XP
This tutorial will teach you on how create a script that restore or import database tables using PHP. The feature of this tutorial is it allows the user to restore or imports database backup without using the phpmyadmin panel. To understand more about this tutorial follow the steps bellow
Creating Our Database
First we are going to create our database which stores our data.
To create a database:
1. Open phpmyadmin
2. Then create database and name it as "tutorials".
Creating Our Form
The code bellow will provide us the form where we can attach our sql file. Copy the link bellow and save it as “index.php”.
Writing Our Script that Upload and Import our databse
The codes bellow includes our database connection, upload script and import to database script opy the code bellow and save it as “import.php”.
Note. Use the birthday.sql file attach together with this tutorial when you browse for database table.
Hope this code will help you.
Download
Creating Our Database
First we are going to create our database which stores our data.
To create a database:
1. Open phpmyadmin
2. Then create database and name it as "tutorials".
Creating Our Form
The code bellow will provide us the form where we can attach our sql file. Copy the link bellow and save it as “index.php”.
- <
form action=
"import.php"
method=
"POST"
enctype=
"multipart/form-data"
>
- <
input type=
"file"
name=
"image"
class
=
"ed"
><
br />
- <
input type=
"submit"
value=
"Save"
/>
- </
form>
Writing Our Script that Upload and Import our databse
The codes bellow includes our database connection, upload script and import to database script opy the code bellow and save it as “import.php”.
- <?php
- // new data
- $file
=
$_FILES
[
'image'
]
[
'tmp_name'
]
;
- $image
=
addslashes
(
file_get_contents
(
$_FILES
[
'image'
]
[
'tmp_name'
]
)
)
;
- $image_name
=
addslashes
(
$_FILES
[
'image'
]
[
'name'
]
)
;
- $image_size
=
getimagesize
(
$_FILES
[
'image'
]
[
'tmp_name'
]
)
;
- move_uploaded_file
(
$_FILES
[
"image"
]
[
"tmp_name"
]
,
""
.
$_FILES
[
"image"
]
[
"name"
]
)
;
- $filename
=
$_FILES
[
"image"
]
[
"name"
]
;
- $mysql_host
=
'localhost'
;
- $mysql_username
=
'root'
;
- $mysql_password
=
''
;
- $mysql_database
=
'tutorials'
;
- // Connect to MySQL server
- mysql_connect
(
$mysql_host
,
$mysql_username
,
$mysql_password
)
or die
(
'Error connecting to MySQL server: '
.
mysql_error
(
)
)
;
- // Select database
- mysql_select_db
(
$mysql_database
)
or die
(
'Error selecting MySQL database: '
.
mysql_error
(
)
)
;
- mysql_query
(
"SET NAMES 'utf8'"
)
;
- // Temporary variable, used to store current query
- $templine
=
''
;
- // Read in entire file
- $lines
=
file
(
$filename
)
;
- // Loop through each line
- foreach
(
$lines
as
$line
)
- {
- // Skip it if it's a comment
- if
(
substr
(
$line
,
0
,
2
)
==
'--'
||
$line
==
''
)
- continue
;
- // Add this line to the current segment
- $templine
.=
$line
;
- // If it has a semicolon at the end, it's the end of the query
- if
(
substr
(
trim
(
$line
)
,
-
1
,
1
)
==
';'
)
- {
- // Perform the query
- mysql_query
(
$templine
)
or print
(
'Error performing query \'<strong>'
.
$templine
.
'\': '
.
mysql_error
(
)
.
'<br /><br />'
)
;
- // Reset temp variable to empty
- $templine
=
''
;
- }
- }
- echo
'<div style="text-align: center; margin-top: 50px;">'
;
- echo
"Tables imported successfully<br>"
;
- $dir
=
dirname
(
__FILE__
)
;
- echo
'form'
.
$dir
.
'/'
.
$filename
.
'To '
.
$mysql_database
.
'<br>'
;
- echo
'<a href="../index.php">Back</a>'
;
- echo
'</div>'
;
- ?>
Note. Use the birthday.sql file attach together with this tutorial when you browse for database table.
Hope this code will help you.
Download
You must upgrade your account or reply in the thread to view the hidden content.