Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

[PHP] File Read/Upload Arrays to DB


ghost's Avatar
0 0

Hi, basically I've got a script which reads data from a .txt file and saves it into an array and uploads the array of data to a database.

My problem is in the text file I have data like this:

hello/I/am/going/to/be/put/into/an/array

and that saves to an array fine, but as soon as I have another set of data to be read and saved into an array it only saves the first bit of data and stops before it reads the second bit:

hello/I/am/going/to/be/put/into/an/array

[id] => hello
[null_one] =>  I
[null_two] => am
[null_three] => going
[null_four] => to
[null_five] => be
[null_six] => put
[null_seven] => into
[testField] => an
[testField2] => array

If you can understand what I mean, can anyone please suggest an answer Please.

Thank you.


ghost's Avatar
0 0

<?php

if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  echo "<br /><br /><br />";
  }


  $file = fopen($_FILES["file"]["tmp_name"], "r");

  $readFile=fread($file,$_FILES["file"]["size"]);
  $sortData = list(
			$id,
			$null_one,
			$null_two,
			$null_three,
			$name,
			$Number,
			$pMonth,
			$Year,
			$cv,
			$address,
			$town,
			$town,
			$state,
			$country,
			$zipCode,
			$email
			) 
   = explode("/", $readFile);


  $sortData_assoc_array=array(
				'id' => $id, 
				'null_one' => $null_one,
				'null_two' => $null_two,
				'null_three' => $null_three, 
				'name' => $name,
				'Number' => $Number,
				'Month' => $Month,
				'Year' => $Year,
				'CV' => $cv,
				'address' => $address,
				'town' => $town,
				'state' => $state,
				'country' => $country,
				'zipCode' => $zipCode,
				'email' => $email
			      );

    echo "<pre>".print_r($sortData_assoc_array,true)."</pre>";
    echo "<br /><br /><br />";


/*
Uploading the Array to a database
*/


$query = sprintf("
		 INSERT INTO
		 `byte_database`.`orders`
		 (
			`id`,
			`null_one`,
			`null_two`,
			`null_three`,
			`name`,
			`Number`,
			`expMonth`,
			`Year`,
			`CV`,
			`address`,
			`town`,
			`state`,
			`country`,
			`zipCode`,
			`email`
		)
		VALUES
		(
			 '%s',
			 '%s',
			 '%s',
			 '%s',
			 '%s',
			 '%s',
			 '%s',
			 '%s',
			 '%s',
			 '%s',
			 '%s',
			 '%s',
			 '%s',
			 '%s',
			 '%s'
		);",
			$id,
			$null_one,
			$null_two,
			$null_three,
			$name,
			$Number,
			$Month,
			$Year,
			$cv,
			$address,
			$town,
			$state,
			$country,
			$zipCode,
			$email
		    );

mysql_query($query) or die(mysql_error());


?>```


There was a database connector in there but i took it out obviously for security :P

Also, I know this doesn't apply to coding standards in most places, and Its probably not the most efficient way of doing it, but it works ^^


ghost's Avatar
0 0

Yeah I thought that would be a big problem posting my problem here, I wasn't sure how to phrase it. Okay, well thanks for trying anyway.

Umm, I'll have a think on how to rephrase it better.

And sorry, I thought you meant just post it here:)


spyware's Avatar
Banned
0 0

serialze() unserialize()