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 just started coding so i dont know whats wrong with my code
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: kd_user
Filename: views/dokter.php
Line Number: 1
Controller
function read_user()
{
$this->load->model('dokter_model');
$data['datauser']=$this->dokter_model->read_user();
$this->load->view('dokter', $data);
}
Model
function read_user()
{
$q="SELECT a.*, b.*
FROM users a
LEFT JOIN dokter b ON a.kd_user=b.kd_user
WHERE a.kd_user='".$this->session->userdata('kd_user')."'";
$query=$this->db->query($q);
return $query->result();
}
Views
<p align="center">Selamat Datang <?php echo $kd_user;?></p>
Sorry for my poor english
Change :
$data['datauser']=$this->dokter_model->read_user();
to:
$data['kd_user']=$this->dokter_model->read_user();
In general, the error
Message: Undefined variable: kd_user
Means that you are referring to a variable (in this case, $kd_user), and it was never defined (you never initialized it and/or assigned a default value to it.)
Looking at your code, you are referring to kd_user in two places -- once in the associative array user_data in your model, and once in your view as kd_user.
To begin, you may wish to debug in each of those files (most probably the view) if that variable actually exists.
Without seeing all of your app's code, its hard to say for sure, but I'd verify that your view has a variable available called $kd_user for you to access. If it isn't there, you either need to find a way to get it into there, or reference another variable to get to the value you need.
Related
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 am currently in a process of writing my PHP script, and I am doing it with
E_ALL set in php and I can not allow ANY errors, even
PHP Notice: Undefined variable
And I have many places where i ECHO $myvariable without checking if it's empty, which is causing error mentioned above.
Is it just me or it's extremely stupid to do this for every variable that can be undefined:
if(!empty($myvariable)) {
echo $myvariable;
}
Is this only way to avoid these errors?
EDIT:
Question is not a duplicate, what you refereed to as duplicate has nothing to do with what i asked here.
Three possible solutions:
1st: initialize your var at the very beginning of your code (or method, or class, whatever you're doing)...
$var = "";
// Do stuff with $var
echo $var;
2nd: use a ternary operator
echo (isset($var)) ? $var : "";
3rd: use #
#echo($var);
It is a notice which gives you an insight that your code could produce unexpected results since you did not assign a value (constant or calculated).
You can avoid this error by checking if it's set using the isset function or set a value for that variable at the top of your script.
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.
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']))
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Best way to test for a variable's existence in PHP; isset() is clearly broken
(17 answers)
Closed 7 years ago.
How do the following two function calls compare to check if a variable exists for a GET call or a POST call ?
if(isset($_GET['var']))
if($_GET['var'])
Both do their job but sometimes without isset() i get a warning :
PHP Notice: Undefined index: var on at Line XX
Update
It was a $_GET not $GET. Also i know it has nothing to do with GET/POST. All i am discussing is variable check instead of HTTP method. Thanks.
$_GET[''] is usually retrieved from the browser, so if you had something like the following:
www.hello.com/index.php?var=hello
You could use:
if(isset($_GET['var'])) {
echo $_GET['var']; // would print out hello
}
Using isset() will stop the undefined index error, so if you just had www.hello.com/index.php for example then you would not see the error even though the var is not set.
Posts are usually when one page posts information to another, the method is the same but using $_POST[''] instead of $_GET['']. Example you have a form posting information:
<form method="post" action="anotherpage.php">
<label></label>
<input type="text" name="text">
</form>
In anotherpage.php to get the information would be something like:
$text = isset($_POST['text']);
echo $text; // would echo what ever you input into the text field on the other page
In a nut shell, just putting $_GET['name'] if name is not set you will get an error.
Using isset($_GET['name']) will check is the var name has any value before continuing.
Not sure if this is what you were after, but from the question this is my best guess at an answer
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.
I'm getting an undefined index notice, but can't figure out how to fix it.
Notice: Undefined index: id in /home/jharvard/vhosts/localhost/html/book_details.php on line 10
This is the code that's having problems:
// query for the listing
$listing = query("SELECT name, author, id, edition, price, course, date FROM books WHERE submission = ?", $_GET["submission"]);
// query for the email of the seller
$seller = query("SELECT email FROM users WHERE id = ?", $listing["id"]);
Can anyone help? Thank you very much.
The problem is that the array returned from your first query, $listing doesn't have an element with the key id.
I suspect this would happen if there was no record in the database for whatever was passed in as the submission GET parameter. Hard to say for sure though without knowing how the query() function was written.