Notice: Undefined index: intakeid [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)
Closed 8 years ago.
I am trying since last hour to update my database using php but unable to update.
I get the error
Notice: Undefined index: intakeid in C:\wamp\www\Multi_Edit\edit_save.php on line 3.
<?php
include('dbcon.php');
$intakeid=$_POST['intakeid'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$password=$_POST['password'];
$confirmpassword=$_POST['confirmpassword'];
$homeaddress=$_POST['homeaddress'];
$email=$_POST['email'];
$N = count($intakeid);
for($i=0; $i < $N; $i++)
{
$result = mysql_query("UPDATE registration SET firstname='$firstname[$i]',
lastname='$lastname[$i]',
password='$password[$i]',
confirmpassword='$confirmpassword[$i]',
homeaddress='$homeaddress[$i]',
email='$email[$i]'
WHERE intakeid='$intakeid[$i]`enter code here`'")or die(mysql_error());
}
error_reporting(E_ALL);
ini_set("display_errors", 5);
?>

Its always best method to check $_POST['value'] variable has value set or not using isset() function , otherwise you will get NOTICES as you have it already. check this

It happens simply there is no variable called "intakeid" in $_POST array. If you still need to get it (whereas it is set) you can do something like follows.
$intakeid=(isset($_POST['intakeid']) ? $_POST['intakeid'] : NULL ;
This will put $intakeid to null if the post variable not have the value and if it has it will put the posted value

Related

PHP Variable Undefined [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 4 years ago.
i get this php error
Notice: Undefined variable: total_earn in /var/www/..../hello.php on
line 221
But the variable works fine..
anyone can help me & have an idea ?
<?php
$con=mysqli_connect("localhost:3306","***","***","superpayment");
$a="select * from users where ref='".$req_user_info['refer']."' ";
$b=mysqli_query($con,$a);
$c=mysqli_fetch_array($b);
$mm="select * from configuration where config_name='refer_referer_points' ";
$nn=mysqli_query($con,$mm);
$oo=mysqli_fetch_array($nn);
if($c['id']>0) {
$x="select * from users where ref='".$req_user_info['refer']."' ";
$q=mysqli_query($con,$x);
$i=0;
while($res=mysqli_fetch_array($q))
{
$i++;
$x="select count(*) as ld, sum(points_used) as earn from Completed where username='".$res['username']."' ";
$y=mysqli_query($con,$x);
$z=mysqli_fetch_array($y);
$user_earn=$z['earn']/1000;
$your_earn=($user_earn*$oo['config_value'])/100;
$total_earn=$total_earn+$your_earn;
?>
The jist of it, is $total_earn is not yet initialized when you're doing $total_earn=$total_earn+$your_earn;. While PHP will automatically initialize it (As 0), it's letting you know that by not initializing it, it could be a bug in your code.
It's helpful when you may accidentally misspell a variable. See this post for more information on this.
Set it like so:
$i=0;
$total_earn=0;
while($res=mysqli_fetch_array($q))
{
$i++;
$x="select count(*) as ld, sum(points_used) as earn from Completed where username='".$res['username']."' ";
$y=mysqli_query($con,$x);
$z=mysqli_fetch_array($y);
$user_earn=$z['earn']/1000;
$your_earn=($user_earn*$oo['config_value'])/100;
$total_earn=$total_earn+$your_earn;

How to Avoid Notice: Undefined index in php and set default value [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 7 years ago.
I want a default value and get rid off the notice. Below is my code
<?php
$color = $_POST["color"]; //initialize variable
if (!isset($color)){ // if nothing selected set default to Blue
$color = 'Blue';
}
echo $color;
?>
So when I run the code and nothing is selected and the blue value (as it is set for default) does show up but the notice message also shows up. I want to get rid of the notice message completely. I have tried array_key_exists as well. Still can not get rid of the notice.
below is the notice I'm getting.
Notice: Undefined index: color in C:\xampp\htdocs\test3\Assignment_1_Q2.php on line 17
Thank you in advance.
Try this, First check the value with isset function.
<?php
$color = ""; //initialize variable
if(isset($_POST["color"]))
{
$color = $_POST["color"];
}
else
{
// if nothing selected set default to Blue
$color = 'Blue';
}
echo $color;
?>

Error when writing code php [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 8 years ago.
if i write code php same:
<?php
$title = strip_tags($_POST['title']);
?>
unknown error show!
Notice: Undefined index: title in C:\xampp\htdocs\file.php on line 3
$_POST has value, after submitting form, so before that anybody can't use $_POST ..
<?php
if(isset($_POST['title'])){
//Here in condition if(array_key_exists ( 'title' , $_POST )) can also be checked...
//OR if(!empty($_POST)) OR if(!empty($_POST['title'])) can also be put..
$title = strip_tags($_POST['title']);
}
?>
If both form and action in the same page, the first load will show error as there is no data posted. So first checj whether a POST has been made and then assign. Try this
$title = "";
if(isset($_POST['title'])){
$title = strip_tags($_POST['title']);
}

Undefined index on page load [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 8 years ago.
the message is
Notice: Undefined index: flag in C:\xampp\htdocs\myfiles\mobile tracking\index.php on line 63
my code is
<?php
$stat=$_REQUEST['flag'];
if($stat=="FAILED")
{
echo "Username/password doesnot exists";
}
?>
The global variable $_REQUEST['flag'] is probably having value NULL. This is the reason you are getting this error. Well, try using isset(). to check whether the variable is having any value or not.
You should check if the $_REQUEST['flag'] variable has been set:
<?php
$stat= ( isset($_REQUEST['flag']) ? $_REQUEST['flag'] : null) ;
if($stat=="FAILED")
{
echo "Username/password doesnot exists";
}
?>
You received a notice because you didn't initialized the values of the array. Use this construction to prevent them.
if (! array_key_exists('flag', $_REQUEST)) {
$_REQUEST['flag'] = whatever value goes here;
}

Notice: Undefined variable: ids in [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.
i have a script that works fine for years and suddenly i face this problem :
please check the code and mend any solutionplease. any kind of help willbe appreciated.
Notice: Undefined variable: ids in /home/content/a/t/a/ataasaid1/html/category.php on line 4
Notice: Undefined variable: id in /home/content/a/t/a/ataasaid1/html/category.php on line 5
Notice: Use of undefined constant image - assumed 'image' in /home/content/a/t/a/ataasaid1/html/category.php on line 11
Notice: Use of undefined constant video - assumed 'video' in /home/content/a/t/a/ataasaid1/html/category.php on line 15
Notice: Use of undefined constant sound - assumed 'sound' in /home/content/a/t/a/ataasaid1/html/category.php on line 19
the code in file category.php is :
<?
error_reporting(E_ALL | E_STRICT);
ini_set("display_errors", 1);
$id1=$ids;
$id2=$id;
include('config.php');
$result= mysql_query("SELECT * FROM products where id like '$id1' ;");
$row=mysql_fetch_array ($result);
$type=$row['type'];
if($type==image)
{
include('categories.php');
}
else if($type==video)
{
include('categories2.php');
}
else if($type==sound)
{
include('categories3.php');
}
?>
i use the url like:
/category.php?id=64&ids=305
thank you in advanced
write like this:
if($type=="video")
else if($type=="sound")
Are you looking for
$id1=$_GET["ids"];
$id2=$_GET["id"];
or
$id1=$_POST["ids"];
$id2=$_POST["id"];
???
This error basically means that you're trying to use the variables $ids and $id, even though they do not exist. You need to define a variable before you can use it. Otherwise, you will get notices like this and your application will end up being a buggy mess. Because those two variables are not set and you are assigning them to other variables:
$id1 = $ids;
$id2 = $id;
Those variables are now useless, which means that your SQL query is also useless:
$result= mysql_query("SELECT * FROM products where id like '$id1' ;");
because $id1 isn't anything.

Categories