会话无法在锚标签中工作

时间:2018-05-05 06:00:00

标签: javascript php

我将会话传递到循环中的下一页,其中我得到$ post_id的不同值,但它似乎取1值,而不是更改。

<?php
include('dataconnect.php');
$conn = new mysqli($servername, $username, $password, $dbname); 
if ($conn->connect_error) {
    die("Connection failed: " .$conn->connect_error);
}                   
$sql = "SELECT * FROM user";
$result = $conn->query($sql);
if ($result->num_rows > 0) { 
    while($row = $result->fetch_assoc()) { 
        $post_id = "{$row['id']}"; 
        ?>

        <a href="comment.php?<?php $_SESSION['user_id'] = intVal($post_id);  ?>" >Comment</i></a>

    <?php } 
}  
$conn->close(); 
?>

2 个答案:

答案 0 :(得分:0)

在脚本开头使用 session_start()功能。

<?php
session_start();
include "dataconnect.php";
// do stuff...
?>

答案 1 :(得分:0)

我改变了这个,希望这有帮助!

 <a href="comment.php?session=<?php echo $_SESSION['user_id'] = intVal($post_id);  ?>" >Comment</i></a>