PHP Undefined Index [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 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.

Related

how to send checkbox value 1 or 0 in database? [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.
<input type="checkbox" name="chkbx" value="true" checked>
$data['active'] = $this->input->post('active');
if($data['active'] == true){
echo $data['active'] = 1 ;
}
else{
echo $data['active'] = 0 ;
}
It always put 0 value in database. I can't understand.
please help me to solve this problem.
Checkboxes are posted only when they are checked.
Use isset()
Use proper checkbox name.
$data['active'] = $this->input->post('chkbx');
if (isset($data['active'])) {
echo $data['active'] = 1;
}
else {
echo $data['active'] = 0;
}
Your checkbox name name="chkbx" and $this->input->post('active') is different. Use the same and check.
According to docs "The main advantage of using the provided functions rather than fetching an item directly ($_POST['something']) is that the functions will check to see if the item is set and return false (boolean) if not".
try using
if($data['active'] !== false){
// Check box value is set check its value and insert in DB here
}
else{
// Check box not set you can have your fall back method
}

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

Undefined index variable in $_REQUEST in PHP, getting a boolean value when using ISSET() [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 used $value = (isset($_REQUEST['value'])) to define the index variable. However, $value variable shows as a type Boolean and value of 0 or 1 in the debugger, which results in not getting the correct results when the $value is used with a (if) statement.
$page_limit = (isset($_REQUEST["list_page"]));
$viewdate = (isset($_REQUEST["viewdate"]));
How do I correct the following so that the index is defined?
if($_REQUEST["viewdate"] == '') {
$viewdate = 'All';
} else {
$viewdate = $_REQUEST["viewdate"];
}
$targetpage = "newindex.php?viewdate=".$_REQUEST["viewdate"]."&search=Search";
$page = (isset($_REQUEST['page']));
if($page_limit == '') {
The code above works, without the isset() function, but displays Notice - E messages
You should use isset to test whether a variable has been set, then assign the value. Like so:
$something = null;
if (isset($_REQUEST['something'])) {
$something = $_REQUEST['something'];
...
}
// Later in the code
if ($something !== null) {
// Do stuff

skipe $_POST in php [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.
i have a php page which i should post some data to it like this:
$player=$_POST['player']; $age=$_POST['age']; $data=$_POST['data'];
but some times my page posts data and some times it shouldn't, when i dont post data i got Undefined index: error, is there any way to skip $_POST['data'] when no data is posted?
$player = isset($_POST['player'])?$_POST['player']:"";
$age = isset($_POST['age'])?$_POST['age']:"";
$data = isset($_POST['data'])?$_POST['data']:"";
The isset first checks if it's defined, if yes, it assigns the variable else it assigns empty string.
Use simple if (isset($_POST['key'])):
$player = isset($_POST['player']) ? $_POST['player'] : '';
$age = isset($_POST['age']) ? $_POST['age'] : '';
$data = isset($_POST['data']) ? $_POST['data'] : '';
Or better, but every value to some 'refix':
<input name="Data['player']"/>
and in php just do:
if (isset($_POST['Data'])) {
$player = $_POST['Data']['player'];
/* ... */
}
if(isset($_POST['DATA'])){
$data = $_POST['DATA']
}
if(isset($_POST['player'])):
$player = $_POST['player'];
endif;
Check if data is posted
if(isset($_POST['player']))
$player=$_POST['player'];
}
if(isset($_POST['age']))
$age=$_POST['age'];
}
if(isset($_POST['data']))
$data=$_POST['data'];
}

Messages between PHP files [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.
I have two PHP files, index.php and insert.php.
index.php - a form and a button to send data
insert.php - receives the values of the form to insert them into a database, and it works.
When I redirect to the index.php, it shows a successful message.
I have this code in index.php to show the message:
<?php
$mensaje=$_GET['mensaje'];
if($mensaje=="")
{
}
else
{
echo "¡Registro exitoso!";
}
?>
but when I open it the first time, it shows me:
Notice: Undefined index: mensaje in C:\xampp\htdocs\..\index.php on line 41
How could I show the message ONLY after the insert?
Like this:
$mensaje = isset($_GET['mensaje']) ? $_GET['mensaje'] : "";
That way, $mensaje will never be undefined.
The ternary operator works this way:
$mensaje = if($_GET['mensaje'] exists) then $mensaje = $_GET['mensaje'], else $mensaje = "";
It's like doing:
if(isset($_GET['mensaje'])){
$mensaje = $_GET['mensaje'];
}
else
$mensaje = "";
}
For a more accurate information about ternary operators:
http://en.wikipedia.org/wiki/%3F:
For information about function "isset":
http://php.net/manual/en/function.isset.php
This happens because your $_GET['mensaje'] not only is empty but doesn't exist at all.
To work this around, replace
$mensaje = $_GET['mensaje'];
by
$mensaje = empty($_GET['mensaje'])? "": $_GET['mensaje'];
Thus you assign an empty string "" if $_GET['mensaje'] is either blank or not set.
Can $mensaje really be empty (as your if condition implies) or is it simply defined or undefined in $_GET (as your text description implies)? If the latter is the case than your solution is to test for existence, not for content:
<?php
if( isset($_GET['mensaje']) )
{
echo "¡Registro exitoso!";
}
else
{
}
?>
isset() is a language construct, not a function. This is the reason why it can be used to test for existence of an array index without throwing an "undefined index" notice if the index does not exist.
Hint (just for completeness):
isset() will also return False if the value of an array element is NULL. But this is irrelevant in the case of $_GET, as it always and exclusively contains string values.

Categories