Ayaw ako makapag insert

loktoy

Established
ito error na lumalabas
Fatal error: Uncaught mysqli_sql_exception: Cannot add or update a child row: a foreign key constraint fails (test.comments, CONSTRAINT fk_comments_post_id FOREIGN KEY (post_id) REFERENCES posts (id)) in C:\xampp\htdocs\comments-and-replies-php-mysql\index.php:15 Stack trace: #0 C:\xampp\htdocs\comments-and-replies-php-mysql\index.php(15): mysqli_query(Object(mysqli), 'INSERT INTO com...') #1 {main} thrown in C:\xampp\htdocs\comments-and-replies-php-mysql\index.php on line 15

ito code ko

$conn = mysqli_connect("localhost", "root","","test");
if (isset($_POST["post_comment"]))
{
$name = mysqli_real_escape_string($conn, $_POST["name"]);
$email = mysqli_real_escape_string($conn, $_POST["email"]);
$comment = mysqli_real_escape_string($conn, $_POST["comment"]);
$post_id = mysqli_real_escape_string($conn, $_POST["post_id"]);
$reply_of = 0;
mysqli_query($conn, "INSERT INTO comments(name, email, comment, post_id, created_at, reply_of) VALUES ('" . $name . "', '" . $email . "', '" . $comment . "', '" . $post_id . "', NOW(), '" . $reply_of . "')");
echo "<p>Comment has been posted.</p>";
}
 
The error message you're seeing indicates that there is a foreign key constraint violation when trying to insert a new row into the comments table. Specifically, the foreign key constraint fk_comments_post_id is being violated.

This constraint is defined to ensure that the post_id value being inserted into the comments table is a valid reference to an existing row in the posts table.

To resolve this issue, you need to make sure that the post_id value you're trying to insert into the comments table exists in the posts table.

Here are a few possible reasons for the error:

1. The post_id value you're trying to insert does not exist in the posts table. You can check if the post_id value is correct and corresponds to an existing row in the posts table.

2. The posts table does not have the id column defined as the primary key. The foreign key constraint requires that the referenced column (id in this case) is the primary key of the referenced table (posts). Make sure the id column in the posts table is defined as the primary key.

3. There might be some records in the comments table that have invalid post_id values, which violate the foreign key constraint. You can check the existing records in the comments table and make sure their post_id values are valid.

It's also worth mentioning that using mysqli_real_escape_string is not sufficient to prevent SQL injection. It's recommended to use prepared statements or parameterized queries to ensure the security of your application.

If you're still having trouble, please provide more information about your database schema and the values you're trying to insert.
 

About this Thread

  • 2
    Replies
  • 311
    Views
  • 2
    Participants
Last reply from:
vhin le

Online now

Members online
994
Guests online
1,080
Total visitors
2,074

Forum statistics

Threads
2,276,161
Posts
28,967,972
Members
1,231,145
Latest member
kennethCASTRO2012
Back
Top