skipe $_POST in php [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 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'];
}

Related

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

Error when writing code 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.
if i write code php same:
<?php
$title = strip_tags($_POST['title']);
?>
unknown error show!
Notice: Undefined index: title in C:\xampp\htdocs\file.php on line 3
$_POST has value, after submitting form, so before that anybody can't use $_POST ..
<?php
if(isset($_POST['title'])){
//Here in condition if(array_key_exists ( 'title' , $_POST )) can also be checked...
//OR if(!empty($_POST)) OR if(!empty($_POST['title'])) can also be put..
$title = strip_tags($_POST['title']);
}
?>
If both form and action in the same page, the first load will show error as there is no data posted. So first checj whether a POST has been made and then assign. Try this
$title = "";
if(isset($_POST['title'])){
$title = strip_tags($_POST['title']);
}

Notice: Undefined index: intakeid [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 am trying since last hour to update my database using php but unable to update.
I get the error
Notice: Undefined index: intakeid in C:\wamp\www\Multi_Edit\edit_save.php on line 3.
<?php
include('dbcon.php');
$intakeid=$_POST['intakeid'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$password=$_POST['password'];
$confirmpassword=$_POST['confirmpassword'];
$homeaddress=$_POST['homeaddress'];
$email=$_POST['email'];
$N = count($intakeid);
for($i=0; $i < $N; $i++)
{
$result = mysql_query("UPDATE registration SET firstname='$firstname[$i]',
lastname='$lastname[$i]',
password='$password[$i]',
confirmpassword='$confirmpassword[$i]',
homeaddress='$homeaddress[$i]',
email='$email[$i]'
WHERE intakeid='$intakeid[$i]`enter code here`'")or die(mysql_error());
}
error_reporting(E_ALL);
ini_set("display_errors", 5);
?>
Its always best method to check $_POST['value'] variable has value set or not using isset() function , otherwise you will get NOTICES as you have it already. check this
It happens simply there is no variable called "intakeid" in $_POST array. If you still need to get it (whereas it is set) you can do something like follows.
$intakeid=(isset($_POST['intakeid']) ? $_POST['intakeid'] : NULL ;
This will put $intakeid to null if the post variable not have the value and if it has it will put the posted value

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