简体   繁体   English

如何在不知道 id 的情况下获取 POST 数据?

[英]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.

相关问题 在不知道ID或名称属性的情况下,如何收集所有输入值并通过$ .post发送? - How do you collect all input values and send via $.post, without knowing the ID or Name Properties? 我如何在不知道ID名称的情况下获取php中按钮的ID? - how can i get an ID of a button in php without knowing the ID name upfront 我如何在 laravel 中获取帖子 ID - How do i get a post id in laravel 如何在多维数组中获取信息而又不知道它位于哪一部分? - How do I get information within a multidimensional array without knowing in which section it's located? 如何在帖子中获取所有版块的ID? - How do I get all the ID's of sections in a post? 适应性-POST到实体-如何获取ID值? - Apigility - POST to Entity - How do I get the ID value? 如何在不对所有数据进行编码的情况下进行POST调用? - How do I make a POST call without encoding all of the data? 如何在不知道特定元素的类或ID的情况下将其定位为目标,但确实具有该元素的所有DOM详细信息 - How to target a specific element without knowing it's class or id, but do have all DOM details of the element 如何在不知道表单内控件名称的情况下获取表单中的GET / POST元素 - How to fetch the GET/POST elements in a form without knowing the name of the controls inside the form 为什么我在首页时会得到帖子ID数据 - why do i get post id data when i am in homepage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM