Forum Moderators: coopster
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>
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. ;)
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
$stepvariable 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;
//}
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?
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.