Ronel Posted April 9, 2020 Share Posted April 9, 2020 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' Quote Link to comment https://forums.phpfreaks.com/topic/310512-insert-into/ Share on other sites More sharing options...
chhorn Posted April 9, 2020 Share Posted April 9, 2020 so what now? you are sending the ID twice. _If you want to insert, use some other ID. if you want to replace, use an UPDATE statement. Quote Link to comment https://forums.phpfreaks.com/topic/310512-insert-into/#findComment-1576584 Share on other sites More sharing options...
maxxd Posted April 9, 2020 Share Posted April 9, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310512-insert-into/#findComment-1576585 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.