回声文本显示在错误的位置


Echoed text showing up in wrong place

我正在用PHP为一个类创建一个页面,当我回显内容时,它显示在错误的位置。

这是我的HTML页面

<html>
<head>
    <link rel="stylesheet" href="Site.css">
    <?php include("Header.php"); ?>
    </div>
</head>
<body>
    <div id="main">
        <h1>About</h1>
        <form action="Insert.php" method="post">
            <table>
                <tr>
                    <td><span>First name:</span></td>
                    <td><input type="text" name="firstname"></td>
                </tr>
                <tr>
                    <td><span>Last name:</span></td>
                    <td><input type="text" name="lastname"></td>
                </tr>
                <tr>
                    <td><span>Age:</span></td>
                    <td><input type="number" name="age"></td>
                </tr>
            </table>
            <input type="submit">
        </form>
        <?php include("Footer.php");?>
    </div> 
</body>
</html>

这是我的PHP页面:

<?php 
$con = mysql_connect("localhost","USERNAME","PASSWORD");
if(!$con) {
    die("could not connect to localhost:" .mysql_error());
}
mysql_select_db("a7068104_world") or die("Cannot connect to database");
header("refresh:1.5; url=NamesAction.php");
$firstname = mysql_real_escape_string($_POST['firstname']);
$lastname = mysql_real_escape_string($_POST['lastname']);
$fullname = mysql_real_escape_string($_POST['firstname'] . " " . $_POST['lastname']);
$age = mysql_real_escape_string($_POST['age']);
$query = "SELECT * FROM names_1 WHERE fullname='$fullname'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0 ){
        echo "Your name is already in the database and will not be added again!";
}
else {
        $query = "INSERT INTO names_1 (firstname, lastname, fullname, age) VALUES('$firstname', '$lastname', '$fullname', '$age')";
        $result = mysql_query($query);
        if($result) {
            echo "Your name was successfully added to the database!";
        }
        else{
            echo "Your name couldn't be added to the database!";
        }
}
mysql_close($con);
?>
<html>
<head>
    <link rel="stylesheet" href="Site.css">
    <?php include("Header.php"); ?>
    </div>
</head>
<body>
    <div id="main">
        <h1>Names</h1>
        <p>You will be redirected back to the <b>Names</b> page in a moment.</p>
        <?php include("Footer.php");?>
    </div>
</body>
</html>

当我在PHP页面中回显内容时,它会显示在正上方的框架顶部

<div id="main">

我想把回显的文本放在的最底部

<div id="main">

有什么办法我能做到吗?我感谢你的帮助!

谢谢,Leonardude

您的问题是,在提供HTML之前,您正在回声消息。这在这里很明显:

        if($result) {
            echo "Your name was successfully added to the database!";
        }
        else{
            echo "Your name couldn't be added to the database!";
        }

因为PHP是一种服务器端语言,HTML是客户端语言,所以PHP将在HTML之前处理好,这意味着它将在页面显示之前回显。因此,问题就出现在<div id="main"></div>之前。

解决方法是设置一个可变

        if($result) {
            $var = "Your name was successfully added to the database!";
        }
        else{
            $var = "Your name couldn't be added to the database!";
        }

<div id="main"></div>的某个地方,你可以做以下事情:

<div id="main">
<?php 
    if(isset($var) && !empty($var)) {
        echo $var;
    }
?>
</div>