CSV Import using PHP - Undefined Offset using explode - php

I have a script which imports data from csv and creates variables from the data which are imported into sql tables. It all works fine but the error log goes crazy with Undefined Offset errors.
From what I have read it is because it only finds index [0] so any others throw up the error.
I've looked at the following and can't seem to get the suggestions to work:
PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"
PHP Notice: Undefined offset error when importing from .csv
and many more...
Here is the code:
$filename='XXXXX_'.$today.'.csv';
$url=$proto.$user.':'.$pass.'#'.$server.$path.$filename;
$csv_data=file_get_contents($url);
foreach(preg_split("/((\r?\n)|(\r\n?))/", $csv_data) as $line){
list($type, $id, $ref) = explode(',', $line);
The error is generated on the 'list' and 'explode' line of code.
Apologies, quite new to this, have read multiple articles on the 'why' just not found a fix, it is the only error type i have populating the errorlog. Any help would be much appreciated.

you explode must return least 3 output , if in the line exist less than 3 item , error will be show. For this case you can use:
$lineData = explode(','$line);
$type = isset($lineData[0]) ? $lineData[0] : null;
$id = isset($lineData[1]) ? $lineData[1] : null;
$ref = isset($lineData[2]) ? $lineData[2] : null;
Or use:
#list($type, $id, $ref) = explode(',', $line);

Related

Undefined index: client_id" on line 1001 [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 2 years ago.
any help you offer on this will be useful. Am working on my school project and this error code keeps popping up on the application "Undefined index: client_id" on line 1001"
Here is the code
public function onNewclientaddress(){
$addShipmentForm = Settings::get('addShipmentForm',true);
$data = post();
= \Spot\Shipment\Models\Address::where('user_id', $data['client_id'])->update(['default' => 0]);
if ( $addShipmentForm == "add_form_normal"){
$subitem = new \Spot\Shipment\Models\Address;
$subitem->name = htmlspecialchars($data['street_addr']);
$subitem->user_id = htmlspecialchars($data['client_id']);
$subitem->street = htmlspecialchars($data['street_addr']);
$subitem->city = htmlspecialchars($data['city_id']);
$subitem->zipcode = htmlspecialchars($data['postal_code']);
$subitem->country = htmlspecialchars($data['country_id']);
$subitem->default = 1;
$subitem->created_at = \Carbon\Carbon::now();
$subitem->updated_at = \Carbon\Carbon::now();
$subitem->save();
}
else{
line 1001 is the first line of code in the post. please pardon my English
This error indicates that $data['client_id'] is not being set. You will need to ensure that whatever form is providing this data, is passing client_id correctly.
You can see what is currently in $data with a line like:
die(var_dump($data));
This will output the data on the page.
Ensure your client_id field in your form has a name attribute:
name="client_id"

PHP Notice: Undefined offset error when importing from .csv [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
I'm continually getting the above error when importing data from csv using the following code:
$csv_data=file_get_contents("data3_received.csv");
foreach(preg_split("/((\r?\n)|(\r\n?))/", $csv_data) as $line){
list($service_id, $ki3) = explode(',', $line,2);
The data imports as expected but the error log is filling up as we utilise the same code in multiple php scripts. The error is on the following line:
list($service_id, $ki3) = explode(',', $line,2);
Tried using the suggestion here: Undefined offset error on php when importing a CSV to no avail, and other on the site.
Any help with this would be most welcome.
Since list() is attempting to assign two variables it is attempting to access two array elements [0] and [1]. Because there is no comma on the line, [1] does not exist.
Try some different functions:
$csv_data = file("data3_received.csv", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($csv_data as $line) {
$data[] = str_getcsv($line);
}
To get individual variables you need to check if you have more than one column, or something similar:
if(count($data = str_getcsv($line)) > 1) {
list($service_id, $ki3) = $data;
} else {
$service_id = $data[0];
}

PHP Notice: Undefined offset: 1 [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I have made an sms handler system, and everything is okey, working fine.
But I get this error: PHP Notice: Undefined offset: 1
Its in the 17. line $user is the 17. line, I know its just notice, but daily 20-30 "notice" is in my php error log, and I wanted to fix this.
I tryed many different method, but no changes.
Somebody can help to fix it? Thanks!
$conn = sqlsrv_connect($serverName, $connectionInfo);
$id = $_GET["id"];
$from = $_GET["from"];
$to = $_GET["to"];
$msg = explode(" ", $_GET['message']);
$user = substr(trim($msg[1]),0,10);
Viewing this code helps less to understand but still i would recommend you to place
if(isset($msg[1]) && $msg[1] != ''){
$user = substr(trim($msg[1]),0,10);
} else {
$user = '';
}
because it looks like in some cases $msg[1] does not exist. For example if $_GET['message'] = 'Hello';
Well, $_GET['message'] does not seem to contain a second element. Are you sure its set?
Your code should handle this nicely by having an if or sthg similar. Examples:
$user = "anonymous";
if (sizeof($msg)) > 1) {
$user = substr(trim($msg[1]),0,10);
}

Notice: Undefined offset: 1 in E:\xampp\htdocs\WPTC-PMS5-5-14\edit_column.php on line 12 [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
when the below code works I get the above mentioned error , what should i do ? please help me ...
<?php
//session_start();
include("dbconnect_database.php");
$tname=$_GET['tn'];
$cname=$_GET['cn'];
$des=mysql_query("desc `$tname` `$cname`");
$row=mysql_fetch_array($des);
list($type, $b) = explode('[(]',$row[1]);
list($size) = explode('[)]',$b);
?>
Try changing this:
list($type, $b) = explode('[(]',$row[1]);
to this:
list($type, $b) = explode('[(]',$row[0]);
UPDATE
The error is telling you that 1 is not a valid index for $row, so that is the problem. Just before that line, try var_dump($row). This will tell what the valid indexes for $row are, and you should be able to use that to fix your code.
there is no ' around table and column name
$des=mysql_query("desc $tname $cname");
sidenote: use mysqli_query instead of mysql because use of mysql is been deprecated

Notice: Undefined variable: ids in [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 9 years ago.
i have a script that works fine for years and suddenly i face this problem :
please check the code and mend any solutionplease. any kind of help willbe appreciated.
Notice: Undefined variable: ids in /home/content/a/t/a/ataasaid1/html/category.php on line 4
Notice: Undefined variable: id in /home/content/a/t/a/ataasaid1/html/category.php on line 5
Notice: Use of undefined constant image - assumed 'image' in /home/content/a/t/a/ataasaid1/html/category.php on line 11
Notice: Use of undefined constant video - assumed 'video' in /home/content/a/t/a/ataasaid1/html/category.php on line 15
Notice: Use of undefined constant sound - assumed 'sound' in /home/content/a/t/a/ataasaid1/html/category.php on line 19
the code in file category.php is :
<?
error_reporting(E_ALL | E_STRICT);
ini_set("display_errors", 1);
$id1=$ids;
$id2=$id;
include('config.php');
$result= mysql_query("SELECT * FROM products where id like '$id1' ;");
$row=mysql_fetch_array ($result);
$type=$row['type'];
if($type==image)
{
include('categories.php');
}
else if($type==video)
{
include('categories2.php');
}
else if($type==sound)
{
include('categories3.php');
}
?>
i use the url like:
/category.php?id=64&ids=305
thank you in advanced
write like this:
if($type=="video")
else if($type=="sound")
Are you looking for
$id1=$_GET["ids"];
$id2=$_GET["id"];
or
$id1=$_POST["ids"];
$id2=$_POST["id"];
???
This error basically means that you're trying to use the variables $ids and $id, even though they do not exist. You need to define a variable before you can use it. Otherwise, you will get notices like this and your application will end up being a buggy mess. Because those two variables are not set and you are assigning them to other variables:
$id1 = $ids;
$id2 = $id;
Those variables are now useless, which means that your SQL query is also useless:
$result= mysql_query("SELECT * FROM products where id like '$id1' ;");
because $id1 isn't anything.

Categories