0

I tried various codes which worked for others, even now the codes do not show any sort of syntax errors but the information from html form does not get inserted into database, but table gets created from same or similar code. Can I please someone guide me on where Im going wrong. The give below is the code Im currently using :

    <?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>
  • Where do you define $pdo variable? It seems undefined – user2463644 Dec 05 '20 at 08:41
  • I have included that in db.php file and its code is: ` getMessage(); } ?>` – Akaaqua Dec 05 '20 at 12:51
  • Double check column names match in $sql and db, I had a misspelled column name and there was no error thrown. If they seem ok, copy that query into Mysql Workbench, add some dummy data as values and run it to see if it executes fine or with errors – user2463644 Dec 05 '20 at 14:49
  • I did column names were not misspelled but when I tried to run the query in sql it threw error : INSERT INTO contact (Username, Email, Comments) VALUES (JohnDoe, jd@gmail.com, done) **Error Code: 1064.** You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '@gmail.com, done)' at line 2 0.000 sec – Akaaqua Dec 05 '20 at 18:22
  • Put values in quotes ('JohnDoe', 'jd@gmail.com', 'done') and see if it helps – user2463644 Dec 05 '20 at 20:56
  • thank you it helped with putting quotes but when I tried to insert from html form it does not works, after filling in the details and as I click submit the webpage loads to a blank page instead of echoing successful or any error. – Akaaqua Dec 06 '20 at 12:39
  • Instead of inserting try selecting from that table, just to see if there's a db connection. For example $sql = 'SELECT * FROM user WHERE id=1'; $stmt = $pdo->prepare($sql); $stmt->execute(); $result = $stmt->fetch(PDO::FETCH_ASSOC); print_r($result); – user2463644 Dec 06 '20 at 16:37
  • You should be checking the return value of `$stmt->execute()`, as well as [checking for any PDO errors](https://stackoverflow.com/questions/8776344/how-to-view-query-error-in-pdo-php). – kmoser Aug 31 '21 at 04:17

1 Answers1

0

Try:

    <?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,
                                ]);
                            }
                        }

In response to @kmoser, I had added '' around the data which is to be added.

Mr. Programmer
  • 95
  • 1
  • 10