Re: postgresql csv import

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On 21/09/2021 00:23, ourdiaspora wrote:
On Monday, September 20th, 2021 at 11:19 PM, Ashley Sheridan <ash@xxxxxxxxxxxxxxxxxxxx> wrote:

On 20 September 2021 22:49:25 BST, ourdiaspora ourdiaspora@xxxxxxxxxxxxxx wrote:
$targetfilepath = $uploaddir . $uploadfile;
	if(isset($_POST["submit"])){
		$filename=$_FILES["userfile"]["tmp_name"];
		if($_FILES["file"]["size"] > 0){
			$file = fopen($filename, "r");
			while (($getdata = fgetcsv($file, 10000, ",")) !== FALSE){
				$dbdataentry = "INSERT INTO somedata(name, id, emailaddress, groupname) VALUES ('".$getdata[0]."','".$getdata[1]."','".$getdata[2]."','".$getdata[3]."')";
				$result = pg_query($dbconnect, $dbdataentry);
				}
			}
			fclose($file);
		 }
So what exactly is your code doing? Check each part:
Did you attempt to use any kind of simple print statement debugging to see if any of your assumptions are correct?
Changed the file to:

"
...
$targetfilepath = $uploaddir . $uploadfile;
$filename=$_FILES["userfile"]["tmp_name"];
	if(isset($_POST["submit"])){
		$filename=$_FILES["userfile"]["tmp_name"];
		if($_FILES["file"]["size"] > 0){
			$file = fopen($filename, "r");
			while (($getdata = fgetcsv($file, 10000, ",")) !== FALSE){
				$dbdataentry = "INSERT INTO somedata(name, id, emailaddress, groupname) VALUES ('".$getdata[0]."','".$getdata[1]."','".$getdata[2]."','".$getdata[3]."')";
				$result = pg_query($dbconnect, $dbdataentry);
				}
			}
			fclose($file);
		 }
		 print "filename is $targetfilepath";
...
"

The file path was correctly printed in the web browser.

Then substituted the last line above to:

"
...
print "filename is $filename";
...
"

The filename was _not_ printed in the web browser.

Then substituted for:

"
...
print "filename is $_FILES";
...
"

Result printed in web browser:

"
...
Notice: Array to string conversion in
...filename is Array
...
"

Then substituted for:

"
...
print "filename is $file";
...
"

Result printed in web browser:

"
...
Notice: Undefined variable: file in
...
"

Then substituted for:

"
...
print "filename is $getdata";
...
"

Result printed in web browser:

"
...
Notice: Undefined variable: getdata in
...
"

So it would seem that these variables are not being accepted?

You didn't need to continue after the first failure, the rest of your code won't just magically understand what you wanted it to do and fill in the gaps.

$filename=$_FILES["userfile"]["tmp_name"];

That one line is not working as expected, which means that the rest of your code will fail. Now you need to ask yourself, why is that failing.

What is sending the file to your PHP script in the first place? Can you show a simplified form which *only* contains the parts necessary to show the failure?

--
Ashley Sheridan
https://www.ashleysheridan.co.uk



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux