Forum Moderators: coopster

Message Too Old, No Replies

troubles importing a csv file with a php script

         

suicide

7:03 pm on May 14, 2009 (gmt 0)

10+ Year Member



I'm having trouble executing this script. I have a file named 1033.csv located in the /serials/ folder
I don't know if I have the script written wrong but can someone look at it and see if I'm missing anything and suggest on how to fix it?
Thanks

1033.csv contents:
------------------

1033,S1234,FA
1033,S1235,FA

Import.php contents:
------------------------


<?php
include(utsi_config.php);

$uinput=$_GET['uinput'];
$path="./serials";
$ext='csv';
$filename=($path.'/'.$uinput.'.'.$ext);

if (file_exists($filename))
{
function AddMerchant($nid,$serial,$location)
{
mysql_connect("$db_server","$username","$password")or die("cannot connect");
mysql_select_db("$database")or die("cannot select DB");
$sql="INSERT INTO serial (nid, serial, location) VALUES ('$nid', '$serial', '$location')");
mysql_query($sql) or die("cannot query");
}

$file=file_get_contents($filename);
$rows=explode("\n", $file);

foreach($rows as $row)
{
$items=explode(",", $row);
AddMerchant($items[0],$items[1]),$items[2]);
}
}
else
{
echo "fix it!";

?>

dreamcatcher

8:34 pm on May 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi suicide, welcome to WebmasterWorld. :)

One thing I notice immediately. You are using your connection vars inside a function, but you are not declaring them as global.

function AddMerchant($nid,$serial,$location)
{
global $db_server,$username,$password,$database;
mysql_connect("$db_server","$username","$password")or die("cannot connect");
mysql_select_db("$database")or die("cannot select DB");
$sql="INSERT INTO serial (nid, serial, location) VALUES ('$nid', '$serial', '$location')");
mysql_query($sql) or die("cannot query");
}

Variables declared outside of a function are not available inside the function by default.

dc

suicide

9:26 pm on May 14, 2009 (gmt 0)

10+ Year Member



Ok I added the statment and it's still not working.
It doesnt import to the database :(
Here is the 2 php files from start to end:

step1.php
----------


<form action="import.php" method="get">
Enter the Filename<input type="text" name="uinput" />
<input type="submit" value="Submit" />
</form>

import.php (modified with the suggestion)
-----------

<?php
include(utsi_config.php);

$uinput=$_GET['uinput'];
$path="./serials";
$ext='csv';
$filename=($path.'/'.$uinput.'.'.$ext);

if (file_exists($filename))
{
function AddMerchant($nid,$serial,$location)
{
global $db_server,$username,$password,$database;
mysql_connect("$db_server","$username","$password")or die("cannot connect");
mysql_select_db("$database")or die("cannot select DB");
$sql="INSERT INTO serial (nid, serial, location) VALUES ('$nid', '$serial', '$location')");
mysql_query($sql) or die("cannot query");
}

$file=file_get_contents($filename);
$rows=explode("\n", $file);

foreach($rows as $row)
{
$items=explode(",", $row);
AddMerchant($items[0],$items[1]),$items[2]);
}
}
else
{
?>
fix it
<?php
}
?>

dreamcatcher

7:40 am on May 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This line:

$sql="INSERT INTO serial (nid, serial, location) VALUES ('$nid', '$serial', '$location')");

Change to:

$sql="INSERT INTO serial (nid, serial, location) VALUES ('$nid', '$serial', '$location')";

dc

suicide

1:07 pm on May 15, 2009 (gmt 0)

10+ Year Member



Thanks, DC for spotting that. After staring at the code for so long, I must of missed that, but with that fixed. It will not import into the database. I'm not getting any types of error. Come to think of it, It doesn't seems that it does this part at all. With that added statement I don't get that echo printed onto the screen. So I believe I'm doing something wrong in that area. :(


foreach($rows as $row)
{
$items=explode(",", $row);
AddMerchant($items[0],$items[1]),$items[2]);
echo "Complete"; <---- I added this
}

Also I notice that i forgot to put this in quotes

include('utsi_config.php');

penders

1:40 pm on May 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



AddMerchant($items[0],$items[1]),$items[2]);

(Just jumping into this thread..) You have an extra closing bracket/parenthesis inside your function call.

I would have thought this would have produced some kind of PHP parse error? Did you get anything output to the browser? If not then check your server error logs.

Make sure you have full error reporting on, by placing this at the start of your script:

error_reporting(E_ALL); 
ini_set('display_errors','On');

suicide

2:56 pm on May 15, 2009 (gmt 0)

10+ Year Member



Awesome I thank everyone with the help and my second pair of eyes :)

I knew i wasn't going crazy.

Thanks again that fix my problem

Carlos

dreamcatcher

5:51 am on May 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got sorted.

dc