PHP Notice: Undefined offset: 1 [duplicate] - php

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);
}

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"

ErrorException (E_NOTICE) Undefined variable: actualLabels [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 3 years ago.
Help, i got an error "ErrorException (E_NOTICE) Undefined variable: actualLabels" in my code
$title = "Data Confusion Matrix";
$testing_data = DataTesting::count();
$klasifikasi = Klasifikasi::with('sentimen')->get();
foreach($klasifikasi as $kelas){
$predictedLabels[] = $kelas->sentimen->kategori;
$testing = DataTesting::where('id_testing',$kelas->id_testing)->first();
$twitter = TwitterStream::with('sentimen')->where('id_crawling',$testing->id_crawling)->first();
$actualLabels[] = $twitter->sentimen->kategori;
}
$getPrecision = new ControllerConfusionMatrix($actualLabels, $predictedLabels);
$accuracy = ControllerConfusionMatrix::score($actualLabels, $predictedLabels);
$recall = $getPrecision->getRecall();
$precision = $getPrecision->getPrecision();
Add this line to beginning of your code : $actualLabels = [];
You are getting error because when $klasifikasi is empty, then the statement inside loop is not executed. So $actualLabels variable is not created. In this case you get errro of (E_NOTICE) Undefined variable: actualLabels.
Hope you understand.
define Array()
$predictedLabels = array();
$actualLabels = array();
foreach($klasifikasi as $kelas){
$predictedLabels[] = $kelas->sentimen->kategori;
$testing = DataTesting::where('id_testing',$kelas->id_testing)->first();
$twitter = TwitterStream::with('sentimen')->where('id_crawling',$testing->id_crawling)->first();
$actualLabels[] = $twitter->sentimen->kategori;
}

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];
}

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

PHP Undefined Index [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 4 years ago.
I have this code and it's giving me an undefined error if country is not a variable in the URL. I've never had this problem before so it's obviously something to do with server settings but I'd rather not change those because this is probably done for security purposes. I've searched Google and can't find the workaround, although I'm sure it's blindingly obvious!
$country = $_GET['country'];
if(!$country){
$incoming = $outgoing = $sms = $free = "---";
} else {
get_rates($country);
}
you should use the following approach:
if (!isset($_GET['country'])) {
$incoming = $outgoing = $sms = $free = "---";
} else {
get_rates($_GET['country']);
}
isset allows you to check if the variable exists (if not we give it the value false).
$country = isset($_GET['country'])? $_GET['country'] : false;
"Undefined index" means that element doesn't exist in the array. Your server is probably set to report these errors, while your system so far wasn't. Check if the element exists before accessing it.
$country = null;
if (!empty($_GET['country']) {
$country = $_GET['country'];
}
You should probably check to see if $_GET['country'] is set. I would wrap the entire code block you posted in an if statement that checks if the country field is set, using the isset function.
Just change:
$country = $_GET['country'];
To this:
$country = #$_GET['country'];
'#' is an operator which will remove errors output.

Categories