【问题标题】:$_SERVER['PHP_SELF'] -Xampp Error$_SERVER['PHP_SELF'] -Xampp 错误
【发布时间】:2016-05-19 20:02:25
【问题描述】:
<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的问题吗?

错误:

【问题讨论】:

  • 你忘记了你的 PHP 标签
  • @JohnConde 加了php标签还是不行!
  • &lt;form action='$_SERVER["PHP_SELF"]' method='post'&gt; 那是因为您不在 PHP 中使用它 - 只需执行 action=''。并且if(isset($_POST['submit'])){...} 中的所有内容都不会启动。
  • 那你也会失败($_POST[username],$_POST[email_id])

标签: php mysqli xampp


【解决方案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>

【讨论】:

  • 现在我访问了数据库,但没有将值输入到表中。
  • name=submit 丢失//
  • 是的。我还有一个疑问 "INSERT INTO userdetails (username,email_id) VALUES ('{$_POST['username']}','{$_POST['email_id']}')"); 有效,但这 "INSERT INTO userdetails (username,email_id) VALUES ('($_POST['username']','($_POST['email_id'])')"); 无效。唯一的区别是大括号。
  • 你应该查看手册。我在答案中附上了一个链接。另外,请投票/接受答案,以便对其他人也有帮助。
猜你喜欢
  • 2019-04-01
  • 2014-07-04
  • 1970-01-01
  • 2019-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-22
  • 1970-01-01
相关资源
最近更新 更多