PHP$\u POST传递了哪个数组项?
我对$_POST有一个问题,我正在编写一个管理屏幕,并输出一个注释列表,供用户编辑或删除。我输出到屏幕的是注释标题和注释内容,然后是编辑和删除的两个按钮。我也放了便条id,但这是隐藏的。 显然,读取和输出的记录数不同,但当用户单击删除按钮时,如何将正确的id传递给删除进程 我必须在屏幕上输出的代码是:PHP$\u POST传递了哪个数组项?,php,arrays,post,Php,Arrays,Post,我对$_POST有一个问题,我正在编写一个管理屏幕,并输出一个注释列表,供用户编辑或删除。我输出到屏幕的是注释标题和注释内容,然后是编辑和删除的两个按钮。我也放了便条id,但这是隐藏的。 显然,读取和输出的记录数不同,但当用户单击删除按钮时,如何将正确的id传递给删除进程 我必须在屏幕上输出的代码是: <form name="sticky2" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"])
<form name="sticky2" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<?php
$i = 0;
foreach($stickynotes as $notes)
{
echo $i . " - " . $notes['id']; \\this line is just testing
?>
<li>
<input type="hidden" name="id[]" value="<?php echo $notes['id'] ?>">
<h2><?php echo $notes['title'] ?></h2>
<p><?php echo $notes['content'] ?></p>
<br>
</li>
<input type="submit" name="edit" class="StickyBtn StickyBlue" value="Edit" />
 
<input type="submit" name="delete" class="StickyBtn" value="Delete" />
<br><br>
 
<?php
$i++;
}
?>
</form>
下面的一行使用了['0'],我知道它是
$notes数组,但我只是确保所有的删除代码都有效
是细节导致了我的问题吗
$stmt->bindParam(':id', $_POST['id']['0'], PDO::PARAM_INT);
try
{
// $stmt->execute(); // actual delete commented out while testing
}
catch(PDOException $ex)
{
$msg = "STICKY NOTE NOT Found";
//user friendly message and error handling here
}
header('Location: stickynotes.php' );
}
}
我不明白删除过程如何真正知道它必须删除哪条记录,或者我的过程错了?我想你会遇到的问题是,你的表单包含所有ID,任何提交按钮都会影响所有ID。最简单的方法可能是将标记移动到循环内部,这样每个项都有不同的形式。然后你有独立的删除按钮和单独的ID字段,这样就不必是一个数组,因为每个表单也只有一个附加的ID
$stmt->bindParam(':id', $_POST['id']['0'], PDO::PARAM_INT);
try
{
// $stmt->execute(); // actual delete commented out while testing
}
catch(PDOException $ex)
{
$msg = "STICKY NOTE NOT Found";
//user friendly message and error handling here
}
header('Location: stickynotes.php' );
}
}