dongshuo1856 2018-01-25 03: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-25 04: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!!

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

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题