php undefined offset
hi... i'm getting an undefined offset for the associative arrays [28-46] in this format.
PHP Code:
i have read that i can prevent the notices by doing the following, but it's not working for me PHP Code:
|
You could do like that:
$data = fgetcsv($handle, 1000, ","); foreach ($data as &$d) { if (empty($d)) { $d = 'default value'; } } Then INSERT... |
Quote:
Judging by what it appears you are trying to do, something like this might work better: PHP Code:
|
i know this is a stupid question... but the name.$i... will i require to make an array for this area to place all of the different field names from the csv file into it?
|
Quote:
That said though, I'm not completely sure that I understand your question. Are you asking if you'll need to create an array for the "name$i" line? If so, no, you do not. You are using the same $data array to create both parts of the query. Actually, you can probably use implode() instead of a loop in both places.... something like: PHP Code:
PHP Code:
|
As far as I understand that you have a table with 46 columns and want to read in a csv file completely?
// foreach ($data as $i => $item) { $i is 0 at the start // $sql .= "name$i"; so the column names are not matched ?? while ($data = fgetcsv($handle, 1000, ",")) { //kick out empty foreach ($data as &$d) { if (empty($d)) { $d = 'default value'; } } //create sql $sql = "insert into tableName ("; for ($i = 1; $i <47 ; $i++) { $sql .= "name$i"; if ($i < 46) { $sql .= ", "; } } $sql .= " ) "; $sql .= "values ('" . implode("', '", $data) . "')"; $sqlData = mysql_query($sql); } |
All times are GMT -5. The time now is 09:21 AM. |