一旦页面第二次加载,我的会话变量才会更改为正确的值。 由于第一次加载页面时正确设置了其他会话变量(matchid),因此显示此行为很奇怪。 如果(审核编号),则设置不正确的变量,它设置在代码段的底部。
代码
我为长代码段道歉,但我不知道错误在哪里:
if ($stmt = $dbc->prepare("SELECT matchid, user1, user2, user1_accept, user2_accept FROM matches WHERE user1_accept = ? or user2_accept = ? LIMIT 1")) { $stmt->bind_param('ii', $id, $id); // Bind id to parameter. $stmt->execute(); // Execute the prepared query. $stmt->store_result(); // get variables from result. $stmt->bind_result($matchid, $user1, $user2, $user1_accept, $user2_accept); $stmt->fetch(); $num_rows = mysqli_stmt_num_rows($stmt); if ($num_rows == 0){header('Location: /nomatches.php');} $_SESSION['matchid'] = $matchid; } print_r($_SESSION); if ($user1 != $id){ echo 'user 1 !='; $reviewnumber = 'user2_accept'; echo $reviewnumber; if ($stmt = $dbc->prepare("SELECT aboutme, friend, picture FROM userprofile WHERE id = ? LIMIT 1")) { $stmt->bind_param('i', $user1); // Bind id to parameter. $stmt->execute(); // Execute the prepared query. $stmt->store_result(); // get variables from result. $stmt->bind_result($aboutme, $friend, $picture); $stmt->fetch(); $picture = implode('/', array_map('rawurlencode', explode('/', $picture))); } if ($stmt = $dbc->prepare("SELECT full_name FROM users WHERE id = ? LIMIT 1")) { $stmt->bind_param('i', $user1); // Bind id to parameter. $stmt->execute(); // Execute the prepared query. $stmt->store_result(); // get variables from result. $stmt->bind_result($full_name); $stmt->fetch(); } } if ($user2 != $id){ echo 'user 2 !='; $reviewnumber = 'user1_accept'; echo $reviewnumber; if ($stmt = $dbc->prepare("SELECT aboutme, friend, picture FROM userprofile WHERE id = ? LIMIT 1")) { $stmt->bind_param('i', $user2); // Bind id to parameter. $stmt->execute(); // Execute the prepared query. $stmt->store_result(); // get variables from result. $stmt->bind_result($aboutme, $friend, $picture); $stmt->fetch(); $picture = implode('/', array_map('rawurlencode', explode('/', $picture))); } if ($stmt = $dbc->prepare("SELECT full_name FROM users WHERE id = ? LIMIT 1")) { $stmt->bind_param('i', $user2); // Bind id to parameter. $stmt->execute(); // Execute the prepared query. $stmt->store_result(); // get variables from result. $stmt->bind_result($full_name); $stmt->fetch(); } } $_SESSION["reviewnumber"] = $reviewnumber;变量:reviewnumber,在第一页加载时正确回显,这就是为什么我不明白为什么会话变量没有正确设置的原因。
My session variable only changes to the correct value once the page has been loaded for the second time. This behaviour exhibited is weird due to the fact that the other session variable (matchid) is set correctly the first time that the page is loaded. The variable that is not setting properly if (review number), it is set at the bottom of the code snippet.
The Code
I apologise for the long code snippet, however I have no idea where the error is:
if ($stmt = $dbc->prepare("SELECT matchid, user1, user2, user1_accept, user2_accept FROM matches WHERE user1_accept = ? or user2_accept = ? LIMIT 1")) { $stmt->bind_param('ii', $id, $id); // Bind id to parameter. $stmt->execute(); // Execute the prepared query. $stmt->store_result(); // get variables from result. $stmt->bind_result($matchid, $user1, $user2, $user1_accept, $user2_accept); $stmt->fetch(); $num_rows = mysqli_stmt_num_rows($stmt); if ($num_rows == 0){header('Location: /nomatches.php');} $_SESSION['matchid'] = $matchid; } print_r($_SESSION); if ($user1 != $id){ echo 'user 1 !='; $reviewnumber = 'user2_accept'; echo $reviewnumber; if ($stmt = $dbc->prepare("SELECT aboutme, friend, picture FROM userprofile WHERE id = ? LIMIT 1")) { $stmt->bind_param('i', $user1); // Bind id to parameter. $stmt->execute(); // Execute the prepared query. $stmt->store_result(); // get variables from result. $stmt->bind_result($aboutme, $friend, $picture); $stmt->fetch(); $picture = implode('/', array_map('rawurlencode', explode('/', $picture))); } if ($stmt = $dbc->prepare("SELECT full_name FROM users WHERE id = ? LIMIT 1")) { $stmt->bind_param('i', $user1); // Bind id to parameter. $stmt->execute(); // Execute the prepared query. $stmt->store_result(); // get variables from result. $stmt->bind_result($full_name); $stmt->fetch(); } } if ($user2 != $id){ echo 'user 2 !='; $reviewnumber = 'user1_accept'; echo $reviewnumber; if ($stmt = $dbc->prepare("SELECT aboutme, friend, picture FROM userprofile WHERE id = ? LIMIT 1")) { $stmt->bind_param('i', $user2); // Bind id to parameter. $stmt->execute(); // Execute the prepared query. $stmt->store_result(); // get variables from result. $stmt->bind_result($aboutme, $friend, $picture); $stmt->fetch(); $picture = implode('/', array_map('rawurlencode', explode('/', $picture))); } if ($stmt = $dbc->prepare("SELECT full_name FROM users WHERE id = ? LIMIT 1")) { $stmt->bind_param('i', $user2); // Bind id to parameter. $stmt->execute(); // Execute the prepared query. $stmt->store_result(); // get variables from result. $stmt->bind_result($full_name); $stmt->fetch(); } } $_SESSION["reviewnumber"] = $reviewnumber;The variable: reviewnumber, is correctly echoed on the first page load, which is why I don't understand why the session variable is not correctly set.
最满意答案
会话值在更改会话值之前打印 - 因此新会话值仅在下一页加载时显示。 如果其他人有类似的问题,这绝对是一个重要的事情要检查。
The session values were being printed before the session value was being changed - therefore the new session value was only being shown on the next page load. If anybody else has a similar problem, this is definitely an important thing to check.
更多推荐
发布评论