Subrat Saxena
Subrat Saxena

Reputation: 78

Submitting fully filled form creates empty row in mysql

This is the form:

<form>
            <div class="form-group ">
                                    <label for="exampleInputFirstName">First Name</label>
                                    <input type="text" class="form-control" id="exampleInputFirstName" name="FirstName" placeholder="First Name">
                              </div>
                              <div class="form-group">
                                    <label for="exampleInputLastName">Last Name</label>
                                    <input type="text" class="form-control" id="exampleInputLastName" name="LastName" placeholder="Last Name">
                              </div>
                              <div class="form-group">
                                    <label for="exampleInputEmail1">Email address</label>
                                    <input type="email" class="form-control" id="exampleInputEmail1" name="Email" aria-describedby="emailHelp" placeholder="Enter Email">
                                    <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
                              </div>
                              <div class="form-group">
                                    <label for="exampleInputPassword">Password</label>
                                    <input type="password" class="form-control" id="exampleInputPassword" name="Password" placeholder="Password">
                              </div>
                              <div class="form-group">
                                    <label for="exampleInputPassword1">Confirm Password</label>
                                    <input type="password" class="form-control" id="exampleInputPassword1" name="confirmPassword" placeholder="Confirm Password">
                              </div>
                              <button type="submit" name="submit" class="btn btn-primary">Submit</button>
                        </form>

And this is the php:

<?php 
        $link = mysqli_connect("shareddb-i.hosting.stackcp.net", "LoginCredentials-3337b6db", "subrat410", "LoginCredentials-3337b6db");

        if (mysqli_connect_error()) {

            die ("There was an error connecting to the database");

        } 
    if(isset($_POST['submit'])) {
        $FirstName = $_POST['FirstName'];
        $LastName = $_POST['LastName'];
        $Email = $_POST['Email'];
        $Password = $_POST['Password'];
        $confirmPassword = $_POST['confirmPassword'];
    }
        $query ="INSERT INTO `Login-Credentials`( `FirstName`, `LastName`, `Email`, `Password`, `ConfirmPassword`) VALUES (' $FirstName ',' $LastName ',' $Email ',' $Password ',' $confirmPassword ')";

        // $query = "UPDATE `LoginCredentials` SET password = 'uedjUFH7^%' WHERE email = '[email protected]' LIMIT 1";

        mysqli_query($link, $query);
?>

Every time I fill my form and submit, another empty row gets created in my database. Any help will be appreciated. I've implemented php.ini file and its not showing any type of error. Also, while viewing page source I see Undefined Index Error in my php code where I am declaring $FirstName=$_POST('FirstName') . Thank you

Upvotes: 0

Views: 104

Answers (3)

Orchid Engr
Orchid Engr

Reputation: 48

Lovelace is right, maybe your $_POST variables are still not SET. Your form tag has no method, or action attributes

<form actions="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
    //the rest of your code is fine

</form>

Upvotes: 1

rez
rez

Reputation: 2087

“The <form> element defines how the data will be sent. All of its attributes are designed to let you configure the request to be sent when a user hits a submit button. The two most important attributes are action and method.” see more.

Your HTML code might be like:

<form actions=”[target url]” method=“POST”>
        <div class="form-group ">
             <label for="exampleInputFirstName">First Name</label>
             <input type="text" class="form-control" id="exampleInputFirstName" name="FirstName" placeholder="First Name">
        </div>
        <div class="form-group">
             <label for="exampleInputLastName">Last Name</label>
             <input type="text" class="form-control" id="exampleInputLastName" name="LastName" placeholder="Last Name">
        </div>
       <div class="form-group">
             <label for="exampleInputEmail1">Email address</label>
             <input type="email" class="form-control" id="exampleInputEmail1" name="Email" aria-describedby="emailHelp" placeholder="Enter Email">
             <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
       </div>
       <div class="form-group">
            <label for="exampleInputPassword">Password</label>
            <input type="password" class="form-control" id="exampleInputPassword" name="Password" placeholder="Password">
       </div>
       <div class="form-group">
            <label for="exampleInputPassword1">Confirm Password</label>
            <input type="password" class="form-control" id="exampleInputPassword1" name="confirmPassword" placeholder="Confirm Password">
       </div>
      <button type="submit" name="submit" class="btn btn-primary">Submit</button>
 </form>

Upvotes: 1

Tom G
Tom G

Reputation: 3650

Your form should have an action and a method defined, it's probably using GET and not POST:

<form action="your/php/file.php" method="post">

See here about form's defaulting to GET request (PHP $_GET variable would be useful if you go the GET request route): What is the default form HTTP method?

In your php, it's usually always better to use $_REQUEST["propertyName"] than either $_POST["propertyName"] or $_GET["propertyName"] since it can roll with either method type

Also, you should probably put your query in the if statement to prevent empty rows.

Finally, you can try the following php lines to see what's going on:

var_dump($_REQUEST);
var_dump($_GET);
var_dump($_POST);

Let me know what you find and we'll keep helping you resolve

Upvotes: 0

Related Questions