Jump to content

[SOLVED] import sql file into database


trpplayer79

Recommended Posts

Dear,

 

I would like to create a page where i can select a sql file on my local disk and when I press import (submit) the sql file is imported into the mysql database i have on my server.

 

I found a script to import the sql but I don't find the solution to select the file and handle the selected file.

http://www.phpfreaks.com/quickcode/SQL-script-import-function/661.php

I know i have to create a input type file with the form attributes method="post" and enctype="multipart/form-data".

but how do i handle the file in the php page?

Link to comment
https://forums.phpfreaks.com/topic/67928-solved-import-sql-file-into-database/
Share on other sites

If it is off your local disk then you don't need to do any uploading. You can accept the location of the file as a string and use file_get_contents() to read the file.

mysql_query(file_get_contents($_PUT['fileloc']));

 

More filesystem functions: http://www.php.net/manual/en/ref.filesystem.php

I'l get the error:

Undefined variable: _put in I:\PortableOffice\Webserver\Root\dbtest\pages\update.php on line 7

the form code is:

<form method="put" name="updaten" action="update.php" enctype="multipart/form-data">
<input type="file" name="file2upl" size="36"><br><br>
<input type="submit" value="Import">
</form>

and the php (sumbit) code is:

<?
mysql_query(file_get_contents($_put['file2upl']));
?>

If there ever was a global variable called $_PUT, it would need to be capitalized; however, since my brain aparently wasn't working, you will need to disregard my previous post and use $_POST in place of $_PUT (remember to capitalize.) You will also need to change the method on your form to post.

 

Sorry about that.

sorry but i'll get the next (different) error:

Notice: Undefined index: file2upl in I:\PortableOffice\Webserver\Root\dbtest\pages\update.php on line 12

 

my code is like this:

html file:
<html><head>
<title>agenda updaten</title>
</head>
<body>
<center>
<form method="POST" name="updaten" action="update.php" enctype="multipart/form-data">
<input type="file" name="file2upl" size="36"><br><br>
<input type="submit" value="Import">
</form>
</center>
</body></html>

php file:
<?
function connect_db(){
GLOBAL $connect;
$connect=mysql_connect("localhost", "muziek", "muziek");
// om te connecteren zorg dat het paswoord voor deze gebruiker op de oude manier opgeslagen wordt in mysql
// volgend commando uitvoeren: set password for webwinkel@localhost=old_password('webwinkel')
if($connect && mysql_select_db("muziek"))
	return ($connect);
return (FALSE);
}
if(!isset($connect)){connect_db();}
mysql_query(file_get_contents($_POST['file2upl']));
?>

I'm trying to create the script in one page.

 

the current code is:

<?
function connect_db(){
GLOBAL $connect;
$connect=mysql_connect("localhost", "muziek", "muziek");
// om te connecteren zorg dat het paswoord voor deze gebruiker op de oude manier opgeslagen wordt in mysql
// volgend commando uitvoeren: set password for webwinkel@localhost=old_password('webwinkel')
if($connect && mysql_select_db("muziek"))
	return ($connect);
return (FALSE);
}

function import($file)
{
alert($file);
if(!isset($connect)){connect_db();}
mysql_query(file_get_contents($file));
}
?>
<html>
<head>
<title>update</title>
</head>
<body>
<center>
<form method="POST" name="updaten" action="update.php" enctype="multipart/form-data">
<input type="file" name="file2upl" size="36" onchange="alert(file2upl.value)"><br><br>
<input type="button" value="Import" onclick="import(file2upl.value)">
</form>
</center>
</body>
</html>

 

the problem i have for the moment is that when i click on the button the script isn't executed.

thank you for your reply but I think I have a other problem.

my pages have the following code:

update.html:
<html><head>
<title>agenda updaten</title>
</head>
<body>
<center>
<form method="POST" name="updaten" action="update.php">
<input type="file" name="file2upl" size="36"><br><br>
<input type="submit" value="Import">
</form>
</center>
</body></html>

update.php:
<?
function connect_db(){
GLOBAL $connect;
$connect=mysql_connect("localhost", "muziek", "muziek");
// om te connecteren zorg dat het paswoord voor deze gebruiker op de oude manier opgeslagen wordt in mysql
// volgend commando uitvoeren: set password for webwinkel@localhost=old_password('webwinkel')
if($connect && mysql_select_db("muziek"))
	return ($connect);
return (FALSE);
}
if(!isset($connect)){connect_db();}
mysql_query(file_get_contents($_POST['file2upl']));
if(mysql_query(file_get_contents($_POST['file2upl']))) 
{ 
echo "Success!!"; 
}
else 
{ 
echo "failure:<br/>".mysql_error(); 
}
?>

 

but i get the following error when i try to import the sql file:

failure:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; -- -- Gegevens worden uitgevoerd voor tabel `tuitstappen` -- INSERT ' at line 35

 

the sql file is this:

-- phpMyAdmin SQL Dump
-- version 2.9.0-rc1
-- http://www.phpmyadmin.net
-- 
-- Host: localhost
-- Generatie Tijd: 05 Sept 2007 om 11:02
-- Server versie: 5.0.24
-- PHP Versie: 5.1.6
-- 
-- Database: `muziek`
-- 

-- --------------------------------------------------------

-- 
-- Tabel structuur voor tabel `tuitstappen`
-- 

CREATE TABLE `tuitstappen` (
  `Uitstapnr` int(11) NOT NULL auto_increment,
  `Datum` date default NULL,
  `Secr` tinyint(4) default NULL,
  `Bestuur` tinyint(4) default NULL,
  `Iedereen` tinyint(4) default NULL,
  `Zaal` tinyint(4) default NULL,
  `Naam` varchar(50) default NULL,
  `Plaats` varchar(50) default NULL,
  `LokatieOptreden` varchar(50) default NULL,
  `Adres` varchar(50) default NULL,
  `Aanvangsuur` time default NULL,
  `UurSamenkomst` time default NULL,
  `Samenkomst` varchar(25) default NULL,
  `Opmerking` varchar(100) default NULL,
  PRIMARY KEY  (`Uitstapnr`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=282 ;

-- 
-- Gegevens worden uitgevoerd voor tabel `tuitstappen`
-- 

INSERT INTO `tuitstappen` (`Uitstapnr`, `Datum`, `Secr`, `Bestuur`, `Iedereen`, `Zaal`, `Naam`, `Plaats`, `LokatieOptreden`, `Adres`, `Aanvangsuur`, `UurSamenkomst`, `Samenkomst`, `Opmerking`) VALUES 
(1, '2001-09-03', 1, 1, 1, 0, 'Kermis uitstap', 'Rosmeer', 'Harmoniezaal', 'Kerkstraat 37', '19:00:00', '19:00:00', 'We komen samen om:', 'Geen'),
(2, '2001-09-29', 1, 1, 1, 0, 'K. Fanfare Sint Lambertus', 'Veldwezelt', 'Zaal "De Toekomst"', 'Kerkveld 29', '18:30:00', '17:30:00', 'We vertrekken om:', 'Geen'),
(3, '2001-09-08', 1, 1, 1, 0, 'Koninklijke Philharmonie', 'Bocholtz NL', 'Feesttent', 'Schoolstraat 47(adres maatschappij)', '17:30:00', '16:00:00', 'We vertrekken om:', 'Geen'),
(4, '2001-09-09', 1, 1, 1, 0, 'E&D(Mis Ziekenzorg)', 'Rosmeer', 'Harmoniezaal', 'Kerstraat 37', '11:00:00', '10:00:00', 'We komen samen om:', 'Geen'),
(5, '2001-09-09', 1, 1, 1, 0, 'K. Harmonie "Recht Door Zee"', 'Eigenbilzen', 'Zaal "Hartenberg"', 'Hartenberg', '17:00:00', '16:00:00', 'We vertrekken om:', 'Geen'),
(6, '2001-10-06', 1, 1, 1, 0, 'Herfstconcerten', 'Rosmeer', 'Harmoniezaal', 'Kerkstraat 37', '16:00:00', '15:00:00', 'Aanwezig zijn om:', 'Geen'),
(7, '2001-10-07', 1, 1, 1, 0, 'Herfstconcerten', 'Rosmeer', 'Harmoniezaal', 'Kerkstraat 37', '18:00:00', '17:30:00', 'Aanwezig zijn om:', 'Geen'),
(8, '2001-11-01', 1, 1, 1, 0, 'Allerheiligen', 'Rosmeer', 'Oud en nieuw kerkhof', 'Kerkstraat - Diepestraat', '14:00:00', '13:30:00', 'We komen samen om:', 'Geen'),
(9, '2001-11-11', 1, 1, 1, 0, 'Wapenstilstand', 'Rosmeer', 'Oud Kerkhof', 'Kerkstraat', '11:00:00', '10:30:00', 'We komen samen om:', 'Geen'),
[REMOVED CODE because always the same]
but this until:
(281, '2007-09-15', 1, 1, 1, 0, 'Herfstfestival', 'Herderen', 'Harmoniezaal', NULL, NULL, NULL, NULL, 'Geen');

the file should work because it is a export from phpmyadmin.

but an other sql file does create the same error.

the sql file is like this:

failure:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; INSERT INTO TUitstappen (Uitstapnr, Datum, Secr, Bestuur, Iedereen, Zaal, N' at line 9

 

the file looks like this:

# MdbToMySQL MySQL-Dump
# version 1.01
# http://www.zebradb.de
# --------------------------------------------------------

#
# Table data for table TUitstappen
#
TRUNCATE TABLE `tuitstappen`;

INSERT INTO TUitstappen (Uitstapnr, Datum, Secr, Bestuur, Iedereen, Zaal, Naam, Plaats, LokatieOptreden, Adres, Aanvangsuur, UurSamenkomst, Samenkomst, Opmerking) VALUES (1, '2001-09-03 00:00:00', 1, 1, 1, 0, 'Kermis uitstap', 'Rosmeer', 'Harmoniezaal', 'Kerkstraat 37', '1899-12-30 19:00:00', '1899-12-30 19:00:00', 'We komen samen om:', 'Geen');
INSERT INTO TUitstappen (Uitstapnr, Datum, Secr, Bestuur, Iedereen, Zaal, Naam, Plaats, LokatieOptreden, Adres, Aanvangsuur, UurSamenkomst, Samenkomst, Opmerking) VALUES (2, '2001-09-29 00:00:00', 1, 1, 1, 0, 'K. Fanfare Sint Lambertus', 'Veldwezelt', 'Zaal \"De Toekomst\"', 'Kerkveld 29', '1899-12-30 18:30:00', '1899-12-30 17:30:00', 'We vertrekken om:', 'Geen');
INSERT INTO TUitstappen (Uitstapnr, Datum, Secr, Bestuur, Iedereen, Zaal, Naam, Plaats, LokatieOptreden, Adres, Aanvangsuur, UurSamenkomst, Samenkomst, Opmerking) VALUES (3, '2001-09-08 00:00:00', 1, 1, 1, 0, 'Koninklijke Philharmonie', 'Bocholtz NL', 'Feesttent', 'Schoolstraat 47(adres maatschappij)', '1899-12-30 17:30:00', '1899-12-30 16:00:00', 'We vertrekken om:', 'Geen');
INSERT INTO TUitstappen (Uitstapnr, Datum, Secr, Bestuur, Iedereen, Zaal, Naam, Plaats, LokatieOptreden, Adres, Aanvangsuur, UurSamenkomst, Samenkomst, Opmerking) VALUES (4, '2001-09-09 00:00:00', 1, 1, 1, 0, 'E&D(Mis Ziekenzorg)', 'Rosmeer', 'Harmoniezaal', 'Kerstraat 37', '1899-12-30 11:00:00', '1899-12-30 10:00:00', 'We komen samen om:', 'Geen');
INSERT INTO TUitstappen (Uitstapnr, Datum, Secr, Bestuur, Iedereen, Zaal, Naam, Plaats, LokatieOptreden, Adres, Aanvangsuur, UurSamenkomst, Samenkomst, Opmerking) VALUES (5, '2001-09-09 00:00:00', 1, 1, 1, 0, 'K. Harmonie \"Recht Door Zee\"', 'Eigenbilzen', 'Zaal \"Hartenberg\"', 'Hartenberg', '1899-12-30 17:00:00', '1899-12-30 16:00:00', 'We vertrekken om:', 'Geen');
INSERT INTO TUitstappen (Uitstapnr, Datum, Secr, Bestuur, Iedereen, Zaal, Naam, Plaats, LokatieOptreden, Adres, Aanvangsuur, UurSamenkomst, Samenkomst, Opmerking) VALUES (6, '2001-10-06 00:00:00', 1, 1, 1, 0, 'Herfstconcerten', 'Rosmeer', 'Harmoniezaal', 'Kerkstraat 37', '1899-12-30 16:00:00', '1899-12-30 15:00:00', 'Aanwezig zijn om:', 'Geen');
INSERT INTO TUitstappen (Uitstapnr, Datum, Secr, Bestuur, Iedereen, Zaal, Naam, Plaats, LokatieOptreden, Adres, Aanvangsuur, UurSamenkomst, Samenkomst, Opmerking) VALUES (7, '2001-10-07 00:00:00', 1, 1, 1, 0, 'Herfstconcerten', 'Rosmeer', 'Harmoniezaal', 'Kerkstraat 37', '1899-12-30 18:00:00', '1899-12-30 17:30:00', 'Aanwezig zijn om:', 'Geen');
INSERT INTO TUitstappen (Uitstapnr, Datum, Secr, Bestuur, Iedereen, Zaal, Naam, Plaats, LokatieOptreden, Adres, Aanvangsuur, UurSamenkomst, Samenkomst, Opmerking) VALUES (8, '2001-11-01 00:00:00', 1, 1, 1, 0, 'Allerheiligen', 'Rosmeer', 'Oud en nieuw kerkhof', 'Kerkstraat - Diepestraat', '1899-12-30 14:00:00', '1899-12-30 13:30:00', 'We komen samen om:', 'Geen');
INSERT INTO TUitstappen (Uitstapnr, Datum, Secr, Bestuur, Iedereen, Zaal, Naam, Plaats, LokatieOptreden, Adres, Aanvangsuur, UurSamenkomst, Samenkomst, Opmerking) VALUES (9, '2001-11-11 00:00:00', 1, 1, 1, 0, 'Wapenstilstand', 'Rosmeer', 'Oud Kerkhof', 'Kerkstraat', '1899-12-30 11:00:00', '1899-12-30 10:30:00', 'We komen samen om:', 'Geen');
INSERT INTO TUitstappen (Uitstapnr, Datum, Secr, Bestuur, Iedereen, Zaal, Naam, Plaats, LokatieOptreden, Adres, Aanvangsuur, UurSamenkomst, Samenkomst, Opmerking) VALUES (11, '2001-12-01 00:00:00', 1, 1, 1, 0, 'K.Fanfare \"De Eendracht\"', 'Kl-Spouwen', 'Cultureel Centrum', NULL, '1899-12-30 21:00:00', '1899-12-30 20:00:00', 'We vertrekken om:', 'Geen');
INSERT INTO TUitstappen (Uitstapnr, Datum, Secr, Bestuur, Iedereen, Zaal, Naam, Plaats, LokatieOptreden, Adres, Aanvangsuur, UurSamenkomst, Samenkomst, Opmerking) VALUES (12, '2001-11-04 00:00:00', 1, 1, 1, 0, 'K. Harmonie \"St.Catharina\"', 'Kortessem', 'Cultureel Centrum Mozaïek', 'Kerkplein 14', '1899-12-30 18:00:00', '1899-12-30 16:30:00', 'We vertrekken om:', 'Geen');
INSERT INTO TUitstappen (Uitstapnr, Datum, Secr, Bestuur, Iedereen, Zaal, Naam, Plaats, LokatieOptreden, Adres, Aanvangsuur, UurSamenkomst, Samenkomst, Opmerking) VALUES (13, '2002-02-08 00:00:00', 1, 1, 1, 0, 'Jaarlijkse ledenvergadering', 'Rosmeer', 'Harmoniezaal', 'Kerkstraat 37', '1899-12-30 20:00:00', NULL, NULL, 'Geen');
INSERT INTO TUitstappen (Uitstapnr, Datum, Secr, Bestuur, Iedereen, Zaal, Naam, Plaats, LokatieOptreden, Adres, Aanvangsuur, UurSamenkomst, Samenkomst, Opmerking) VALUES (14, '2002-02-17 00:00:00', 1, 1, 1, 0, 'Restaurantdag', 'Rosmeer', 'Harmoniezaal', 'Kerkstraat 37', '1899-12-30 11:30:00', NULL, NULL, 'Geen');
INSERT INTO TUitstappen (Uitstapnr, Datum, Secr, Bestuur, Iedereen, Zaal, Naam, Plaats, LokatieOptreden, Adres, Aanvangsuur, UurSamenkomst, Samenkomst, Opmerking) VALUES (15, '2002-04-08 00:00:00', 1, 1, 1, 0, 'Jeugdkamp te Voeren 8 tot 11 april \'02', 'Voeren', 'Jeugdherberg', NULL, NULL, NULL, NULL, 'Geen');
INSERT INTO TUitstappen (Uitstapnr, Datum, Secr, Bestuur, Iedereen, Zaal, Naam, Plaats, LokatieOptreden, Adres, Aanvangsuur, UurSamenkomst, Samenkomst, Opmerking) VALUES (16, '2002-04-20 00:00:00', 1, 0, 1, 0, 'Lenteconcerten', 'Rosmeer', 'Harmoniezaal', 'Kerkstraat 37', '1899-12-30 15:30:00', '1899-12-30 14:30:00', 'Aanwezig zijn om:', 'Geen');
INSERT INTO TUitstappen (Uitstapnr, Datum, Secr, Bestuur, Iedereen, Zaal, Naam, Plaats, LokatieOptreden, Adres, Aanvangsuur, UurSamenkomst, Samenkomst, Opmerking) VALUES (17, '2002-05-06 00:00:00', 1, 1, 1, 0, 'Kermisuitstap', 'Rosmeer', 'Harmoniezaal', 'Kerkstraat 37', '1899-12-30 19:00:00', '1899-12-30 19:00:00', 'We komen samen om:', 'Geen');
INSERT INTO TUitstappen (Uitstapnr, Datum, Secr, Bestuur, Iedereen, Zaal, Naam, Plaats, LokatieOptreden, Adres, Aanvangsuur, UurSamenkomst, Samenkomst, Opmerking) VALUES (18, '2002-06-23 00:00:00', 1, 1, 1, 0, 'Intercity', 'Rosmeer', NULL, NULL, '1899-12-30 15:00:00', '1899-12-30 15:00:00', 'We komen samen om:', 'Geen');
INSERT INTO TUitstappen (Uitstapnr, Datum, Secr, Bestuur, Iedereen, Zaal, Naam, Plaats, LokatieOptreden, Adres, Aanvangsuur, UurSamenkomst, Samenkomst, Opmerking) VALUES (19, '2002-06-30 00:00:00', 1, 1, 1, 0, 'Harmonie Waterschei Zwartberg', 'Waterschei', 'Park aan rond punt (parkeren aan de kerk)', 'André Dumontlaan', '1899-12-30 17:15:00', '1899-12-30 15:45:00', 'We vertrekken om:', 'Geen');
INSERT INTO TUitstappen (Uitstapnr, Datum, Secr, Bestuur, Iedereen, Zaal, Naam, Plaats, LokatieOptreden, Adres, Aanvangsuur, UurSamenkomst, Samenkomst, Opmerking) VALUES (20, '2002-07-07 00:00:00', 1, 1, 1, 0, 'Stepkesrace-CongéROCK  7 juli \'02', 'Rosmeer', 'Harmoniezaal', 'Kerkstraat 37', NULL, NULL, NULL, 'Geen');
.........

this file works when I use the import function from phpmyadmin.

what can be the problem.

 

I'll get this error when I use the following script:

Warning: mysqli::mysqli() [function.mysqli-mysqli]: (HY000/2003): Can't connect to MySQL server on 'localhost' (10061) in I:\PortableOffice\Webserver\Root\dbtest\pages\update.php on line 23

Warning: main() [function.main]: Couldn't fetch mysqli in I:\PortableOffice\Webserver\Root\dbtest\pages\update.php on line 26

Notice: Fout bij verbinding: in I:\PortableOffice\Webserver\Root\dbtest\pages\update.php on line 26

Warning: mysqli::multi_query() [function.mysqli-multi-query]: Couldn't fetch mysqli in I:\PortableOffice\Webserver\Root\dbtest\pages\update.php on line 31

Notice: Undefined variable: mysqli in I:\PortableOffice\Webserver\Root\dbtest\pages\update.php on line 33

Notice: Trying to get property of non-object in I:\PortableOffice\Webserver\Root\dbtest\pages\update.php on line 33

Notice: Fout in query: in I:\PortableOffice\Webserver\Root\dbtest\pages\update.php on line 33

 

$connect = new mysqli('localhost', 'muziek', 'muziek', 'muziek');
if(mysqli_connect_errno())
{
    trigger_error('Fout bij verbinding: '.$connect->error);
}

$sql = file_get_contents($_POST['file2upl']);

if(!$connect->multi_query($sql))
{
    trigger_error('Fout in query: '.$mysqli->error);
}
else
{
    do
    {
        if($connect->affected_rows > 0)
        {
            echo 'Query gelukt <br>';
        }
    }
    while($connect->next_result());
}    
?> 

 

but with this script I don't get the connection error.

function connect_db(){
GLOBAL $connect;
$connect=mysql_connect("localhost", "muziek", "muziek");
if($connect && mysql_select_db("muziek"))
	return ($connect);
return (FALSE);
}

I have got it working.

I used the following code:

HTML file:
<form method="POST" name="updaten" action="update.php">
<input type="file" name="file2upl" size="36"><br><br>
<input type="submit" value="Import">
</form>


PHP file:
<?php
$connect = mysqli_connect('localhost', 'muziek', 'muziek', 'muziek',"3307");
if(mysqli_connect_errno())
{
    trigger_error('Fout bij verbinding: '.$connect->error);
}
if ($connect->query("TRUNCATE TABLE TUitstappen") === TRUE) {
    printf("Table successfully TRUNCATED.<br>\n");
}
$sql = file_get_contents($_POST['file2upl']);
if (mysqli_multi_query($connect, $sql)) 
{
    do {
        /* store first result set */
        if ($result = mysqli_store_result($connect)) 
	{
            while ($row = mysqli_fetch_row($result)) 
		{ 
		    printf("%s\n", $row[0]);
            }
            mysqli_free_result($result);
        }
        /* print divider */
	if (mysqli_more_results($connect)) 
	{
            printf("--Data-Inster-Successfull--");
        }
    } while (mysqli_next_result($connect));
}
/* close connection */
mysqli_close($connect);
?> 

 

thank you for pointing me in the correct direction.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.