I am trying to read from a file and insert this to a database. But the error message is giving me trouble
Notice: Undefined offset: 1 in C:\xampp\htdocs\test\try2.php on line 15. This is the error message.
On line 15 is: $userName = $tmp[1];
my code is
<?php
mysql_connect("localhost", "root", "")or die("cannot connect");
mysql_select_db("test")or die("cannot select DB");
$file = "test.txt";
$fp = fopen($file, "r");
$data = fread($fp, filesize($file));
fclose($fp);
$delimiter = ".";
$output = explode($delimiter, $data);
foreach($output as $var) {
$tmp = explode("|", $var);
$userId = $tmp[0];
$userName = $tmp[1];
$sql = "INSERT INTO sample SET userId='$userId', userName='$userName'";
mysql_query($sql);
}
?>