0

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);
}
?>
2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by xxreenaxx1
0

This simply means that the $var you're trying to split doesn't contain any | at all, therefore the resulting array ($tmp) is only 1 element long and $tmp[1] doesn't exist.

0

I can input the userid. But I cant insert the username.

my text file contains something like
1 Mike
2 Lisa
3 Holy

0

If your file looks like that, why are you trying to split on "|" which you know isn't in the file at all?

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

Should be what you want.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.