Jump to content

INSERT INTO


Ronel
 Share

Recommended Posts

Dear Sir/Madame

I am trying to insert comments into a table called comments using echo ID in the input form but so far i can insert the comments with the post id but unable re-comment again kindly help me what do i do? 

This is the input form:

 <div>
        <form action="ehscomment.php" method="post">
        <input type="hidden" name="id" value="<?php echo $id ?>"> 
            <div>
                <label>Add comment</label>
    <div>
        <textarea rows="6" cols="110" name="comment" placeholder="comment"></textarea>
    
        </div>
        </div>
            <input type="submit" name="postcomment" value="comment"></form>
</div> 

And this is ehscomment.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "registration";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
} 
if(isset($_POST['postcomment'])){
       $id = $_POST['id'];
       $comment = $_POST['comment'];
       

$sql = "INSERT INTO comments (id,comment)
VALUES ('$id','$comment')";

if ($conn->query($sql) === TRUE) {
   echo "New record created successfully";
} else {
   echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();}
?>

Error message: Error: INSERT INTO comments (id,comment) VALUES ('242','asddd')
Duplicate entry '242' for key 'PRIMARY'

Link to comment
Share on other sites

If you're trying to comment on an existing comment, you need a parent_id column in your table. The id of the initial comment would then go into that column.

For a more robust and future-proof solution, you'd create a separate table to track the parent/child relationships between comments, but the idea is the same.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.