hello everyone

i m inserting data from text file into database. .......but the data into the text file is not inserted into database only blank record display in the database..........
my text file is like this:
james flower 22

may be i m not giving space delimiter correct... plz help me out

<html>
<?

$con=mysql_connect("localhost","root","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db('test',$con);
//(1) Read the text file into a variable
$file = "testFile.txt";
$fp = fopen($file, "r");
$data = fread($fp, filesize($file));
fclose($fp);


//(2) Then we can get rid of the tabs in there:

$output = str_replace("\t", $data);


//(3) Then we explode it at every line break

$output = explode("\n", $output);


//(4) Then we loop through all the array elements and explode them again and insert them into mysql

foreach($output as $var) {
$tmp = explode("|", $var);
$FirstName = $tmp[0];
$LastName = $tmp[1];
$Age = $tmp[2];
$sql = "INSERT INTO student SET FirstName='$FirstName', LastName='$LastName',Age='$Age'";
mysql_query($sql);
 echo "Done!";
}
mysql_close($con);
?> </html>

You didn't provide the test file, so I think the code may be failing due to what is or is not in the test file.
You can insert some var_dump() 's to see if the expected arrays are being returned.

Or you could correct this:

$tmp = explode("|", $var);

You said that your file contained the information like

james flower 22

...so that means that you explode using space, not pipe.

$tmp = explode(" ", $var);

My 2ยข. Hope this helps.

hi wraithmanilian
thankz 4 ur help..it works........

hello dr..........
i m trying to store a text file into database below format of text file
i hv text file like this:
0100102001110271 0409141151I1476201
0100102001110125 0409141351O1476301
0100101300000030 0409141630O1476401
0100102001110271 0409141631O1476501
0100102001110126 0409141632O1476601

how can store it into my database table att0310
my table fileds:
cardno date time IO(in-out) and serial no
0100102001110271 this is card no
040914 for date
1151 for time
I for IO
1476201 for serial no
plz help me out

using previous code till date data is inserted correctly into database but next data not stored into database. because of there is no space b/w those digits .... hw can i store that into database.... i tried so many times........... plz help me out.........

preetg,

You can do what you are looking to do using substr:

foreach($output as $var) {
	$tmp = explode(" ", $var);
	$cardnumber = $tmp[0];
	$cluster = $tmp[1];

	$thedate = substr($tmp[1],0,6);
	$thetime = substr($tmp[1],6,4);
	$io = substr($tmp[1],10,1);
	$serial = substr($tmp[1],11);

	$sql = "INSERT INTO student SET FirstName='$FirstName', LastName='$LastName',Age='$Age'";
	mysql_query($sql);
	echo "Done!";
}

This is the foreach statement from your previous code modified. You can look up the complete usage for substr here:

http://us2.php.net/manual/en/function.substr.php

thankz alot for ur help........it works..........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.