What wrong with undefined index o? [duplicate] - php

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
I got error in my data viewing page, what is the correction for my php script
Here is my php script:
Notice: Undefined index: o in /home/tz005/public_html/COMP1687/view_data.php on line 64
<?php
$con=mysqli_connect("mysql.cms.gre.ac.uk","tz005","punceg5L","mdb_tz005");
if (is_numeric($_GET['o']))
{
$o=$_GET['o'];
}else {
$o=0;
}
if ($o >=1){
$prev=$o-1;
} else{
$prev=0;
}

You need to add an isset() function to check if $_GET['o'] even exists.
Read more about isset() Here
use this:
<?php
$con=mysqli_connect("mysql.cms.gre.ac.uk","tz005","punceg5L","mdb_tz005");
if (isset($_GET['o'] && is_numeric($_GET['o']))
{
$o=$_GET['o'];
}else {
$o=0;
}
if ($o >=1){
$prev=$o-1;
} else{
$prev=0;
}

Related

How can I check if an object method exists? [duplicate]

This question already has answers here:
Is there a way to check if a function exists within a class?
(3 answers)
Check if method exists in the same class
(4 answers)
Closed 3 years ago.
I want to use some code only if the method getProductgroup exists.
My first approach:
if(isset($item->getProductgroup())){
$productgroupValidation = 0;
$productgroupId = $item->getProductgroup()->getUuid();
foreach($dataField->getProductgroup() as $productgroup){
$fieldProductgroup = $productgroup->getUuid();
if($productgroupId==$fieldProductgroup){
$productgroupValidation = 1;
}
}
I got the error message:
Compile Error: Cannot use isset() on the result of an expression (you
can use "null !== expression" instead)
if(($item->getProductgroup())!==NULL){
$productgroupValidation = 0;
$productgroupId = $item->getProductgroup()->getUuid();
foreach($dataField->getProductgroup() as $productgroup){
$fieldProductgroup = $productgroup->getUuid();
if($productgroupId==$fieldProductgroup){
$productgroupValidation = 1;
}
}
But like this I also get an error message:
Attempted to call an undefined method named "getProductgroup" of class
"App\Entity\Documents".
You can use the function method_exists to check if the method is existing in a class or not. for example
if(method_exists('CLASS_NAME', 'METHOD_NAME') )
echo "it does exist!";
else
echo "nope, it is not there...";
In your code try
if(method_exists($item, 'getProductgroup')){
$productgroupValidation = 0;
if(method_exists($item->getProductgroup(), 'getUuid'))
{
$productgroupId = $item->getProductgroup()->getUuid();
foreach($dataField->getProductgroup() as $productgroup)
{
$fieldProductgroup = $productgroup->getUuid();
if($productgroupId==$fieldProductgroup){
$productgroupValidation = 1;
}
}
}
}

getting an "undefined variable" on isset [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.
So i've got a rather complex bit of code that i'm trying to make work (actually; it works fine on my test server but not so much my live server. so i'm trying to make it rework) -- it keeps telling me that a variable i'm asking it to check with isset is not a defined variable. I'm probably over thinking this whole method and there's probably a simplier way. but i need you guys to bash me over the head with it apparently
include('bootstra.php');
include('includes/parts/head.php');
if(!isset($_SESSION['user']['uid']))
{
echo $html;
include('includes/parts/login.php');
if(isset($_GET['mode']))
{
$file = $_GET['mode'];
$path = "includes/parts/{$file}.php";
if (file_exists($path)) {
require_once($path);
} else {
die("The Page REquested could not be found!");
}
}
else
{
include('includes/parts/frontpage.php');
}
}
else
{
if(!isset($_SESSION['x']) || !isset($_SESSION['y']))
{
if(isset($_GET['x']) && isset($_GET['y']))
{
$_SESSION['x'] = $_GET['x'];
$_SESSION['y'] = $_GET['y'];
}
else
{
$_SESSION['x'] = 0;
$_SESSION['y'] = 0;
}
header("location:index.php?x={$_SESSION['x']}&y={$_SESSION['y']}");
}
else
{
if($_SESSION['x'] != $_GET['x'] || $_SESSION['y'] != $_GET['y'] )
{
header("location:index.php?x={$_SESSION['x']}&y={$_SESSION['y']}");
}
else
{
echo $html;
echo $base->displayMap($_GET['x'], $_GET['y']);
include('includes/context_menu.php');
}
}
}
Here is the exact error:
Notice: Undefined index: x in /home/****/public_html/reddactgame/index.php on line 27
Change it
<pre>
if( !isset($_SESSION['x']) || !isset($_SESSION['y']) )
</pre>
to
<pre>
if( !( isset($_SESSION['x']) && isset($_SESSION['y']) ) )
</pre>
There is no error, that is a Warning...
It is saying that $_SESSION['x'] was never setted, so, when you do that isset it tells you that it was never declared.
Example:
This gives you the same warning
empty($x);
This does not give you warning
$x = 0;
empty($x);
EDIT
I see this is a common question, so here is a better explanation.

PHP Error when check if URL Query = equals to [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I get an error when I try to check if a query taken from the url equals something.
Error: syntax error, unexpected T_IS_EQUAL, expecting ',' or ')'
if (isset($_GET['lang'] == 'eng')) {
echo 'ENG';
}else if (isset($_GET['lang'] == 'alb')) {
echo 'ALB';
}else {
echo 'MKD';
}
Am I doing something wrong?
Thanks
You can not use == and isset to gether:
if (isset($_GET['lang']) && $_GET['lang']=='eng') {
echo 'ENG';
}
else if (isset($_GET['lang']) && $_GET['lang'] == 'alb') {
echo 'ALB';
}else {
echo 'MKD';
}

php notice undefined index session [duplicate]

This question already has answers here:
Cannot access the session array with php
(3 answers)
Closed 8 years ago.
i have PHP Notice: Undefined index: langid in the error logs
and here is the code it's defined so any idea about what is wrong with my code
<?php
if(($_SESSION['langid'])!="" ){
$langid=$_SESSION['langid'];
$_SESSION['langid']=$langid;
if(isset($_REQUEST['langid']) && $_REQUEST['langid']!="" ){
$langid=$_REQUEST['langid'];
$_SESSION['langid']=$langid;
}else{
}
}else{
$langid=1;
$_SESSION['langid']=$langid;
if(isset($_REQUEST['langid']) && $_REQUEST['langid']!="" ){
$langid=$_REQUEST['langid'];
$_SESSION['langid']=$langid;
}else{
}
}
Try :
<?php
if ( isset($_SESSION['langid']) )
{
$langid=$_SESSION['langid'];
$_SESSION['langid']=$langid;
if(isset($_REQUEST['langid']) && $_REQUEST['langid']!="" ){
$langid=$_REQUEST['langid'];
$_SESSION['langid']=$langid;
}else{
}
}else{
$langid=1;
$_SESSION['langid']=$langid;
if(isset($_REQUEST['langid']) && $_REQUEST['langid']!="" ){
$langid=$_REQUEST['langid'];
$_SESSION['langid']=$langid;
}else{
}
}
?>
And you will will have no notices about warnings .but the code is no sense :
$langid=$_SESSION['langid'];
$_SESSION['langid']=$langid;
has no sense

Strict standards: Only variables should be passed [duplicate]

This question already has answers here:
Error message "Strict standards: Only variables should be passed by reference"
(6 answers)
Closed 8 years ago.
I have image upload script which shows this (Strict standards: Only variables should be passed by reference in C:\wamp\www\Fcomail-Final\cpanel\fcomail\Gallery.php on line 33
Call Stack) error Line no 33 is
require('../conn/include.php');
require('quick.php');
$query="SELECT * FROM category";
$result=mysql_query($query);
$project=$_POST['project'];
$alttext=$_POST['alttext'];
$relation=$_POST['Section'];;
if(isset($_FILES['image'])) {
$errors=array();
$allowed_ext=array('jpg','png','jpeg','JPG');
$filename=$_FILES['image']['name'];
$name=stripslashes($filename);
**Line 33** $type=strtolower(end(explode('.',$filename)));
$size=$_FILES['image']['size'];
$file_tmp=$_FILES['image']['tmp_name'];
if(in_array($type,$allowed_ext) ===false) {
$errors[]= "<span class=\"notification n-error\">Extenstion Not Allowed</span>";
}if($size > 1048576) {
$errors[]= "<span class=\"notification n-error\">File must be less then 2mb</span>";
}if(file_exists('../../images/fcomail/gallery/'.$filename)) {
$errors[]= "<span class=\"notification n-error\">File $filname Already Exists in directory</span>";
}if(empty($errors)) {
if(move_uploaded_file($file_tmp, '../../images/fcomail/gallery/'.$filename)) {
$insert="Insert into `my`.gallery(name,alttext,project,relation)VALUES('$name','$alttext','$project','$relation')";
//echo $insert;
$que=mysql_query($insert);
echo "<span class=\"notification n-success\">File $filname Uploaded Sucessfully</span>";
}
}else {
foreach($errors as $error) {
echo $error,'<br/>';
}
}
}
You need to break the below line
$type=strtolower(end(explode('.',$filename)));
to
$fname = explode('.',$filename);
$filename = end($fname);
$type=strtolower($filename);
Reason :
From the documentation of end() in PHP Manual.
The array. This array is passed by reference because it is modified by
the function. This means you must pass it a real variable and not a
function returning an array because only actual variables may be
passed by reference.

Categories