AutoTester213
AutoTester213

Reputation: 2881

SQL - Insert form into a database

I have a php script to insert form input into a database called XXXX_comments into the Table called Comments that has Name and Comment as columns.When a user hits Save button the form should be inserted into the DB. I connect to the database using connect.php :

<?php
$servername = "localhost";
$username = "XXXXX";
$password = "XXXXX";
$Dbconnect = "XXX_comments";

// Create connection
$conn = new mysqli($servername, $username, $password);
mysqli_select_db($conn,$Dbconnect);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

in my index.php file i have the following form:

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

<?php               
        if(isset($_POST['Save'])){
        $sql = $conn->prepare("INSERT INTO Comments (Name, Comment) VALUES (?, ?)");
        $stmt->bind_param("ss", $firstname, $comment);
        $firstname = $_POST['firstname'];
        $comment = $POST['comment'];
        $stmt->execute();  
        Echo "Succefully inserted to table";
 }
 ?>
<div class="container">
  <div class="row" id="post-review-box" style="display:none;">
    <div class="col-md-12">
      <form id="form" accept-charset="UTF-8" action="index.php" method="post">
        <input type="text" class="form-control animated" id="firstname" type="hidden" placeholder="Enter your Name">
        <br>
        <input id="ratings-hidden" name="rating" type="hidden">
        <textarea class="form-control animated" cols="50" id="comment" placeholder="Enter your review here..." rows="5"></textarea>
        <br>
        <div class="text-right">
          <div class="stars starrr" data-rating="0"></div>
          <a class="btn btn-danger btn-sm" href="#" id="close-review-box" style="display:none; margin-right: 10px;">
            <span class="glyphicon glyphicon-remove"></span>Cancel</a>
          <button class="btn btn-success btn-lg" type="submit" name="Save">Save</button>
        </div>
      </form>

    </div>
  </div>

</div>
<div class="display"></div>
</div>
</div>
</div>

The connection is successful but the data is not being inserted.

Upvotes: 0

Views: 123

Answers (4)

AutoTester213
AutoTester213

Reputation: 2881

For any one intrested I have solved the issue, It seemed like the submit button wasn't responding So i added a javascript onclick function to force submit the form.

<input class="btn btn-default" onclick="myFunction()" value="Submit">
<script>
function myFunction() {
    document.getElementById("form").submit();
}
</script>   

Thanks for all the answers.

Upvotes: 0

MyLibary
MyLibary

Reputation: 1771

Few remarks:

First of all, in the connect.php file you're mixing OOP and Procedural styles, you can gain the same result by using the following code:

 $conn = new mysqli($servername, $username, $password, $Dbconnect);

Next, in your index.php, you've created a prepared statement, but you assigned that to $sql variable, and later you tried to use the undefined $stmt variable, so in order to fix that just change the first 2 lines

 $stmt = $conn->prepare("INSERT INTO Comments (Name, Comment) VALUES (?, ?)"); 
 $stmt->bind_param("ss", $firstname, $comment);

Hope it will solve your little issue!

Upvotes: 1

RAUSHAN KUMAR
RAUSHAN KUMAR

Reputation: 6004

You should give name to your inputs and textarea inside the form as

<input type="text" name="firstname" class="form-control animated" id="firstname" type="hidden" placeholder="Enter your Name">
<input id="ratings-hidden" name="rating" type="hidden">
<textarea class="form-control animated" cols="50" name="comment" id="comment" placeholder="Enter your review here..." rows="5"></textarea

and you are assigning values after the binding it to the query, you should assign values for $firstname and $comment before binding it to the query as

if(isset($_POST['Save'])){
    $firstname = $_POST['firstname'];
    $comment = $_POST['comment'];
    $sql = $conn->prepare("INSERT INTO Comments (Name, Comment) VALUES (?, ?)");
    $stmt->bind_param("ss", $firstname, $comment);    
    $stmt->execute();  
    echo "Succefully inserted to table";
}

Edit: The code was using $POST instead of $_POST.

Upvotes: 1

ln9187
ln9187

Reputation: 740

You need to assign values before you can use them as below code:

$firstname = $_POST['firstname'];
$comment = $_POST['comment'];
$sql = $conn->prepare("INSERT INTO Comments (Name, Comment) VALUES (?, ?)");
$stmt->bind_param("sss", $firstname, $comment);

Upvotes: 1

Related Questions