0

For some reason, this form doesn't insert into my database.

Html

<form action="../php/register.php" method="post">
    <div id ="personal-form">
        <h4><b>Personal Details:</b></h4>
        <hr>

        <div class="form-group">
            <label class="sr-only" for="first-name">First name</label>
            First Name

            <input type="text" name="firstname" placeholder="" 
                   class="form-control" id="firstname">

            <button type="submit" class="btn btn-next" id="submit">
                Submit
            </button>
        </center>
        </div>
    </div>
</form>

php/register.php

<?php
    include('connect.php');

    if(isset($_POST["submit"])) {
        $firstname = $_POST["firstname"];
        $stmt = $conn->prepare("INSERT INTO storeowners (firstname) VALUES 
(:firstname)");

        $stmt->bindParam(':firstname', $firstname);
        $stmt->execute();

        header("location: next.php");
    }
?>

This is connect.php

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=blaza", $username, 
$password);
        //set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "success";
    } catch(PDOException $e) {
        echo "Connection failed: " . $e->getMessage();
    }
?>

When I click on submit button, it shows the php/register.php page with the message success which is thesame message in the connect.php code if db connection was successful. I dont know where the problem is cause it doesnt store the firstname to the database and no error was given.

2
  • for="first-name" & id="firstname" do not match. Commented Jun 4, 2018 at 12:40
  • you have a closing </center> tag with no opening one (not a php issue but an issue) Commented Jun 4, 2018 at 12:46

3 Answers 3

5
if(isset($_POST["submit"]))

You have no form controls with name=submit so this condition will never be true.

You connect to the database unconditionally, but you never use the connection to do anything.

Sign up to request clarification or add additional context in comments.

Comments

1

Add name="submit" to your button.

<form action="../php/register.php" method="post">
      <div id ="personal-form">
        <h4><b>Personal Details:</b></h4>
        <hr>
        <div class="form-group">
           <label class="sr-only" for="first-name">First name</label>
              First Name
           <input type="text" name="firstname" placeholder="" class="form-control" id="firstname">
           <button name="submit" type="submit" class="btn btn-next" id="submit">Submit</button></center>
       </div>
     </div>
 </form>

Comments

0

Instead of $_POST["submit"] add this

if(isset($_POST["firstname"]))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.