Troubleshooting "Undefined offset" error - php

It's show following error:
Notice: Undefined offset: 1 in C:\xampp\htdocs\evantechbd\secure\content\feature_link_process.php on line 28.
Why it's show the error...? Can anyone tell me... Here is the code:
$row = explode("|", $_POST['coun_name']);
$coun_id = $row[1]; // cat_id
$coun_name = $row[0];
if(isset($coun)){
$errors = array();
if(empty($coun))
$errors[] = 'Country Name require<br>';
}

$row = explode("|", $_POST['coun_name']);
$coun_id = $row[1]; // cat_id
$coun_name = $row[0];
if $_POST['coun_name] has no | than $row[1] is not defined.

You will probably want to do something like this, instead:
$errors = array();
if( empty($_POST['coun_name']) )
{
$errors[] = 'Country Name required.';
}
elseif( strpos($_POST['coun_name'], '|') === false )
{
$errors[] = 'Country Name invalid.';
}
else
{
list($coun_name, $coun_id) = explode("|", $_POST['coun_name'], 2);
}
The first condition checks to make sure the user submitted a value for $_POST['coun_name']. The second condition checks to make sure that the value contains a '|' character.
If both conditions are met, $coun_name and $coun_id are populated by explode()'ing $_POST['coun_name'] as normal (I dropped in a call to list() for brevity).

Related

Add item to the cart and change quentity show following error.how to fix it

Notice: Undefined index: 9781400108503 in C:\xampp\htdocs\ibshelf.com\cart.php on line 38
Notice: Undefined index: 9781400108503 in C:\xampp\htdocs\ibshelf.com\cart.php on line 42
Warning: array_count_values(): Can only count STRING and INTEGER values! in C:\xampp\htdocs\ibshelf.com\cart.php on line 53
// book_isbn got from form post method
if(isset($_POST['bookisbn'])){
$book_isbn = $_POST['bookisbn'];
}
if(isset($book_isbn))
{
// new item selected
if(!isset($_SESSION['cart']))
{
// $_SESSION['cart'] is associative array that bookisbn => qty
$_SESSION['cart'] = array();
$_SESSION['total_items'] = 0;
$_SESSION['total_price'] = '0.00';
}
if(!isset($_SESSION['cart'][$book_isbn]))
{
$_SESSION['cart'][$book_isbn] = 1;
} elseif(isset($_POST['cart']))
{
$_SESSION['cart'][$book_isbn]++;
unset($_POST);
}
}
// if save change button is clicked , change the qty of each bookisbn
if(isset($_POST['save_change'])){
foreach($_SESSION['cart'] as $isbn =>$qty){
if($_POST[$isbn] == '0')
{
unset($_SESSION['cart']["$isbn"]);
} else {
$_SESSION['cart']["$isbn"] = $_POST["$isbn"];
}
}
}
$title = "Your shopping cart";
if(isset($_SESSION['cart']) && (array_count_values($_SESSION['cart']))){
$_SESSION['total_price'] = total_price($_SESSION['cart']);
$_SESSION['total_items'] = total_items($_SESSION['cart']);
"9781400108503" seems to be the value of $isbn in the for loop on save_change.
On line 42, the notice is triggered for $_POST[$isbn]. Make sure you use isset($_POST[$isbn]). There doesn't seem to be a value posted for this ISBN.
On line 53, you're using array_count_values(). That's not the function you're intending to use. Do count($_SESSION['cart']) instead.

how to resolve this error: (Warning: Illegal string offset) and (Notice: Uninitialized string offset: 0)

Let's say I have search.php and edit.php.
Inside the search.php they can remove and update some records.
If the user clicks the "update" button, the system will redirect the user to another page called edit.php.
I successfully called the itemid.
but this happens..
<?php
$itemid = $_GET["itemid"];
$description = "";
$classification = "";
$unit = "";
$quantity = "";
$price = "";
$reorder = "";
$status ="";
$record = MYDB::query(
"select
*
from
item
where
itemid = ? ",
array($itemid),
"SELECT"
);
if(count($record) > 0)
{
$record = $record[0];
$description = $description['description'];
$classification = $classification['classification'];
$unit = $unit['unit'];
$quantity = $quantity['quantity'];
$price = $price['price'];
$reorder = $reorder['reorder'];
$status = $status['status'];
}
else
{
echo '<div class="fail">';
echo '<b>FAIL!</b> Item Not Found!</div>';
die();
}
if(isset($_POST["btnsubmit"]))
{
if(isset($_POST["description"])){
$description=ucwords(trim($_POST["description"]));
}
else{
echo "Please Enter description";
}
if(isset($_POST["classification"])){
$classification=ucwords(trim($_POST["classification"]));
}
else{
echo "Please Enter classification";
}
if(isset($_POST["unit"])){
$unit=ucwords(trim($_POST["unit"]));
}
else{
echo "Please Enter unit";
}
if(isset($_POST["quantity"])){
$quantity=ucwords(trim($_POST["quantity"]));
}
else{
echo "Please Enter quantity";
}
if(isset($_POST["price"])){
$price=ucwords(trim($_POST["price"]));
}
else{
echo "Please Enter Price";
}
if(isset($_POST["reorder"])){
$reorder=ucwords(trim($_POST["reorder"]));
}
else{
echo "Please Enter reorder";
}
if(isset($_POST["status"])){
$status=ucwords(trim($_POST["status"]));
}
else{
echo "Please Enter status";
}
}
?>
This are the errors,
Warning: Illegal string offset
Notice: Uninitialized string offset: 0
That error basically means that you are calling an array by a key which does not exist. I would tell you which key that is but you have not provided the specific variable.
Say I have this array:
$array = ('some_real_key' => 'a very important value');
Now if I call this $array['some_fake_key'], given that our array does not have that some_fake_key then it will produce the error you are seeing. Same goes for your 0 offset.
You are calling it in your code:
$record = $record[0];
That means that there's no 0 offset, which could mean a range of things... Again there's not enough data provided. But it would follow the same example as above.
To fix those issues you can use array_key_exists():
if ( array_key_exists( 'some_real_key', $array )
{
echo $array['some_real_key'];
}
else if ( array_key_exists( 'some_fake_key', $array )
{
echo $array['some_fake_key'];
}
This will output only the first array key; and will output no errors.
UPDATE
Thinking about it, I think your error may be produced from your variables as follows:
$record = $record[0];
$description = $description['description'];
$classification = $classification['classification'];
$unit = $unit['unit'];
$quantity = $quantity['quantity'];
$price = $price['price'];
$reorder = $reorder['reorder'];
$status = $status['status'];
You are not really setting them from an array; you may be wanting to call $record['key'] instead of $description = $description['description'];
For instance this would look like:
$description = $record['description'];
// ... so on ...
This looks like the culprit
$record = $record[0];
You cant be setting a variable as a none array with itself becoming an array with an uninitialized value. That's just crazy talk... :)
What do you think you are trying to do with that line?
Assuming $record[0] exists and its some kind of id, you could do...
$record_id = $record[0];
But you are using an associative array for all your other items, so where does the index of 0 come into the picture...
You'd need to perform a var_dump of $record to check you are getting what you expect.

foreach ($delete_addresses[$key] value failure with imap_delete()

In below example there is an array $delete_addresses filled with email-addresses. The print_r() show all correct.
But within the foreach I do something wrong and do not get the $key number needed for the imap_delete).
I get this not saying a lot error message:
Warning: Invalid argument supplied for foreach() in /home/deb96951n2/domains/domain.nl/public_html/wp-admin/email_handlers/bouncehandler.php on line 75
Does someone of you with more experience then me can tell me what I do wrong and how to correct it? Imap is not my daily work.
$delete_addresses = array();
$bouncecounter = 0;
$deletecounter = 0;
for ($n=1;$n<=$num_msgs;$n++) {
$bounce = imap_fetchheader($conn, $n).imap_body($conn, $n); //entire message
$multiArray = $bouncehandler->get_the_facts($bounce);
if (!empty($multiArray[0]['action']) && !empty($multiArray[0]['status']) && !empty($multiArray[0]['recipient']) ) {
echo $multiArray[0]['action'].'= '.$multiArray[0]['status'].'::'.$multiArray[0]['recipient'].'<br>';
$delete_addresses[] = $multiArray[0]['recipient'];
if ($multiArray[0]['action'] == 'failed') {
$result = mysqli_query($db, "UPDATE wp_mm_external_emails set deleted = 1 WHERE email = '".$multiArray[0]['recipient']."' ");
} //if delivery failed
$deletecounter++;
} //if passed parsing as bounce
$bouncecounter++ ;
} //for loop
foreach ($delete_addresses as $key => $value) { //trim($key) is email address, $value is number of failures
if ($value>=$delete) {
/*
do whatever you need to do here, e.g. unsubscribe email address
*/
# mark for deletion
print_r($delete_addresses[$key]);
foreach ($delete_addresses[$key] as $delnum) imap_delete($conn, $delnum);
} //if failed more than $delete times
} //foreach

Notice: Undefined index: name when posting

So I'm trying to grasp the basics of how to make/use an API. So I have a bit of code that should check if the name is what is should be, then return a 1 or 0. So here's the code
$name = $_POST['name'];
//Set array
$status = array();
if(isset($name) || $name = "Bob"){
$status['success'] = 1;
echo json_encode($status);
}
else{
$status['success'] = 0;
echo json_encode($status);
}
Then I hop over to PostMan to send the POST, I do
http://localhost/index.php?name=Bob
But all I end up getting is
( ! ) Notice: Undefined index: name in /Users/matt/Dropbox/API Server/index.php on line 3
Line 3 turns out to be $name = $_POST['name'];. Any help would be great
if you are getting the value from URL(index.php?name=Bob) it has to be
$name = $_GET['name'];

error! got undefined variable if sent empty post

i got error message if sent empty post in my registration form
Notice: Undefined index: terms in /opt/lampp/htdocs/user/register.php on line 34
in the line 34 i have this
$terms = trim($_POST["terms"]);
and in the form i have this
<p>
<input type="checkbox" name="terms" id="terms"> I have read and accept the conditions of use
</p>
and this is all the validation form
if(!empty($_POST))
{
$errors = array();
$terms = trim($_POST["terms"]);
$captcha = md5($_POST["captcha"]);
$name = trim($_POST["name"]);
if($terms == "")
{
$errors[] = lang("ACCOUNT_SPECIFY_NAME");
}
//End data validation
if(count($errors) == 0)
{
//Construct a user object
$user = new User($username,$password,$email,$name,$lastname);
//Checking this flag tells us whether there were any errors such as possible data duplication occured
if(!$user->status)
{
if($user->username_taken) $errors[] = lang("ACCOUNT_USERNAME_IN_USE",array($username));
if($user->email_taken) $errors[] = lang("ACCOUNT_EMAIL_IN_USE",array($email));
if($user->email_blocked) $errors[] = lang("ACCOUNT_EMAIL_BLOCKED");
}
else
{
//Attempt to add the user to the database, carry out finishing tasks like emailing the user (if required)
if(!$user->userCakeAddUser())
{
if($user->mail_failure) $errors[] = lang("MAIL_ERROR");
if($user->sql_failure) $errors[] = lang("SQL_ERROR");
}
}
}
}
?>
why i got this alert message?
thanks
Try to replace this:
if(!empty($_POST))
with this:
if(!empty($_POST["terms"]))
You need to make sure that array $_POST has a value with key terms
EDIT:
Try this:
$terms = empty($_POST["terms"]) ? null : trim($_POST["terms"]);
$captcha = empty($_POST["captcha"]) ? null : md5($_POST["captcha"]);
$name = empty($_POST["name"]) ? null : trim($_POST["name"]);
instead of this:
$terms = trim($_POST["terms"]);
$captcha = md5($_POST["captcha"]);
$name = trim($_POST["name"]);
It's not an error but a Notice and a notice can be ignored if it’s not critical..
if(isset($_POST['terms']))$terms = trim($_POST["terms"]);
isset() function in PHP determines whether a variable is set and is not NULL. It returns a Boolean value, that is, if the variable is set it will return true and if the variable value is null it will return false.
You can also turn off notices by using
error_reporting(E_ALL ^ E_NOTICE);
in the start of your script..

Categories