Forum Moderators: coopster
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!";
?>
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
step1.php
----------
<form action="import.php" method="get">
Enter the Filename<input type="text" name="uinput" />
<input type="submit" value="Submit" />
</form>
<?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
}
?>
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');
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');