如何将行变量传递到另一页?

时间:2019-03-02 03:57:38

标签: php

<?php
    if (isset($_POST['submit'])) {
        $flag=0;
        $con = new mysqli('localhost', 'root', '', 'sconnect');
        $location = $con->real_escape_string($_POST['location']);
        $data="SELECT * FROM events WHERE location='$location';";
        $resmy=mysqli_query($con,$data);
        while($row = mysqli_fetch_array($resmy)) { 
            $gid = $row['event_id'];
            $gquery ="select * from events where event_id='$gid';";
            $myresq = mysqli_query($con,$gquery);
            $grow = mysqli_fetch_array($myresq);
            echo "<tr><td>".$row['event_id']."</td><td>".$grow['gname']." 
            </td><td>".$grow['location']."</td><td>".$grow['date_from']." 
            </td><td>".$grow['date_to']."</td><td>".$grow['venue_details']." 
            </td><td>".$grow['participants']."</td>
            <td><form action='joineventphp.php' method='POST'><input 
            type='hidden' name='event_id' value='$row['event_id']'/><input 
            type='submit' name='submit' value='Register' /></form></td> 
            </tr>";
            $flag=1;
          }
           if(!$flag)
             {
                 echo "<tr><td>No events</td><td>found.</td></tr>";
             }
    }
?>

单击注册后,我希望将“ $ row ['event_id']”变量发送到另一页。在稍后的页面joineventphp.php中,我希望将其存储在数据库中。该怎么做?

1 个答案:

答案 0 :(得分:1)

代替

<input type='hidden' name='event_id' value='$row['event_id']'/>

<input type='hidden' name='event_id' value='{$row['event_id']}'/>

例如,您可能在其中存在语法或解析错误(我们必须首先对此达成共识)

 //this is a very simplified version of what you are doing
 //you have a double quoted string your putting an array access variable into
 //I have the same thing, agree?
 $foo = ['a' => 'b'];
 echo "some $foo['a'] thing";

输出

<br />
<b>Parse error</b>:  syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in <b>[...][...]</b> on line <b>5</b><br />

Sandbox

执行此操作时:

    echo "some {$foo['a']} thing";
    //or
    echo "some ".$foo['a']." thing";

那消失了,一切都按预期进行。 PHP仅插值$var这样的简单变量,因此上述两种方式都使PHP知道“此物”内的所有内容都是变量。

在看这部分内容之前,应该更加清楚:

  <td>".$row['event_id']."</td>

这是我上面提到的两种方法之一,您只是错过了这一方法。