所以我正在尝试在WordPress中使用自定义表单。我安装了一个Insert PHP插件,因此[insert_php]
与下面脚本中的<?php
相同。
基本上,我看到的插件都没有我需要的东西,所以我用PHP创建了自己的插件,当第64行的if语句应该运行时,它会重新启动,我完全不知道为什么。
我已经设置了会话变量,如果有什么不同的话,我的所有表单都使用POST方法。
脚本:
<div id="mcoc_form">
[insert_php]
session_start();
$_SESSION['name'] = $_POST['username'];
$_SESSION['stars3'] = $_POST['tstar'];
$_SESSION['stars4'] = $_POST['fstar'];
$_SESSION['points3']= $_POST['tpoints'];
$_SESSION['rank3'] = $_POST['trank'];
$_SESSION['points4']= $_POST['fpoints'];
$_SESSION['rank4'] = $_POST['frank'];
$_SESSION['name_set'] = isset($_SESSION['name']);
$_SESSION['stars3_set'] = isset($_SESSION['stars3']);
$_SESSION['stars4_set'] = isset($_SESSION['stars4']);
$_SESSION['points3_set']= isset($_SESSION['points3']);
$_SESSION['rank3_set'] = isset($_SESSION['rank3']);
$_SESSION['points4_set']= isset($_SESSION['points4']);
$_SESSION['rank4_set'] = isset($_SESSION['rank4']);
if (!function_exists('threeFunction')) {
function threeFunction($conn, $userID, $threeRank, $threePoints) {
if ($threeRank !== 0 && $threePoints !== 0) {
$sql = "INSERT INTO `3star` (playerID, rank, points, arenaID) VALUES (?, ?, ?, 6)";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, "iiii", $userID, $threeRank, $threePoints);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
}
}
}
if (!function_exists('fourFunction')) {
function fourFunction($conn, $userID, $fourRank, $fourPoints, $fourChamps) {
if ($fourRank !== 0 && $fourPoints !== 0) {
$sql = "INSERT INTO `4star` (playerID, rank, points, arenaID) VALUES (?, ?, ?, 6)";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, "iiii", $userID, $fourRank, $fourPoints);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
}
}
}
if (!$_SESSION['name_set']) {
echo '<form method="post" id="form">';
echo '<label>Player Username: <input type="text" name="username" required /></label>';
echo '<label>I participated in the arena for 3-star Winter Soldier: <input type="checkbox" name="tstar" /></label>';
echo '<label>I participated in the arena for 4-star Winter Soldier: <input type="checkbox" name="fstar" /></label>';
echo '<br />';
echo '<input type="submit" />';
echo '</form>';
} else {
if ($_SESSION['stars3_set'] && !$_SESSION['points3_set']) {
echo '<form method="post" id="form">';
echo '<h3>3* Winter Soldier Arena</h3>';
echo '<label>How many <span class="bold">points</span> did you gain in the arena?<br />	<input type="number" name="tpoints" required /></label>';
echo '<label>What did you <span class="bold">rank</span> with the aforementioned points?<br />	<input type="number" name="trank" required /></label>';
// echo '<input type="hidden" name="username" /><input type="checkbox" name="tstar" checked style="display:none" />';
echo '<br />';
echo '<input type="submit" />';
echo '</form>';
} else if ($_SESSION['stars4_set'] && !$_SESSION['points4_set']) {
echo '<form method="post" id="form">';
echo '<h3>4* Winter Soldier Arena</h3>';
echo '<label>How many <span class="bold">points</span> did you gain in the arena?<br />	<input type="number" name="tpoints" required /></label>';
echo '<label>What did you <span class="bold">rank</span> with the aforementioned points?<br />	<input type="number" name="trank" required /></label>';
// echo '<input type="hidden" name="username" /><input type="checkbox" name="fstar" checked style="display:none" />';
echo '<br />';
echo '<input type="submit" />';
echo '</form>';
} else {
$server = 'server';
$database = 'database';
$user = 'user';
$pass = 'password';
$conn = mysqli_connect($server, $user, $pass, $database);
if (!$conn) {
die('Connection failed: ' . mysqli_connect_error());
}
$sql = "SELECT id FROM names WHERE name = '" . $_SESSION['name'] . "'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$userID = mysqli_fetch_assoc($result)['id'];
threeFunction($conn, $userID, $_SESSION['rank3'], $_SESSION['points3']);
fourFunction($conn, $userID, $_SESSION['rank4'], $_SESSION['points4']);
} else {
$sql = "INSERT INTO names (name) VALUES ('$username')";
if (mysqli_query($conn, $sql)) {
$userID = mysqli_insert_id($conn);
threeFunction($conn, $userID, $_SESSION['rank3'], $_SESSION['points3']);
fourFunction($conn, $userID, $_SESSION['rank4'], $_SESSION['points4']);
}
}
mysqli_close($conn);
echo "<p>Thank you, " . $_SESSION['name'] . ", for helping gather information for a more comprehensive feel as to what goes on behind the curtains.</p>";
}
}
[/insert_php]
</div>
只是为了更容易发现,第64行是
} else if ($_SESSION['stars4_set'] && !$_SESSION['points4_set']) {
有人能告诉我为什么它不会进入if语句,而是重新开始吗?
当您提交第二个表单时,没有$_POST['username'],因此此行将$_SESSION['name']重新设置为NULL/FALSE:
$_SESSION['name'] = $_POST['username'];
然后,此行将$_SESSION['name_set']设置为FALSE
$_SESSION['name_set'] = isset($_SESSION['name']);
这就是为什么你会回到第一个表单。你只需要一个围绕$_POST变量的IF语句:
if (isset($_POST['username'])) $_SESSION['name'];
这将阻止$_SESSION['name']被覆盖。