【问题标题】:One form writes to db other does not一种形式写入数据库,另一种没有
【发布时间】:2020-12-04 23:47:30
【问题描述】:

我有两个相似的表格,都写入同一个数据库,但不同的表。 使用相同类型的字段,varchar。

当我在两个表单上发布数据时,我都会收到一条成功的消息,假设只有在数据成功写入数据库后才会出现。使用以下教程创建了我的第一个表单:“https://www.youtube.com/watch?v=LC9GaXkdxF8&t=4482s”。试图基于它创建一个注册和登录系统,但数据没有写入数据库。

一种形式(一种来自视频)正在将数据写入数据库,另一种则不是。不知道是什么原因。 有人知道可能是什么问题吗?

工作并写入数据库的表单:

    <form action="includes/signup.inc.php" method="post">
        <input type="text" name="uid" placeholder="Username">
        <input type="text" name="mail" placeholder="E-mail">
        <input type="password" name="pwd" placeholder="Password">
        <input type="password" name="pwd-repeat" placeholder="Repeat Password">
        <button type="submit" name="signup-submit">Signup</button>
        <?php

    //if an error occurs 
    if (isset($_GET['error'])) {
        if ($_GET['error'] == 'emptyfields') {
            echo '<p>Fill in all the fields!</p>';
        }
        else if ($_GET['error'] == 'invaliduidmail') {
            echo '<p>Invalid username and email</p>';
        }
        else if ($_GET['error'] == 'invaliduid') {
            echo '<p>Invalid username</p>';
        }
        else if ($_GET['error'] == 'invalidmail') {
            echo '<p>Invalid email</p>';
        }
        else if ($_GET['error'] == 'passwordcheck') {
            echo '<p>Passwords do not match</p>';
        }
        else if ($_GET['error'] == 'usertaken') {
            echo '<p>Username is alread used!</p>';
        }
    }
    //if succesfull         
    else if (isset($_GET['signup']) == 'success'){
        echo '<p>signup succesfull!!!</p>';
    }

     ?>
    </form>

下面的表格似乎不起作用。 (请注意,当我添加了更多字段时,我确实对 PHP 文件 include/signup.inc.php 做了一些小改动) 由于某种原因,以下标记未出现在堆栈溢出中

"form action="includes/signup.inc.php" class="form-signin" method="post"" 但我确实添加了它,所以引用它以查看它是否可见。

        <h1 class="h3 mb-3 font-weight-normal">Create Account</h1>
        <label for="inputUserName" class="sr-only">User Name</label>
        <input name="userName" type="text" id="inputUserName" class="form-control" placeholder="User Name" >
        
        <label for="inputFullName" class="sr-only">Full Name</label>
        <input name="FullName" type="text" id="inputFullName" class="form-control" placeholder="Full Name" >

        <label for="inputAge" class="sr-only">Age</label>
        <input name="Age" type="text" id="inputAge" class="form-control" placeholder="Age" >
        
        <label for="inputEmail" class="sr-only">Email address</label>
        <input name="Email" type="text" id="inputEmail" class="form-control" placeholder="Email" >
        
        <label for="inputPhoneNumber" class="sr-only">Phone Number</label>
        <input name="Contact" type="tel" id="inputPhoneNumber" class="form-control" placeholder="Phone Number" >
        
        <label for="inputPassword" class="sr-only">Password</label>
        <input  name="pwd" type="password" id="inputPassword" class="form-control" placeholder="Password" >

        <label for="inputPassword" class="sr-only">Confirm Password</label>
        <input name="ConfirmPwd" type="password" id="inputConfirmPassword" class="form-control" placeholder="Confirm Password" >
        
        <button type="submit" name="signup-submit" class="btn btn-lg btn-primary btn-block" >Register</button>
        <p class="text-center">Already Registered? <a href="SignIn.php">Sign in here</a> </p>  
    <?php
    //check for errors on the FORM
    //if an error occurs 
    if (isset($_GET['error'])) {
        if ($_GET['error'] == 'emptyfields') {
            echo '<p>Fill in all the fields!</p>';
        }
        else if ($_GET['error'] == 'invaliduidmail') {
            echo '<p>Invalid username and email</p>';
        }
        else if ($_GET['error'] == 'invaliduid') {
            echo '<p>Invalid username</p>';
        }
        else if ($_GET['error'] == 'invalidmail') {
            echo '<p>Invalid email</p>';
        }
        else if ($_GET['error'] == 'passwordcheck') {
            echo '<p>Passwords do not match</p>';
        }
        else if ($_GET['error'] == 'usertaken') {
            echo '<p>Username is alread used!</p>';
        }
    }
    //if succesfull
    else if (isset($_GET['signup']) == 'success'){
        echo '<p>Register was succesfull!!!</p>';
    }

     ?>
    </form>

【问题讨论】:

  • 欢迎来到 Stack Overflow。第二种表格的代码不完整 - 请编辑您的问题以在 minimal,reproducible example 中包含所有相关代码,以便我们提供帮助 - 我们还需要查看处理表格的代码,因为这可能是问题所在。还请包括任何错误消息的详细信息(在屏幕上、日志中等)以及究竟出了什么问题的详细信息 - 当我们不知道它是什么时,很难帮助解决问题:)

标签: php css mysql forms


【解决方案1】:

您尚未在第二个表单的顶部添加&lt;form action="includes/signup.inc.php" method="post"&gt;

【讨论】:

  • 不知道为什么它似乎被剪掉了,但你在我的表格上有这个:
  • 我在表单中添加了它,但由于某种原因它没有显示在 stackover 流上。会发送上下文图像,但不确定如何上传图像。当我编辑以下是第二种形式:
    在我这边我看到了
猜你喜欢
  • 2010-10-06
  • 1970-01-01
  • 2012-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
  • 2023-04-01
  • 1970-01-01
相关资源
最近更新 更多