【问题标题】:Inserting information in database with PHP [duplicate]使用 PHP 在数据库中插入信息 [重复]
【发布时间】:2018-04-27 20:42:44
【问题描述】:

我是初学者,我正在努力学习 PHP 和 SQL。我已经编写了下面的代码,所以我可以将数据插入到我的数据库中,但我不明白为什么它不起作用。它加载了所有内容,但是当我单击“注册”按钮时,它什么也不做。有人可以帮我弄清楚吗?

<?php include 'config.php'; ?>

<?php
        if(isset($_POST['submit'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];


            $query = "INSERT INTO users1(username,password) VALUES ('$username','$password')";
            $result = mysqli_query($con,$query) or die ("problem inserting new product into database");
        }
    ?>

<!DOCTYPE html>
<html>
<head>
<title>Sign Up</title>

<style>


button {
    text-align:center;
    color: gray;
}
</style>

</head>

<body>
<h2>Sign Up</h2>
<h3>Enter your data to register</h3>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">

    <label class="user-name">Username</label><input type="text" name="username" placeholder="Enter your username" autofocus required"><br><br>
    <label class="pass-word"> Password: </label><input type="password" name="pass" class="info" placeholder="Enter your Password" required ><br><br>

    <button class="lbutton" type="submit" value="Submit">Sign Up</button>
    <p>Already registered? <a href="login2.php">Login</a></p>

</body>
</html>

【问题讨论】:

标签: php css sql database html


【解决方案1】:

首先

如果表单将您重定向到同一页面,您可以将action 参数留空:action=""

其次

您检查变量$_POST['submit'] 是否存在,但表单中的所有元素都没有名称submit,因此它永远不会存在。 Html 表单使用字段名称作为键将变量发送到数组$_POST。如果没有给出名字,它是一个数字。

做什么

name="submit" 添加到您的提交button 中应该可以工作。

将密码input中的name="pass"更改为name="password"或将php中的$_POST['password']更改为$_POST['pass']

同时关闭您的表单 (&lt;/form&gt;)。现代浏览器会自动关闭它,但它仍然不正确。

其他提示

直接访问$_POST 不是一个好习惯。这是一个更好的解决方案:$username = filter_input(INPUT_POST, 'username') 而不是 $username = $_POST['username']

在向 SQL 查询插入变量时,使用filter_var 函数也是一个好主意。 示例:$username = filter_var(FILTER_SANITIZE_STRING, $username)

【讨论】:

    【解决方案2】:

    尝试关闭表单。

      <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" 
      method="POST">
    
     <label class="user-name">Username</label><input type="text" name="username" 
     placeholder="Enter your username" autofocus required"><br><br>
    <label class="pass-word"> Password: </label><input type="password" 
    name="pass" class="info" placeholder="Enter your Password" required ><br> 
    <br>
    
     <button class="lbutton" type="submit" value="Submit">Sign Up</button>
     <p>Keni nje adrese? <a href="login2.php">Futu</a></p>
    </form>
    

    【讨论】:

    • 不能。没有name='submit'的字段
    【解决方案3】:

    如果你这样做,提交按钮应该有一个名称,它将包含在 post 数组中。

    <button class="lbutton" type="submit" name="submit" value="Submit">Sign Up</button>
    

    在提交表单时,会为表单内具有有效名称属性的所有元素创建 post 数组。

    你还需要关闭&lt;form&gt;&lt;/form&gt;

    【讨论】:

      猜你喜欢
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多