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

图片转代码服务由CSDN问答提供 功能建议

我有两个不同的文件处理这个 我有一个post.php文件(url看起来像post.php ?id =(number))处理父消息页面上的表单

 &lt; form id =“chardiv”action =“comments.php”method =  “post”&gt; 
Name:&lt; br&gt; 
&lt; input type =“text”name =“mod_name”required&gt;&lt; br&gt; 
 
消息:&lt; br&gt; 
 
&lt; textarea class ='  autoExpand' rows ='3'data-min-rows ='3'placeholder ='Auto-Expanding Textarea'name =“topic”required&gt;&lt; / textarea&gt; 
&lt; input type =“submit”value =“Submit”  &gt;&lt; br&gt; 
&lt; / form&gt; 
   
 
 

以及html表单发布的第二个文件“comments.php”

 &lt;?php 
require'connect.php'; 
 $ conn = Connect(); 
 $ id =(int)$ _ GET ['id']; 
 $ comment =  $ conn-&gt; real_escape_string($ _ POST ['topic']); 
 $ date = time(); 
 $ query =“INSERT into anon_comments(post_id,Date,comment)VALUES('”。$ id。“  ','“。$ date。”','“。$ comment。”')“; 
  $ success = $ conn-&gt; query($ query); 
 
if(!$ success){
 die(“无法输入数据:”。$ conn-&gt;错误); 
} 
  $ conn-&gt; close(); 
header('Location:/index.php');
?>
nn

处理发布到 数据库

我不确定如何从表单所在页面向数据库发送外键(parent id)。 < 某人可以给予帮助 谢谢!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题