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.
my php is very "weak" so I am having trouble with this -
<?php
include_once "../scripts/connect_to_mysql.php";
$title = $_POST['title'];
$desc = $_POST['description'];
$query = mysql_query("UPDATE images SET title='$title', description='$desc'") or die (mysql_error());
echo 'Operation Completed Successfully! <br /><br />Click Here';
exit();
?>
The undefined index error is description on line 9 which is $desc = $_POST['description']; and "description" is in the database spelling is also right.
Your form needs to send this information to the php file. It seems as you only have title in your form and not description. You should post your form for better help.
<form ...>
<input type="text" name="description" value="">
<input type="text" name="title" value="">
</form>
You should always escape your data which you are using in SQL queries.
Apparently, a description field is not present in the $_POST array, thus index description does not point to anything.
What does var_dump($_POST); print?
Also, is this an internal system? If not, you should read up on SQL injections.
There is no element with index description in the $_POST array.
Try var_dump($_POST); before that line to examine the array. (Alternatively, if some information is cut, use print_r($_POST);
Try doing:
if(isset($_POST['description'])){echo "yes";}else{echo "no";}
This wont fix it but it will help determing whether $_POST['description'] exists.. Also, what is the source of $_POST['description']?, is it set in ../scripts/connect_to_mysql.php? How?
Unless performing more complex tasks (i.e. AJAX), POST variables are typically set by submitting a form- does your code come after the page is loaded following a form submission? If not, you may be incorrectly using _POST variables...
If $_POST['description'] is set somewhere in the code of ../scripts/connect_to_mysql.php, and by including the file you wish to then retrieve its value- you may be going about things wrongly- can you provide information about the origin of $_POST['description']?
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 6 years ago.
I have a php script which adds rows to a MySQL database, when I set variables in the script it works fine. As is the norm I want values to be entered via an html page. To test that I understood I could run my php script with arguments to simulate the $_POST values, similar to:
http://www.website.com/php/insert_client3.php?nickname='Bob'&emailaddress='bob#yahoo'
The script beginning is:
<?php
print_r($_POST);
var_dump($_POST);
// check for required fields
$nickname = $_POST['nickname'];
$emailaddress = $_POST['emailaddress'];
print_r and var_dump are returning empty arrays, is my approach wrong or syntax? I'm trying to build up html entry of data by starting with the database and working back
Any help appreciated...
Thanks
You're looking for $_GET and not $_POST. Variables passed in the query string are sent with an HTTP GET request, whereas HTTP POST request data is not visible in the URL.
<?php
print_r($_GET);
var_dump($_GET);
// check for required fields
$nickname = $_GET['nickname'];
$emailaddress = $_GET['emailaddress'];
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.
I have looked all over for a answer to this. But all the questions asked don't seem to answer my question.
I am trying to post from a html page to a php page.
But i am receiving this notice:
Notice: Array to string conversion ... line 41
In order to fix the issue i have stopped the page i am working on (index page) from posting to the next page, and post to the index page instead, so i can see what is going on.
Line 41
WHERE Delivery_Pcode.Pcode LIKE '%".$search."%'");
I am using the exactly the same query, part from the variable names in another page, and it works perfectly.
I am trying to retrieve the area name from the db, which matches the postal code which is inputted by the user in the search bar on the index page, there is nothing on the index page apart from the search bar.
Code
$c_name= '';
if(isset($_POST['feed_me'])){
$search= $_POST;
$select_pcode=mysqli_query($dbc,"SELECT Rest_Details.Resturant_ID, Rest_Details.City_name, Delivery_Pcode.Pcode
FROM Rest_Details INNER JOIN Delivery_Pcode
ON Delivery_Pcode.Restaurant_ID=Rest_Details.Resturant_ID
echo var_dump($select_pcode);
while($row_pcode= mysqli_fetch_array($select_pcode)){
$c_name = $row_pcode['City_name'];
}}
I have never received this notice before, i am unaware how to deal with it or how to resolve the issue. Please advice
You are copying the whole $_POST array to $search and then you are trying to concatenate the array to a string.
This is the correct way:
if(isset($_POST['feed_me'])){
$search= $_POST['feed_me'];
Your $search is $search= $_POST; and $_POST is array.
When you use it in %$search% here you get this error.
Implode $_POST with implode('", "', $_POST), take only one value ($_POST['feed_me']) or other way make it to string.
Don't use direct $_POST in query. Use prepared statements.
Well, $_POST is an array and you're passing its whole value to $search. Then you're using $search to display a message. Since $_POST is of type array, when trying to display $search, you would be mixing a string with an array, hence the notice.
Since you want to compare Delivery_Pcode.Pcode, you need to call the "Pcode" that you sent from the $_POST to the $search. Something like this:
$search["Pcode"]
That would bring the value you're looking for (if that's the name of the value).
$search= $_POST;
That's the mistake, change it to
$search= $_POST['feed_me'];
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 attempted to search pretty thoroughly prior to asking this question (very few questions had any back story so I'm unsure if they fit or no). I am attempting to add a select field to a form in which an "if, else" function will result in a line of code being echoed IF the parameter is "yes". For the HTML, the config is as follows:
<select name="pim">
<option value="pim_enabled">Yes</option>
<option value="pim_disabled">No</option>
</select>
On my resulting configuration page, I am attempting to reference the option values and provided pim enabled is selected, additional lines of code will be echoed in which a value from a former field (TenGigED/D/D/D) will be echoed as well. My current configuration here is as such:
<?php if ($_POST["pim_enabled"]) {
echo
"Router PIM"
echo $_POST["TenGigEC/C/C/C"];
} else {
echo
"Final Config"
}
?>
I'm relatively new to this process, but I feel some degree of certainty that I'm off base with my if/else statement. Hopefully you guys can show me the error of my ways and I can build from the lesson learned.
Thanks in advance!
Your $_POST should be reading $_POST['pim']
It's value will either be "pim_enabled" or "pim_disabled", depending on the users' selection on the form.
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:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
first time asking questions here. I am trying to create a small, primitive forum. I am basing it on this: while making changes to it where it is needed.
So far, I have setup the database and all that. Now I am trying to fix the view_topic.php and it is returning "Notice: Undefined index: id" on line 30 something.
This is line 30: $id=$_GET['id'];
You should make sure id is a valid index before accessing it.
$id = isset($_GET['id']) ? $_GET['id'] : null;
Notice: Undefined index:
This means that a piece of code attempts to access an element of an array that does not exist.
So, for example:
$myArray = Array(); // an empty array
echo $myArray['id']; // print 'id' element
// ^^ Oops! No such element yet!
The $_GET array contains the arguments from the querystring, which is the ?id=1 part of your request URL.
You will need to find out why this does not contain the element with key 'id' when id was provided in the URL, and then make your code decide what to do in the case that this value is missing, using a function such as isset or array_key_exists and an if statement.
Most likely you will present a more useful error message and terminate the script, if your code cannot continue without a valid id value.
Are you sure id= exists in the URL? Try adding the following near the top of your code.
var_dump($_GET);
var_dump dumps information about a variable to your screen. In this case, it will display all the GET data sent from your form. This is a debugging technique so you can see if your code is receiving what you expect. If the form uses method="post" and id is a field in the form, then you will need to use $_POST['id'] or $_REQUEST['id'] to access it.