dongxin8392 2016-07-29 01:07
浏览 35

$ _SERVER ['PHP_SELF']不起作用?

Everything works out just fine until I changed the below code from

    <form method='post' action='re.php'>

to

    <form method='post' action='<?php echo $_SERVER["PHP_SELF"];?>'>

It gives me HTTP error 500. I have done some googling but still have no idea why this is happening! Thanks so much for help!

Below is the whole script

<?php

$hostname = 'localhost';
$username = 'Ciara';
$password = 'mypass';
$database = 'users';        

$conn = mysqli_connect($hostname,$username, $password, $database);
if (!$conn) {
    die ("Databas connect failed: " . mysqli_connect_error());
} else {
    echo "Connected successfully. <br>";
}

echo <<<_END
<html>
    <head>
        <title>Register</title>
    </head>
    <body><center>
        <form method='post' action='<?php echo $_SERVER["PHP_SELF"];?>'>
        Firstname: <input type='text' name='firstname'><br>
        Lastname: <input type='text' name='lastname'><br>
        E-mail: <input type='text' name='email'><br>
        Gender: <input type='radio' value='female' name='gender'>Female
                <input type='radio' value='male' name='gender'>Male<br>
        Username: <input type='text' name='username'><br>
        Password: <input type='text' name='password'><br>
        <input type='submit' value='Submit Form'>
        </form></center>

_END;

if (isset($_POST['firstname']) && 
    isset($_POST['lastname']) &&
    isset($_POST['email']) &&
    isset($_POST['gender']) &&
    isset($_POST['username']) &&
    isset($_POST['password'])) {

    $firstname = get($conn, 'firstname');
    $lastname = get($conn, 'lastname');
    $email = get($conn, 'email');
    $gender = get($conn, 'gender');
    $username = get($conn, 'username');
    $password = get($conn, 'password');

    $query = "insert into useracc values" . "(null, '$firstname','$lastname','$email','$gender','$username','$password')";

    if (mysqli_query($conn, $query)) {
        echo "You have successfully registered! <br>";
    } else {
        echo "Failed to register." . mysqli_error($conn);
    }
}

mysqli_close($conn);
echo "</body></html>";

function get($conn, $var) {
     return mysqli_real_escape_string($conn, $_POST[$var]);
}
?>
  • 写回答

1条回答 默认 最新

  • duandao1931 2016-07-29 01:09
    关注

    You can't add PHP inside your HTML when you're using echo

    <?php
    
    $hostname = 'localhost';
    $username = 'Ciara';
    $password = '0950767';
    $database = 'users';        
    
    $conn = mysqli_connect($hostname,$username, $password, $database);
    if (!$conn) {
        die ("Databas connect failed: " . mysqli_connect_error());
    } else {
        echo "Connected successfully. <br>";
    }
    
    ?>
    <html>
        <head>
            <title>Register</title>
        </head>
        <body><center>
            <form method='post' action='<?php echo $_SERVER["PHP_SELF"];?>'>
            Firstname: <input type='text' name='firstname'><br>
            Lastname: <input type='text' name='lastname'><br>
            E-mail: <input type='text' name='email'><br>
            Gender: <input type='radio' value='female' name='gender'>Female
                    <input type='radio' value='male' name='gender'>Male<br>
            Username: <input type='text' name='username'><br>
            Password: <input type='text' name='password'><br>
            <input type='submit' value='Submit Form'>
            </form></center>
    
    <?php
    if (isset($_POST['firstname']) && 
        isset($_POST['lastname']) &&
        isset($_POST['email']) &&
        isset($_POST['gender']) &&
        isset($_POST['username']) &&
        isset($_POST['password'])) {
    
        $firstname = get($conn, 'firstname');
        $lastname = get($conn, 'lastname');
        $email = get($conn, 'email');
        $gender = get($conn, 'gender');
        $username = get($conn, 'username');
        $password = get($conn, 'password');
    
        $query = "insert into useracc values" . "(null, '$firstname','$lastname','$email','$gender','$username','$password')";
    
        if (mysqli_query($conn, $query)) {
            echo "You have successfully registered! <br>";
        } else {
            echo "Failed to register." . mysqli_error($conn);
        }
    }
    
    mysqli_close($conn);
    echo "</body></html>";
    
    function get($conn, $var) {
         return mysqli_real_escape_string($conn, $_POST[$var]);
    }
    ?>
    

    Check this code :).

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么