将一个变量从Page1传递到Page2



//第1页-下面的代码工作正常,但当我点击链接的href//我想要的变量没有发送到第2页。

<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo '<tr>
            <td><a href="recipe_show.php?recipe_name=recipe_name"> ' . $row['recipe_name'] . ' </a></td>    
        </tr>';
    $recipe_name = $row['recipe_name']; 
    }
$_SESSION['recipe_name'] = $recipe_name;
    echo '</table>'; // Close the table
?>
// Page2 - Code below receives the variable from page 1, but only the    //last one in the table and not the one I clicked. 
include ('core/init.php'); // Connect to the database
$recipe_name = $_SESSION['recipe_name'];
    echo "My recipes is: ".$recipe_name."<br>";
 ?>

使用get请求尝试这样做。由于用户可以查看/更改数据,因此这不是最安全的方法,但可以完成任务。这种技术不涉及会话。

<?php
   while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
       echo '<tr><td>
             <a href="recipe_show.php?recipe_name='.$row['recipe_name'].'"> ' . $row['recipe_name'] . ' </a></td></tr>'; 
    }
    echo '</table>';
?>

//Page2

 <?php
    $recipe_name = $_GET['recipe_name'];
    echo "My recipes is: ".$recipe_name."<br>";
 ?>

你的第二页写错了

使用$_GET["];要从url中获取值,我们使用$_GET.

:

$recipe_name = $_GET['recipe_name'];

希望这对你有帮助。

最新更新