【发布时间】:2017-01-30 22:40:55
【问题描述】:
我有一个关于使用 PHP 和 html 表单在数据库中插入外键 ID 的快速查询。
comments 我的数据库设置如下:
Comment_ID PK
Name
Comment_Body
Recipe_ID FK
对于recipes:
Recipe_ID PK
Recipe_Name
Recipe_Description
Method
HTML/PHP:
*<input type="hidden" class="form-control" name="Recipe_ID" autofocus value=<?php echo $row["Recipe_ID"]; ?> readonly>
Name: <input type="text" class="form-control" name="name" required autofocus>
Comment: <input type="text" class="form-control" name="comment_body" required autofocus> </br>*
表单发帖脚本
if(isset($_POST["name"])){
$name = $_POST["name"];
$name = mysqli_real_escape_string($con, $name);
}
if(isset($_POST["comment_body"])){
$comment = $_POST["comment_body"];
$comment = mysqli_real_escape_string($con, $comment);
}
if(isset($_POST["Recipe_ID"])){
$rid = $_POST["Recipe_ID"];
$rid = mysqli_real_escape_string($con, $rid);
}
$sql = "INSERT INTO comment (name, comment_body, Recipe_ID) VALUES
('$name', '$comment', '$rid')";
PHP 变量和 SQL:
$id = $_GET["Recipe_ID"];*
*$sqlcomment = "SELECT * FROM comment WHERE Recipe_ID = '". $id . "'";
$commentsresult = mysqli_query($con, $sqlcomment);
$row = mysqli_fetch_assoc($commentsresult);*
查看该食谱时,将显示与Recipe_ID 1 相关的所有 cmets。
HTML 表单不会发布 Recipe_ID,因为在查看特定食谱页面时无法找到 ID。
如何使用 HTML 表单将特定 ID 发布到数据库?这是为我的设计中的各种配方创建 cmets 的合适方法吗?
非常感谢。
【问题讨论】:
-
表单应在隐藏输入中包含配方 ID。
-
为整理我之前的帖子干杯。真的不要用这么多!我的 PHP 页面设置 由具有动态 URL 的各种食谱组成的主页 --> 获取所选食谱 ID 的下一页 表单显示“只读”,仅此而已。我不确定我哪里出错了。
-
你需要循环调用
mysqli_fetch_assoc()来获取所有的cmets。 -
在问题中发布您的代码。见stackoverflow.com/help/formatting
-
既然您没有显示表单的代码,我怎么知道它为什么没有发布?