0

因此,当我在显示的表单上点击提交时,页面(如果工作正常)应该刷新,并且应该显示 ELSE 语句,但是我有两个问题

在我手动刷新页面之前不会显示 else 语句 Pub Score 在手动刷新页面之前不会更新,我认为我的代码放置可能是导致它的原因,但我试图将我的表单尽可能地放低,我没有想法,任何帮助都会非常感谢。

<?php
require_once('header.php');
require_once('connectdb.php');
require_once('sessioncheck.php');
if (isset($_SESSION['user_id'])) {
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_DATA);

$user_name = mysqli_real_escape_string($dbc, trim($_GET['username']));

$query = "SELECT * FROM blah WHERE username = '$user_name'";

$data = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($data); 

if (mysqli_num_rows($data) != 0) {


if ($row['havemic'] == 1) {
    $micstatus = "Yes";
} else {
    $micstatus = "No";
}
?>




<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title><?php echo $user_name . ' profile' ?></title>
</head>

<body>

<?php 
$commenduser = $user_name;
$query = "SELECT * FROM blah where commenduser = '$commenduser'";
$data = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($data);
$lowerusername = strtolower($username);
$loweruser_name = strtolower($user_name);
    if (mysqli_num_rows($data) == 0) {
    if (isset($_POST['submit'])) {


        $commendplayer = mysqli_real_escape_string($dbc, trim($_POST['commendplayer']));
        $commend = mysqli_real_escape_string($dbc, trim($_POST['commend']));
        $comment = mysqli_real_escape_string($dbc, trim($_POST['comment']));

        if (empty($comment)) {
            echo '<p class="error">Please fillout a comment before submitting</p>';
        } else {

        $query = "INSERT INTO commend (commendby, commenduser, comment) VALUES ('$username', '$user_name', '$comment')";
        mysqli_query($dbc, $query);


        if ($commend == true) {
            $query = "UPDATE blah SET points=points+1 WHERE username='$user_name'";
            mysqli_query($dbc, $query);
            echo '<p class="success">Your commendation has been submitted with + 1 account points.</p>';
        } else {
            echo '<p class="success">Your commendation has been submitted with no affect on the users account points.</p>';
        }


    }
}
}  else {
    echo '<p class="success">You have already submitted a commendation for this player.</p>';
}
?>


<div id="accsettings">
<table cellpadding="5">

<tr><td><label for="username" class="reglabel">Username: </label></td>
<td><label for="username" class="reglabel"><?php echo $row['username']; ?></label></td></tr>
<tr><td><label class="reglabel">Pub Score: </label></td><td><label class="reglabel">

/*This value 'points' should be updated to the new value after form submit */
/*As well the ELSE statement near the bottom should be displayed*/
<?php echo $row['points'] ?></label></td></tr>
<tr><td><label for="steamname" class="reglabel">Steam Name: </label></td>
<td><label for="steamname" id="acclink"><?php echo '<a href="http://steamcommunity.com/id/' . $row['steamname'] . '">' . $row['steamname'] . '</a>'; ?></label>
<tr><td><label for="favchar" class="reglabel">Prefered Hero: </label></td>
<td><label for="favchar" class="reglabel"><?php echo $row['favchar']; ?></label></td></tr>
<tr><td><label for="language" class="reglabel">Spoken Language: </label></td>
<td><label for="language" class="reglabel"><?php echo $row['language']; ?></label></td></tr>
<tr><td><label for="playernote" class="reglabel">Player Note: </label></td>
<td><label for="playernote" class="reglabel"><?php echo $row['note']; ?></label></td></tr>
<tr><td><label for="micstatus" class="reglabel">Has a Mic and VO-IP?</label></td>
<td><label for="micstatus" class="reglabel"><?php echo $micstatus; ?></label></td></tr>

<tr><td colspan="2">Players Comments</td></tr>


<?php
if ($row['commendby'] != $username && $lowerusername != $loweruser_name) {
?>
<tr><td><br></td></tr>
<tr><td colspan="2"><p class="success">Player Commendations/Comments</p></td></tr>
<tr><td><br></td></tr>


<form method="post" action="<?php echo $_SERVER['PHP_SELF'] . '?username=' . $user_name; ?>">
<tr><td><label for="comment">Leave a comment</label></td>
<td><input type="text" name="comment" class="regtext" /></td></td>
<tr><td colspan="2"><label for="commend" class="right">Commend Player?</label><input type="checkbox"  class="right" name="commend" value="yes" /></td></tr>
<tr><td colspan="2"><input id ="submit" type="submit" class="button1" name="submit" value="Submit" /></td></tr>
</form>

<?php
} else {
/*This is what should be being displayed after the form is submitted. But it is not.*/
    $query = "SELECT * FROM blah where commenduser = '$commenduser'";
    $data = mysqli_query($dbc, $query);
    while($row = mysqli_fetch_array($data)) {
        echo '<tr><td><br></td></tr>';
        echo '<tr><td><br></td></tr>';
        echo '<tr><td><label  class="reglabel" for="commendedbyy">Comment From: ' . $row['commendby'] . '</label></td>';
        echo '<td><label class="reglabel">' . $row['comment'];
        echo '<input type="hidden" name="submit" />';
        echo '</form>';


    }

}
?>  

</table>

<?php
} else {
    echo '<p class="error">' . $user_name . ' is not a registered account.</p>';
}
}
else {
    echo '<p class="error">You must <a href="login.php">Log In</a> to view this profile.</p>';
}
?>

</div>
</body>
</html>
<?php
require_once('footer.php');
?>
4

1 回答 1

0

首先$row从数据库中获取,然后更新数据库。

您可以做三件事之一,最后一件是最简单的:

  1. 您可以重构代码以更改顺序
  2. 更新后使用另一个查询重新加载数据
  3. 一旦你更新了点然后更新数组(即做$row['points'] = $row['points'] + 1;
于 2013-01-30T00:46:15.730 回答