Forum Moderators: coopster

Message Too Old, No Replies

Import CSV to mySQL via PHP

A simple and useful script that should work but doesn't. :\

         

Dimac Grandmaster

2:40 am on Jul 15, 2004 (gmt 0)

10+ Year Member



I've written a simple script that should import a CSV file using mySQL's LOAD DATA INFILE command and then write it to the db. I know phpMyAdmin can already perform this function but I want it in my own pagess.

This would be a HUGELY useful script to anyone who needs to import data from different dbs and apps (like Access, Excel...)into your dynamic site. If only I could get it working...

I know I'm missing something very obvious here but I can't get the script to go to step 2. There are no errors and no data gets written. Any help appreciated.
--

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<? include "../datasource.inc";?>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>CSV Loader</title>
</head>
<body>
<p><font size="3" face="Verdana, Arial"><strong><font color="#663366">CSV Loader</font></strong></font></p>
<p><font size="2" face="Verdana, Arial"><strong>INSTRUCTIONS</strong><br />
1. Just drop your CSV file in the same folder as this file.<br />
2. Change
the query here to match the file and the column names. <br />
3. Check the box below and click Submit.</font></p>
<?

if(!isset($step) ¦¦ empty($step)){
$step=1;
}

if($step==1){

?>
<form method="post" action="test_write.php?step=2" enctype="multipart/form-data">
<input type="checkbox" name="do_it" value="yes" />
<font size="1" face="Verdana, Arial">So you want to see if it works?</font><BR /><BR />
<input type="hidden" name="step" value="2">
<input name="Run query" type="submit" value="submit" />
</form>
<?
}

if($step==2){

$query = 'LOAD DATA INFILE \'abstract_test.csv\' INTO TABLE `abstract_test` '
. ' FIELDS TERMINATED BY \',\''
. ' OPTIONALLY ENCLOSED BY \'"\''
. ' ESCAPED BY \'\\\\\''
. ' LINES TERMINATED BY \'\\r\\n\'( `title` ,'
. ' `author` ,'
. ' `primary_author` ,'
. ' `bodytext` ,'
. ' `theme` ,'
. ' `institutes` ,'
. ' `focus` ,'
. ' `level` ,'
. ' `session_cat` ,'
. ' `abstractday` ,'
. ' `abstracttime` ,'
. ' `conf_year` ,'
. ' `activity` )';

$result = mysql_query($query);

if (!$result){
echo ("Query failed!");
}
else
{

while($tests=mysql_affected_rows($result)){
?>
<form>
<input type="hidden" name="step" value="<?php echo $step?>">
<?
if ($do_it=="yes"){
echo "<font face='verdana' size='2'><b>".$tests[0]."</b> <font color='blue'>has been added.</font></font><br>\n";
}
else{
echo "<font face='verdana' size='2'><b>".$tests[0]."</b> <font color='red'>has NOT been added.</font></font><br>\n";
}
}
}
}
?>
</form>
</body>
</html>

Timotheos

4:37 am on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm wondering if you echo your query if it looks like what you'd expect. I'm having a hard time between all the ' and ".

Dimac Grandmaster

4:49 am on Jul 15, 2004 (gmt 0)

10+ Year Member



The query was a direct PHP export from phpMyAdmin so I trust it implicitly. The query doesn't seem to be the problem. However, getting to step 2 in order to run the query is.

I'm thinking I might make the form post to a new page and stick the query on that but I was tryng to be neat about it...and possibly too clever by half. ;)

Timotheos

5:50 am on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry I read the problem wrong. No this is a good way to do it but don't you want to be using the $_POST['step'] variable? (I'm assuming register_globals is off).

Dimac Grandmaster

6:16 am on Jul 15, 2004 (gmt 0)

10+ Year Member



Yeah, I've tried several ways. As you can see I've even gone so far as to force step=2 by use of -

action="test_write.php?step=2"

- but still no go. I hate it when something terribly simple gets in the way like this. Argh! [pulling hair] :)

coopster

12:33 pm on Jul 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, Dimac Grandmaster!

I think Timotheos is spot on with the register globals [php.net] thought process. However, I'll bet the configuration directive is probably set to on but error reporting [php.net] isn't set high enough to allow us to see the issue. Therefore it looks like you need to initialize the

$step
variable otherwise your first edit check will always set it to 1. Try this ternary operation instead of your existing logic...
$step = (isset($_POST['step'])) ? $_POST['step'] : 1; 
//if(!isset($step) ¦¦ empty($step)){
//$step=1;
//}

Dimac Grandmaster

1:59 am on Jul 16, 2004 (gmt 0)

10+ Year Member



Well, that got me a little further. Based on my check to see if the query was successful:


if (!$result){
echo ("Query failed!");
}

I'm now getting 'Query failed' printed to screen below the first form on submit.

Thanks Coop. We're getting there. I'll keep working on it.

NB: Has anyone else noticed that the UBB code tag DOESN'T in fact keep the PRE formatting for pasted code?

Dimac Grandmaster

7:05 am on Jul 16, 2004 (gmt 0)

10+ Year Member



Ahh...

Some more light is shed on the problem. I added -

$result = mysql_query($query) or die("Error executing $query<br />MySQL reported: ".mysql_error());

- in order to get more verbose error reporting and that's just what I got.

MySQL reported:
File '.\ashmwebsitedb\abstract_test.csv' not found (Errcode: 2)

So now I know what the error is. A little more investigation gave me what the problem was as well.

For security reasons, when reading text files located on the server, the files must either reside in the database directory or be readable by all. Also, to use LOAD DATA INFILE on server files, you must have the FILE privilege.

I'll fix this and let you know the results.