【问题标题】:Passing array of data to other page using session in PHP使用PHP中的会话将数据数组传递到其他页面
【发布时间】:2015-07-04 03:19:00
【问题描述】:

我在使用会话在 Php 中传递数据数组时遇到了一些问题: 在我的课堂上,我有这个功能:

 public function check_dupID($id)
 {
     $this->stmt="select id from student where id='$id'";
     $this->res = mysql_query($this->stmt);
     $this->num_rows = mysql_num_rows($this->res);
     if($this->num_rows == 1)
     {
         while($this->row = mysql_fetch_array($this->res))
         {
             $dup_id = $this->row[0];
             return $dup_id;
         }
    }
}

此代码检查学生的重复 ID。现在,这样做的原因是我使用“.csv”文件将文件上传到数据库中。

在上传之前,我将 csv 文件中的所有数据放入一个数组中。现在,我一一检查: //创建用于检查的变量

$id = $check->check_dupID($array[0]);


if($id == TRUE)
{

    session_start();
    $_SESSION['id']; = $id;//passing it to the next back to array again. 
    $redirect->page($error);
}

现在,当它重定向时,它会给我一个“数组”的结果,如果我使用 foreach 解析它,它只会显示一个 id,但还有更多。我想知道如何全部显示。

【问题讨论】:

  • session_start(); 必须是文件中发生的第一件事。不完全确定这可以解决您的问题,但您可以从它开始。
  • 我试过但还是一样..
  • 您正在返回 $dup_itemcode,但我没有看到您的函数中设置的位置。
  • 很抱歉,应该返回 $dup_id。 .我将编辑代码..谢谢
  • 虽然我编辑了还是不行..

标签: php arrays session csv


【解决方案1】:

此代码在第一次找到时会返回重复的 id。如果要返回所有重复的 id,则使用 while 循环将它们添加到数组中,然后返回。 为此,请像这样修改您的代码:

    public function check_dupID($id)
 {
     $this->stmt="select id from student where id='$id'";
     $this->res = mysql_query($this->stmt);
     $this->num_rows = mysql_num_rows($this->res);
     $dup_id = array();// added here to prevent invalid argument supplied to foreach loop error if nothing is found
     if($this->num_rows >1)
     {

         while($this->row = mysql_fetch_array($this->res))
         {
            $dup_id[] = $this->row[0];// adding duplicates into the array

         }
         return $dup_id;
    }
}

【讨论】:

  • 谢谢。 .我试试这个,稍后我会回复你。谢谢
  • 还是一样,我只得到了一个结果而不是四个结果但是感谢您的帮助,我很感激。
  • 如果此语句在这里,它将如何返回多行 ** if($this->num_rows == 1) { ** 。编辑了我的答案。请立即检查。
  • 更好的问题是,如果查询只允许在每次调用函数时输入一个 ID,它如何返回多个 ID。
  • @BrianCohan 他正在尝试查找重复条目。具有相同 ID 的行。\
【解决方案2】:

这段代码解决了我的问题:

if($id == TRUE)
        {
        $get_id = array();
        $get_id[] = $id;
            session_start();
            $_SESSION['id']; = $get_id;//passing it to the next back to array again. 
            $redirect->page($error);
        }

【讨论】:

  • 作为 $id 传递给会话意味着我将它作为单个值传递。因此,我创建了一个名为 $get_id 的新数组,这意味着获取 $id 变量中的所有值,该变量是函数中的数组。接下来,我将 $get_id 传递给 SESSION 并对其进行解析,这解决了我的问题..
猜你喜欢
  • 1970-01-01
  • 2017-04-08
  • 2012-06-17
  • 2018-12-11
  • 2018-04-13
  • 2018-09-05
  • 2017-02-12
  • 1970-01-01
  • 2015-11-04
相关资源
最近更新 更多