epskip
epskip

Reputation: 37

Storing the values into database using php forms

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!!";}
}
 ?>

Upvotes: 2

Views: 79

Answers (4)

Abhishek Sharma
Abhishek Sharma

Reputation: 6661

Try this code :-

<?php
if ($_SERVER['REQUEST_METHOD'] == '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')");

echo "INSERT INTO contact (user, email, subject, message) VALUES ('$name', '$email', '$subject', '$message')";//die;

if ($result) {
$message="successfully sent the query!!";
}
else
{$message="try again!!";}
}
?>

<form action="index.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" name ="name" 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" 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" name="subject" 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" name="submit">Submit</button>
                            </div>
                        </div>
                    </form>

Upvotes: 0

RiggsFolly
RiggsFolly

Reputation: 94682

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.

Upvotes: 1

Krishna Gupta
Krishna Gupta

Reputation: 685

First of all you must need to provide name attribute for each input tags and button tags for a better approach :

<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" name ="email" 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" name ="subject" 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" name ="message"  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 for Insert data in DB :

$result = mysqli_query($conn, "INSERT INTO contact (user, email, subject, message) VALUES ('".$name."', '".$email."', '".$subject."', '".$message."')");

Upvotes: 0

mario.van.zadel
mario.van.zadel

Reputation: 2949

You have to add name attribute to your button element so that if (isset($_POST['submit'])) will be true.

Please change

<button type="submit" class="btn btn-primary">Submit</button>

to

<button type="submit" name="submit" class="btn btn-primary">Submit</button>

or

<input type="submit" name="submit" value="Submit" class="btn btn-primary" />

Upvotes: 0

Related Questions