[英]How do I get POST data without knowing the id?
I have a script that prints out divs in a loop.我有一个循环打印 div 的脚本。 I need each one to have a submit button to POST data that's specific to that div.
我需要每个人都有一个提交按钮来发布特定于该 div 的数据。
How do I get the ID of the post so I can submit it to the database?如何获取帖子的 ID,以便将其提交到数据库?
HTML in PHP echo ($t is incremented for each loop): PHP echo中的HTML(每个循环增加$ t):
<form id="foo'.$t.'">
<input id="bar'.$t.'" name="bar" type="text" value="'.$link.'" />
<input type="submit" value="Report as broken" >
</form>
I tried to get the id of the post by treating it like an array or using var_dump, but was unsuccessful.我试图通过将其视为数组或使用 var_dump 来获取帖子的 id,但没有成功。
PHP script: PHP脚本:
if ($_POST) {
// prepare
$stmt = $con->prepare("INSERT INTO reports (link) VALUES (?)");
// set parameters
$a = isset($_POST) ? $_POST : null;
// bind
$stmt->bind_param("s", $a);
// run
$stmt->execute();
$stmt->close();
}
Add a hidden field with post id:添加一个带有 post id 的隐藏字段:
<form id="foo'.$t.'">
<input id="bar'.$t.'" name="bar" type="text" value="'.$link.'" />
<input type="hidden" name="post_id" value="Some value" />
<input type="submit" value="Report as broken" >
</form>
On server side you can do something like:在服务器端,您可以执行以下操作:
$stmt = $con->prepare("UPDATE reports SET link = ? WHERE id = ?"); // or post_id
// set parameters
$link = $_POST['bar'];
$id = $_POST['post_id'];
// bind, I presume your id is number so we pass `i` as argument
$stmt->bind_param("si", $link, $id);
// run
$stmt->execute();
$stmt->close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.