unnecessary notice from 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)
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
recently i just configure my script with common script for entry data
while im trying to submit the data , the data is successful to submit . but there is something notice that bothering me , they say Notice: Undefined index: type in D:\xampp\htdocs\project\submit.php on line 7
and the line is
<?php
include 'includes/config.php';
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$type=addslashes($_POST['type']); // this is line 7
$nama_barang=addslashes($_POST['nama_barang']);
$kategori=addslashes($_POST['kategori']);
$deskripsi=addslashes($_POST['deskripsi']);
im using xampp v.3.2.1 , it is possible the notice is from the xampp ?
thanks you guys im so glad for your answer :))

type (and other $_POST members) may not always be set so you should try and code to detect that.
e.g:
$type = (isset($_POST['type'])) ? addslashes($_POST['type']) : false;

The notice mentions that your $_POST array has no index type.
So you should check for it before you try to access it:
$type = ""; //you could set a default here
if(array_key_exists("type", $_POST))
$type = addslashes($_POST['type']);

Related

Question about isset($_session['username']) [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)
Reference - What does this error mean in PHP?
(38 answers)
How to use store and use session variables across pages?
(8 answers)
Closed 3 years ago.
I have a login page when clicked on defines the variable $_session[username] = $name;
this $_session['username'] is displayed on my main page and it works fine if someone does login. however when someone does not login, the error notice:undefined index pops up. if i use
<?php session_start(); if($_SESSION['username']==""|| isset($_SESSION['username'])){echo "Login";}else{echo "Logout";}?>
i thought it would fix the problem. but it has the same error
if $_SESSION['username] is not set, you'll receive an undefined error message.
I'd recommend using empty(). It's the same thing as saying if( isset($_SESSION['username']) && $_SESSION['username] != '' ). This is more resource intensive (minimally in your case), as it has to check if the variable isset and that it isn't empty (blank, false, 0, null, etc).
if( empty($_SESSION['username']) ){
echo 'Login';
}else{
echo 'Logout';
}

How to get rid of undefined index in this situation [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)
PHP check value against multiple values with OR-operator
(3 answers)
Closed 4 years ago.
Hi,
I have this code:
if($_GET['s']=="page1" || $_GET['s']=="page2" || $_GET['s']=="page3") {
dosomething();
}
and I get this error: : Undefined index: s in
Which I can dismiss only by adding this line:
$_GET['s']="";
but then this wont execute the code correctly since $_GET['s'] is not supposed to have any initial value. How do I fix this other than disabling the notices and errors?
Thank you.
You can check your $_GET['s']
if(isset($_GET['s'])) {
// your code goes here...
}
isset() is used to check if the index exists.

PHP Problems related to Undefined index error [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 6 years ago.
Can you help me with this errors. I am actually trying to generate an invoice. However, my generated invoice having some problems as you can see as the image below.
Here is the image of my coding.
$oid = ( isset($_GET['oid']) ) ? $_GET['oid'] : null;
Also I believe line 38 should come before line 37. bindParam calls a variable defined after it.
You use variable $oid which it's data is $_GET['oid'], in your query before you actually declare/store data to it.
Move line 38 -> $oid = $_GET['oid']; after opening try/catch, that means to line 29, before stating the query. You should implement #fie answer too, to be sure that $oid is not null and that data actually exists.
Also something is wrong with line 73 but we can't see it.

Why is my index undefined, and does it matter? [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 6 years ago.
When i press F12 and look at the warnings i see a message that says
Line 18: Undefined index: task_uid
I just dont really understand why it is undefined, considering that i did declare it, and then select it, in here:
<?php
session_start();
require ("../../../main/gerais/DBConn.php");
$task_uid = $_GET['task_uid'];
$proj_id = $_GET['proj_id'];
?>
This here comes three lines before the "Line 18" that is supposed to be the error, according to the console. Here is this Line:
<?php
if(!empty($_POST['proj_id'])||($_POST['task_uid']))
die("Invalid proj_id or task_uid.");
$query = "
SELECT pm.proj_id, pm.task_uid, etc etc etc...
?>
So why is it undefined? And, while i'm not sure if this question is answerable without context, but how much does it matter, considering this "task_uid" shows up in the URL, and considering it only showed up as a warning, not as a real error?
You need to use !empty on both variables:
if(!empty($_POST['proj_id'])||!empty($_POST['task_uid']))

Undefined Index Error in PHP Image Upload [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.
Can someone tell me what is wrong with this line of code? I am getting an error (yet the the page still functions properly despite the error)
Error:
"Notice: Undefined index: picture in C:\wamp\www\pplogin\members.php on line 180"
Here is the code:
179 $target = "user_images/fs/";
180 $target = $target . basename($_FILES['picture']['name']);
Like I said earlier despite the error the image gets uploaded to the destination and the image name is saved to my MySQL database.
I've been scratching my head for a while now...
$target = !empty($_FILES['picture']['name']) ? $target.basename($_FILES['picture']['name']) : false;
A file with id
picture
doesn't exist.
Use
isset()
to make sure It's set.
You can also use the ternary operator:
$img = isset($_FILES['picture']) ? $_FILES['picture'] : null;
Or something like that :)

Categories