• We just launched and are currently in beta. Join us as we build and grow the community.

Import Database Tables Using PHP/MySQL

I Rep
0
0
0
Rep
0
I Vouches
0
0
0
Vouches
0
Posts
167
Likes
72
Bits
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”.

  1. <

    form action=

    "import.php"

    method=

    "POST"

    enctype=

    "multipart/form-data"

    >
  2. <

    input type=

    "file"

    name=

    "image"

    class

    =

    "ed"

    ><

    br />
  3. <

    input type=

    "submit"

    value=

    "Save"

    />
  4. </

    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”.

  1. <?php


  2. // new data
  3. $file

    =

    $_FILES

    [

    'image'

    ]

    [

    'tmp_name'

    ]

    ;
  4. $image

    =

    addslashes

    (

    file_get_contents

    (

    $_FILES

    [

    'image'

    ]

    [

    'tmp_name'

    ]

    )

    )

    ;
  5. $image_name

    =

    addslashes

    (

    $_FILES

    [

    'image'

    ]

    [

    'name'

    ]

    )

    ;
  6. $image_size

    =

    getimagesize

    (

    $_FILES

    [

    'image'

    ]

    [

    'tmp_name'

    ]

    )

    ;


  7. move_uploaded_file

    (

    $_FILES

    [

    "image"

    ]

    [

    "tmp_name"

    ]

    ,

    ""

    .

    $_FILES

    [

    "image"

    ]

    [

    "name"

    ]

    )

    ;

  8. $filename

    =

    $_FILES

    [

    "image"

    ]

    [

    "name"

    ]

    ;

  9. $mysql_host

    =

    'localhost'

    ;
  10. $mysql_username

    =

    'root'

    ;
  11. $mysql_password

    =

    ''

    ;
  12. $mysql_database

    =

    'tutorials'

    ;

  13. // Connect to MySQL server
  14. mysql_connect

    (

    $mysql_host

    ,

    $mysql_username

    ,

    $mysql_password

    )

    or die

    (

    'Error connecting to MySQL server: '

    .

    mysql_error

    (

    )

    )

    ;
  15. // Select database
  16. mysql_select_db

    (

    $mysql_database

    )

    or die

    (

    'Error selecting MySQL database: '

    .

    mysql_error

    (

    )

    )

    ;
  17. mysql_query

    (

    "SET NAMES 'utf8'"

    )

    ;
  18. // Temporary variable, used to store current query
  19. $templine

    =

    ''

    ;
  20. // Read in entire file
  21. $lines

    =

    file

    (

    $filename

    )

    ;
  22. // Loop through each line
  23. foreach

    (

    $lines

    as

    $line

    )
  24. {
  25. // Skip it if it's a comment
  26. if

    (

    substr

    (

    $line

    ,

    0

    ,

    2

    )

    ==

    '--'

    ||

    $line

    ==

    ''

    )
  27. continue

    ;

  28. // Add this line to the current segment
  29. $templine

    .=

    $line

    ;
  30. // If it has a semicolon at the end, it's the end of the query
  31. if

    (

    substr

    (

    trim

    (

    $line

    )

    ,

    -

    1

    ,

    1

    )

    ==

    ';'

    )
  32. {
  33. // Perform the query
  34. mysql_query

    (

    $templine

    )

    or print

    (

    'Error performing query \'<strong>'

    .

    $templine

    .

    '\': '

    .

    mysql_error

    (

    )

    .

    '<br /><br />'

    )

    ;
  35. // Reset temp variable to empty
  36. $templine

    =

    ''

    ;
  37. }
  38. }
  39. echo

    '<div style="text-align: center; margin-top: 50px;">'

    ;
  40. echo

    "Tables imported successfully<br>"

    ;
  41. $dir

    =

    dirname

    (

    __FILE__

    )

    ;
  42. echo

    'form'

    .

    $dir

    .

    '/'

    .

    $filename

    .

    'To '

    .

    $mysql_database

    .

    '<br>'

    ;
  43. echo

    '<a href="../index.php">Back</a>'

    ;
  44. echo

    '</div>'

    ;



  45. ?>

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.
 

452,292

324,271

324,279

Top