Storing values ​​in the database using php forms

advertisements

Final Edit: Thank you everyone for the help. I have been trying to write all the code related to connection in index.php rather than submit.php. It is resolved now.


Edit:

I have updated the code based on your feedback. I am able to get the values to the database now but the thing is it is showing only empty results. here is the updated code.

 <form action="submit.php" method="post" class="form-horizontal">
                        <div class="form-group">
                            <label for="name" class ="col-lg-2 control-label" > Name</label>
                            <div class="col-lg-7">
                                <input type="text" class="form-control" id ="name" name="name" placeholder="Enter your Name" required>
                            </div>
                        </div>

                </div>
             <div class="col-lg-1">

             </div>
                <div class="form-horizontal" >

                        <div class="form-group">
                            <label for="email" class ="col-lg-2 control-label" > Email</label>
                            <div class="col-lg-7">
                                <input type="text" class="form-control" id ="email" name="email" placeholder="Enter your email address" required>
                            </div>
                        </div>

                </div> <div class="col-lg-1">

             </div>
                <div class="form-horizontal" >

                        <div class="form-group">
                            <label for="subject" class ="col-lg-2 control-label" > Subject</label>
                            <div class="col-lg-7">
                                <input type="text" class="form-control" id ="subject" name="subject" placeholder="Your Subject" required>
                            </div>
                        </div>

                </div>
             <div class="col-lg-1">

             </div>
                <div class="form-horizontal">

                        <div class="form-group">
                            <label for="message" class ="col-lg-2 control-label" > Message</label>
                            <div class="col-lg-7">
                                <textarea name="message" class="form-control" id ="message" cols="20" rows="3" placeholder="Your Message"></textarea>
                            </div>
                        </div> <!-- end form -->
                        <div class="col-lg-1">

             </div>
                        <div class="form-group">
                            <div class="col-lg-7 col-lg-offset-2">
                            <button type="submit" name="submit" class="btn btn-primary">Submit</button>
                            </div>
                        </div>
                    </form>

PHP Code:

    <?php
    if (isset($_POST)) {
    $conn = mysqli_connect($servername, $username, $password, $db_name);//             Establishing Connection with Server
    mysqli_set_charset($conn, 'utf8');
    if (!$conn) {
    die("Database connection failed: " . mysqli_error($conn));
    }
    else
    echo "connected successfully";

//Escaping string, not 100% safe, also consider validating rules and sanitization
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $subject = mysqli_real_escape_string($conn, $_POST['subject']);
    $message = mysqli_real_escape_string($conn, $_POST['message']);
    $result = mysqli_query($conn, "INSERT INTO contact (user, email, subject, message) VALUES ('$name', '$email', '$subject', '$message')");

    }
    ?>

Here is the snapshot of the database


I have a form made using HTML. I want to store the results when i submit the form in the database. The connection was successful but the data is not being stored in the database.

Basically what submit.php does is just sent the text "Successfully submited the form".

Here's my code:

<form action="submit.php" method="post" class="form-horizontal">
                        <div class="form-group">
                            <label for="name" class ="col-lg-2 control-label" > Name</label>
                            <div class="col-lg-7">
                                <input type="text" class="form-control" id ="name" placeholder="Enter your Name" required>
                            </div>
                        </div>

                </div>
             <div class="col-lg-1">

             </div>
                <div class="form-horizontal" >

                        <div class="form-group">
                            <label for="email" class ="col-lg-2 control-label" > Email</label>
                            <div class="col-lg-7">
                                <input type="text" class="form-control" id ="email" placeholder="Enter your email address" required>
                            </div>
                        </div>

                </div> <div class="col-lg-1">

             </div>
                <div class="form-horizontal" >

                        <div class="form-group">
                            <label for="subject" class ="col-lg-2 control-label" > Subject</label>
                            <div class="col-lg-7">
                                <input type="text" class="form-control" id ="subject" placeholder="Your Subject" required>
                            </div>
                        </div>

                </div>
             <div class="col-lg-1">

             </div>
                <div class="form-horizontal">

                        <div class="form-group">
                            <label for="message" class ="col-lg-2 control-label" > Message</label>
                            <div class="col-lg-7">
                                <textarea name="message" class="form-control" id ="message" cols="20" rows="3" placeholder="Your Message"></textarea>
                            </div>
                        </div> <!-- end form -->
                        <div class="col-lg-1">

             </div>
                        <div class="form-group">
                            <div class="col-lg-7 col-lg-offset-2">
                            <button type="submit" class="btn btn-primary">Submit</button>
                            </div>
                        </div>
                    </form>

PHP code:

$conn = mysqli_connect($servername, $username, $password, $db_name);//         Establishing Connection with Server
mysqli_set_charset($conn, 'utf8');
if (!$conn) {
die("Database connection failed: " . mysqli_error($conn));
}
else
    echo "connected successfully";

if (isset($_POST['submit'])) {

//Escaping string, not 100% safe, also consider validating rules and sanitization
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$subject = mysqli_real_escape_string($conn, $_POST['subject']);
$message = mysqli_real_escape_string($conn, $_POST['message']);
$result = mysqli_query($conn, "INSERT INTO contact (user, email, subject, message) VALUES ('$name', '$email', '$subject', '$message');");
if ($result) {
$message="successfully sent the query!!";
}
else
{$message="try again!!";}
}
 ?>


None of your input fields have a name="" attribute, including the button. So none of these fields will be sent in the $_POST array.

Add a name="" attribute like this to all the fields you want sent to PHP

<form action="submit.php" method="post" class="form-horizontal">
   <div class="form-group">
       <label for="name" class ="col-lg-2 control-label" > Name</label>
       <div class="col-lg-7">
           <input type="text" class="form-control" id ="name" name="name" placeholder="Enter your Name" required>
       </div>
    </div>

    </div>
    <div class="col-lg-1">
        </div>
        <div class="form-horizontal" >
            <div class="form-group">
                <label for="email" class ="col-lg-2 control-label" > Email</label>
                <div class="col-lg-7">
                    <input type="text" class="form-control" id ="email" name="email" placeholder="Enter your email address" required>
                </div>
             </div>
        </div>
        <div class="col-lg-1"></div>
            <div class="form-horizontal" >
                <div class="form-group">
                   <label for="subject" class ="col-lg-2 control-label" > Subject</label>
                   <div class="col-lg-7">
                      <input type="text" class="form-control" id ="subject" name="subject" placeholder="Your Subject" required>
                   </div>
                </div>

         </div>
         <div class="col-lg-1"></div>
         <div class="form-horizontal">
             <div class="form-group">
                 <label for="message" class ="col-lg-2 control-label" > Message</label>
                 <div class="col-lg-7">
                     <textarea name="message" class="form-control" id ="message" name="message" cols="20" rows="3" placeholder="Your Message"></textarea>
                 </div>
             </div> <!-- end form -->
             <div class="col-lg-1"></div>
                <div class="form-group">
                   <div class="col-lg-7 col-lg-offset-2">
                      <button type="submit" class="btn name="submit" btn-primary">Submit</button>
                   </div>
                 </div>
       </form>

Also in your code in submit.php change this so you see an actual error message if one occurs.

if ($result) {
    $message="successfully sent the query!!";
} else {
    $message="Insert failed : " . mysqli_error($conn);
}
echo $message;

Although this does assume you are actually showing the $message value somewhere in your code that you have not shown us.