未定义的索引变量PHP

时间:2013-10-11 06:54:00

标签: php get indexing undefined

好吧,所以这似乎很常见,但我读过的内容都没有帮助过我,所以有谁知道为什么我的变量似乎没有被设置好。

编辑:抱歉忘记了错误的地方,它在第169行,即:

if($world['$queryi'] != 0)

<?php

  if(isset($_GET['upgrade']))
  {
  if($_GET['upgrade'] > 0 && $_GET['upgrade'] < 15){ 

  $_GET['upgrade'];
  $id = $_GET['upgrade'];
  $queryi = "column_$id"; // This shows as undefined.
  $querye = "column_name_$id"; // This shows as undefined

  //When printing them out alone, they are defined, with the value i need them to be.

  if($id>=1 && $id <=14)
  {
//$world[] <- array from outside of the $_GET area (ive tried having it inside, same error)

   if($world['$queryi'] != 0)
   {

   }
   else
   {

       echo "query turned out zero";
   }

  }
  else
  {
      echo "something went wrong.";
  }
 }
 }

?>

//EDIT this is the arrayQUery which seems to be causing the problem..
$query = "SELECT * from this WHERE userid='".$user['id']."'";

$result = mysql_query($query);
$world=mysql_fetch_array($result);

任何人都有线索,哪有错?

2 个答案:

答案 0 :(得分:1)

更改

$_GET['upgrade'];
$id = $_GET['upgrade'];
$queryi = "column_$id"; // This shows as undefined.
$querye = "column_name_$id"; // This shows as undefined

if(isset($_GET['upgrade'])) { 
    $id = $_GET['upgrade'];
} else {
    $id = 1;
}

$queryi = "column_" . $id; // This shows as undefined.
$querye = "column_name_" . $id; // This shows as undefined

应该做的伎俩。

此行也需要检查是否已设置..我认为这会导致错误..:

if($world['$queryi'] != 0)

将其更改为:

if(isset($world[$queryi]) && $world[$queryi] != 0)

答案 1 :(得分:0)

根据您的评论,我发现您的代码存在以下问题

为什么在这一行if($world['$queryi'] != 0)中用单引号编写$ queryi。单引号意味着字符串不是真实的。所以你的代码意味着,搜索索引'$ queryi'。但你想要做的是,你想搜索这个字符串index =&gt; "column_name_$id"。因此,要么删除单引号,要么将其更改为双引号。

所以正确的代码将是

if($world[$queryi] != 0)