0

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 = 'robpercival80@gmail.com' 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

Subrat Saxena
  • 68
  • 1
  • 9
  • 1
    check if your $_POST variables are set and not empty, do something along these lines: `$FirstName = (isset($_POST['FirstName']) && !empty($_POST['FirstName'])) ? $_POST['FirstName'] : false;` only run query if (insert if condition) you don't receive any 'false' values. – lovelace Jun 22 '18 at 19:24
  • Hello Sir, I have applied code as stated above.Now atleast its not inserting empty rows in my database. My problem is that I have written php and html code in the same file index.php and now I am facing a situation where i couldn't send my form data to php variables. Please help – Subrat Saxena Jun 23 '18 at 06:10

3 Answers3

1

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>
  • Hello Sir, my problem is that I have written php and html code in the same file index.php and now I am facing a situation where i couldn't send my form data to php variables. Please help – Subrat Saxena Jun 23 '18 at 06:15
1

“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>
amrezzd
  • 1,787
  • 15
  • 38
  • Hello Sir, my problem is that I have written php and html code in the same file index.php and now I am facing a situation where i couldn't send my form data to php variables. Please help – Subrat Saxena Jun 23 '18 at 06:13
  • @SubratSaxena just put the php form handing code above the html form and check if $POST[‘submit’] is set. Remember to put all your php form handling codes inside an if structure to prevent empty row creation. – amrezzd Jun 23 '18 at 08:39
  • Did it the same way and now its working. Thank you :) – Subrat Saxena Jun 23 '18 at 10:33
0

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

Tom G
  • 3,650
  • 1
  • 20
  • 19
  • Hello sir, I have corrected empty rows problem by setting all the php variables. My problem is that I have written php and html code in the same file index.php and now I am facing a situation where i couldn't send my form data to php variables. Please help – Subrat Saxena Jun 23 '18 at 06:12
  • Have you changed
    to
    ? Can you update your question with the output of after you submit the form?
    – Tom G Jun 23 '18 at 06:16
  • Yes Sir, i have changed action and its working fine now. i had to create another file for all php. Although i could have kept all code at one place by just putting action="" but felt safe by separating codes. Thank you so much. – Subrat Saxena Jun 23 '18 at 07:45