【问题标题】:I tried different codes but data from the form does not insert in database tables我尝试了不同的代码,但表单中的数据没有插入到数据库表中
【发布时间】:2021-03-17 03:39:18
【问题描述】:

我尝试了对其他人有用的各种代码,即使现在这些代码也没有显示任何类型的语法错误,但来自 html 表单的信息不会插入到数据库中,而是从相同或相似的代码创建表。我可以请有人指导我哪里出错了。下面给出的是我目前使用的代码:

    <?php require_once("../includes/db.php"); ?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
        <title>SIGN UP || Admin Panel</title>
        <link href="css/styles.css" rel="stylesheet" />
        <link rel="icon" type="image/x-icon" href="assets/img/favicon.png" />
        <script data-search-pseudo-elements defer src="js/all.min.js"></script>
        <script src="js/feather.min.js"></script>
    </head>
    <body class="bg-primary">
        <div id="layoutAuthentication">
            <div id="layoutAuthentication_content">
                <main>
                    <?php
                        if(isset($_POST['submit'])) {
                          $First_Name = trim($_POST['first-name']);
                            $Last_Name = trim($_POST['last-name']);
                            $Mobile_No = trim($_POST['mobile-no']);
                            $Email_id = trim($_POST['email-address']);
                            $User_Name = trim($_POST['user-name']);
                            $Password = trim($_POST['password']);
                            $confirm_password = trim($_POST['confirm-password']);
                            if($Password != $confirm_password) {
                                $error = "Password doesn't match";
                            }
                            else {
                                $sql = "INSERT INTO user (User_Name, First_Name, Last_Name, Mobile_No, Email_id, Password)
                                       VALUES (:username :fname, :lname, :mobile, :email, :password)";
                                $stmt = $pdo->prepare($sql);
                                $stmt->execute([
                                    ':username' => $User_Name,
                                    ':fname' => $First_Name,
                                    ':lname' => $Last_Name,
                                    ':mobile' => $Mobile_No,
                                    ':email' => $Email_id,
                                    ':password' => $Password,
                                ]);
                            }
                        }
                    ?>

                    <div class="container">
                        <div class="row justify-content-center">
                            <div class="col-lg-7">
                                <div class="card shadow-lg border-0 rounded-lg mt-5">
                                    <div class="card-header justify-content-center"><h3 class="font-weight-light my-4">Create Account</h3></div>
                                    <div class="card-body">
                                        <form action="signup.php" method="POST">
                                            <?php
                                                if(isset($error)) {
                                                    echo "<p class='alert alert-danger'>{$error}</p>";
                                                }
                                            ?>
                                            <div class="form-row">
                                                <div class="col-md-6">
                                                    <div class="form-group">
                                                        <label class="small mb-1" for="inputFirstName">First Name</label>
                                                        <input name="first-name" class="form-control py-4" id="inputFirstName" type="text" placeholder="Enter first name" required="true" />
                                                    </div>
                                                </div>
                                                <div class="col-md-6">
                                                    <div class="form-group">
                                                        <label class="small mb-1" for="inputLastName">Last Name</label>
                                                        <input name="last-name" class="form-control py-4" id="inputLastName" type="text" placeholder="Enter Last Name" required="true" />
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="form-group"><label class="small mb-1" for="inputUsername">Username</label>
                                                <input name="user-name" class="form-control py-4" id="inputUsername" type="text" placeholder="Enter Username" required="true" />
                                            </div>
                                            <div class="form-group"><label class="small mb-1" for="inputMobileNo">Mobile No.</label>
                                                <input name="mobile-no" class="form-control py-4" id="inputMobileNo" type="text" placeholder="Enter Mobile No" required="true" />
                                            </div>
                                            <div class="form-group"><label class="small mb-1" for="inputEmailAddress">Email</label>
                                                <input name="email-address" class="form-control py-4" id="inputEmailAddress" type="email" aria-describedby="emailHelp" placeholder="Enter email address" required="true" />
                                            </div>
                                            <div class="form-row">
                                                <div class="col-md-6">
                                                    <div class="form-group"><label class="small mb-1" for="inputPassword">Password</label>
                                                        <input name="password" class="form-control py-4" id="inputPassword" type="password" placeholder="Enter password" required="true" />
                                                    </div>
                                                </div>
                                                <div class="col-md-6">
                                                    <div class="form-group"><label class="small mb-1" for="inputConfirmPassword">Confirm Password</label>
                                                        <input name="confirm-password" class="form-control py-4" id="inputConfirmPassword" type="password" placeholder="Confirm password" required="true" />
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="form-group mt-4 mb-0">
                                                <button name="submit" class="btn btn-primary btn-block" type="submit">Create Account</button>
                                            </div>
                                        </form>
                                    </div>
                                    <div class="card-footer text-center">
                                        <div class="small">
                                            <a href="signin.php">Have an account? Go to signin</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </main>
            </div>
        </div>

        <!--Script JS-->
        <script src="js/jquery-3.4.1.min.js"></script>
        <script src="js/bootstrap.bundle.min.js"></script>
        <script src="js/scripts.js"></script>
        </body>
        </html>

【问题讨论】:

  • 你在哪里定义 $pdo 变量?似乎未定义
  • 我已将其包含在 db.php 文件中,其代码为:` getMessage(); } ?>`
  • 双重检查 $sql 和 db 中的列名匹配,我有一个拼写错误的列名并且没有抛出错误。如果它们看起来没问题,将该查询复制到 Mysql Workbench,添加一些虚拟数据作为值并运行它以查看它执行是否正常或有错误
  • 我确实没有拼错列名,但是当我尝试在 sql 中运行查询时,它抛出了错误:INSERT INTO 联系人(用户名、电子邮件、评论)值(JohnDoe,jd@gmail.com,完成) 错误代码:1064。 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 2 行 0.000 秒处的“@gmail.com,done)”附近使用正确的语法
  • 将值放在引号中('JohnDoe'、'jd@gmail.com'、'done'),看看是否有帮助

标签: php html css sql pdo


【解决方案1】:

试试:

    <?php require_once("../includes/db.php"); ?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
        <title>SIGN UP || Admin Panel</title>
        <link href="css/styles.css" rel="stylesheet" />
        <link rel="icon" type="image/x-icon" href="assets/img/favicon.png" />
        <script data-search-pseudo-elements defer src="js/all.min.js"></script>
        <script src="js/feather.min.js"></script>
    </head>
    <body class="bg-primary">
        <div id="layoutAuthentication">
            <div id="layoutAuthentication_content">
                <main>
                    <?php
                        if(isset($_POST['submit'])) {
                          $First_Name = trim($_POST['first-name']);
                            $Last_Name = trim($_POST['last-name']);
                            $Mobile_No = trim($_POST['mobile-no']);
                            $Email_id = trim($_POST['email-address']);
                            $User_Name = trim($_POST['user-name']);
                            $Password = trim($_POST['password']);
                            $confirm_password = trim($_POST['confirm-password']);
                            if($Password != $confirm_password) {
                                $error = "Password doesn't match";
                            }
                            else {
                                $sql = "INSERT INTO user (`User_Name`, `First_Name`, `Last_Name`, `Mobile_No`, `Email_id`, `Password`)
                                       VALUES (':username',':fname', ':lname', ':mobile', ':email', ':password')";
                                $stmt = $pdo->prepare($sql);
                                $stmt->execute([
                                    ':username' => $User_Name,
                                    ':fname' => $First_Name,
                                    ':lname' => $Last_Name,
                                    ':mobile' => $Mobile_No,
                                    ':email' => $Email_id,
                                    ':password' => $Password,
                                ]);
                            }
                        }

作为对@kmoser 的回应,我在要添加的数据周围添加了''

【讨论】:

  • 你改变了什么?请描述您所做的不同之处,以便读者受益。
猜你喜欢
  • 2015-08-25
  • 1970-01-01
  • 1970-01-01
  • 2018-02-15
  • 1970-01-01
  • 2021-12-16
  • 2022-01-15
  • 2013-06-22
  • 1970-01-01
相关资源
最近更新 更多