$ _SERVER [' PHP_SELF'] -Xampp错误

时间:2016-02-09 17:50:51

标签: php mysqli xampp

<html>
<body>
<h1>NewsLetter Registration Time! </h1>
<form action='$_SERVER["PHP_SELF"]' method='post'>
Enter the username to be added:
<input type="text" id="nt1" name="username"/>
Enter the corresponding email-id to be added:
<input type="text" id="nt1" name="email_id"/>
<input type="submit"/>

<?php

if(isset($_POST['submit']))
{
    echo "<h1 style='color:red;'> Entering Data..... </h1>";
    $database=mysqli_connect('localhost','root','','userdetails')
or die("didn't work");

mysqli_query($database,"INSERT INTO userdetails (username,password)VALUES ($_POST[username],$_POST[email_id])");

mysqli_close($database);
echo "<h3 style='color:red;'> Check the database..... </h3>";
}
?>

</body>
</html>

我无法使用$_SERVER['PHP_SELF']访问数据库,但如果我使用其他一些PHP脚本,则代码可以正常运行。这是xampp的问题吗?

错误:enter image description here

1 个答案:

答案 0 :(得分:1)

您的表单中缺少php标记,而提交按钮中缺少type=submit。另外,检查manual是否使用php

在数据库中插入数据
<html>
    <body>
    <h1>NewsLetter Registration Time! </h1>
    <form action='<?php $_SERVER["PHP_SELF"];?>' method='post'>
    Enter the username to be added:
    <input type="text" id="nt1" name="username"/>
    Enter the corresponding email-id to be added:
    <input type="text" id="nt1" name="email_id"/>
    <input type="submit" name="submit"/>

    <?php

    if(isset($_POST['submit']))
    {
        echo "<h1 style='color:red;'> Entering Data..... </h1>";
        $database=mysqli_connect('localhost','root','','userdetails')
    or die("didn't work");

    mysqli_query($database,"INSERT INTO userdetails (username,password)VALUES ('$_POST[username]','$_POST[email_id]')");

    mysqli_close($database);
    echo "<h3 style='color:red;'> Check the database..... </h3>";
    }
    ?>

    </body>
    </html>