dongshuo1856 2018-01-24 19:59
浏览 96
已采纳

使用外键将项插入数据库

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

展开全部

  • 写回答

1条回答 默认 最新

  • doudun1029 2018-01-24 20:34
    关注

    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!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?