PHP注意:未定义的偏移量1

时间:2016-07-09 03:43:18

标签: php

我面临一个未定义偏移的问题:1。我无法理解它是什么类型的错误。任何人都可以告诉我为什么这样的错误发生在PHP

   <?php
 $host="localhost";
        $dbusername="root";
        $dbpassword="";
        $dbname="school";
        $conn=mysql_connect($host,$dbusername,$dbpassword) or die(mysql_error());
        mysql_select_db($dbname) or die(mysql_error());
if(isset($_POST['submit']))
{

$cls=$_POST['hhcls'];


{

    {

        {
            $query=mysql_query("select * from stu_class where class='$cls'",$conn);
            while($data=mysql_fetch_row($query))
            {
                $r1=$data[0];
                $r=$_POST[$r1]; //THIS LNE ERROR
                echo $r;
                $query1="update stu_class set rollno=".$r." where ad_no=".$r;
                $sql_query1=mysql_query($query1) or die(mysql_error());
                if($sql_query1)
                {
                    echo "<script type='text/javascript'>alert('Students Details Updated Successfully');</script>"; 
                }
                else
                {
                    echo "<script type='text/javascript'>alert('Updation Failed');</script>"; 
                }


            }           

        }

    }
}

  }
  ?>

我在将数据传递到另一个查询时遇到错误

1 个答案:

答案 0 :(得分:0)

当然)

查找

$query = mysql_query("select * from stu_class where class='$cls'", $conn);
// Now we got not query, but result of this query in $query

while ($data = mysql_fetch_row($query)) {
// Every iteration we will have in $data array (not associated) 
// of fields (next row from database)

$r1 = $data[0];
// After this line we have value of first column of current line in $r1
// usually this is integer ID field and if first ID in database is 1,
// then $r1 = 1

$r = $_POST[$r1]; //THIS LNE ERROR
// here you trying to set $r = $_POST[1] because $r1 = 1
// but you have no any POST variable with key 1.

当您尝试获取一些不存在的阵列成员时,您会收到通知&#34;未定义的偏移%键%&#34;。在严格模式下,您将获得例外。