Stanley Roy Chilton
Stanley Roy Chilton

Reputation: 71

inserting an item into a database using a foreign key

I have two different files that deal with this I have a post.php file (url looks like post.php?id=(number)) that deals with the form on the page of the parent message

<form id="chardiv" action="comments.php" method="post">
Name:<br>
<input type="text" name="mod_name" required><br>

Message:<br>

<textarea class='autoExpand' rows='3' data-min-rows='3' placeholder='Auto-Expanding Textarea' name="topic" required></textarea>
<input type="submit" value="Submit"><br>
</form>

and a second file "comments.php" which the html form posts too

<?php
require 'connect.php';
$conn    = Connect();
$id    = (int)$_GET['id'];
$comment   = $conn->real_escape_string($_POST['topic']);
$date    = time();
$query   = "INSERT into anon_comments (post_id,Date,comment) VALUES('" . $id . "','" . $date . "','" . $comment . "')";
$success = $conn->query($query);

if (!$success) {
    die("Couldn't enter data: ".$conn->error);
}
$conn->close();
header('Location: /index.php');
?>

which deals with the posting to the database

I'm unsure how I would go about sending the foreign key (parents id) to the database from the page the form is on.

can someone give aid Thanks!

database

result on the webpage

Upvotes: 0

Views: 59

Answers (1)

prakash tank
prakash tank

Reputation: 1267

You are doing wrong here. you have to pass your id in : <input type="hidden" name="id" value="YOUR VALUE" />

and on comments.php page get the value by $id = (int)$_POST['id'];

Hope it helps!!

Upvotes: 1

Related Questions