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'];
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 am getting this error:
undefined index:name
why its not responding.
<?php
$url="localhost";
$user="root";
$password="";
$db="sms";
$connection=new mysqli($url,$user,$password,$db);
$username=$_POST["name"];
$query="INSERT INTO student(name)VALUES('".$username."')";
$connection->query($query);
?>
When you get Username $username = $_POST["name"]; Value for $username variable come from global $_POST variable. If you start your script without POST, you cant read key 'name' from $_POST array. This key didn't exist and you get this error. SOLUTION for this problem. Just check for $_POST value with isset() and if() statments.
if (isset($_POST["name"])) {
$username=$_POST["name"];
$query="INSERT INTO student(name)VALUES('".$username."')";
$connection->query($query);
}
Make sure that the <input type="blablabla" name="name" ...>
Also make sure to wrap the PHP code in:
if(!empty($_POST["name"])){
// the code you posted ...
}
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 this code below. The $address variable must get a string, in order to convert, and send it to a database. I have a form with inputs, which I store in further variables. How should i convert the values to get lat and long?
$address = "Rákoscsaba, Nagyréde utca 8. 1172"; // Google HQ
$prepAddr = str_replace(' ','+',$address);
$geocode=file_get_contents('https://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
It s working... the 3 variables I want to use: $user_telepules, $user_utca', $user_irsz
I've already tried these ways:
$address =['user_telepules'].','.['user_utca'].['user_irsz'];
$address =(string)$user_telepules.','.(string)$user_utca.(string)$user_irsz;
Ok, now I get it. Show us few data samples from this three variables. Maybe there is problem with something else, not string format.
How are you passing variables from input? Have you checked, if they actually exist and pass inputs from form?
It should be already a string, form inputs always passes strings to php interpreter.
$address = $user_telepules.",".$user_utca." ".$user_irsz;
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)
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']);
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']?