此刻,我正在使用循环读取我的PostgreSQL“ PDPC” .considerations表中的“问题”和“答案”字段,并将其反映在表单上。我想用一个提交按钮来更新它们。
我参考了PHP MySQL Multiple Forms and Multiple Submits on single page,并尝试使用<input type="hidden">
,数组和while
循环,但是该表单无法执行或无法正确更新所有表单。
我认为错误出在$_POST
(在顶部)和HTML表单(在底部)周围。 (很抱歉,如果代码混乱,这是我第一次使用PHP,HTML和Postgres)。谢谢!
<?php
// Include config file
require_once "config.php";
// Define variables and initialize with empty values
$question = $answer = "";
$question_err = $answer_err = "";
// Processing form data when form is submitted
if(isset($_POST["consideration_no"]) && !empty($_POST["consideration_no"])){
$counter = 0;
// Get hidden input value
//$consideration_no = $_POST['consideration_no'];
$dg_no = $_POST['dg_no'];
$consideration_no = $_POST['consideration_no'];
$answer = $_POST['answer'];
// Check input errors before inserting in database
if(empty($answer_err)){
while ($counter<5){
// Validate address address
$input_answer = trim($_POST["answer"]);
if(empty($input_answer)){
$answer_err = "Please enter an answer.";
} else{
$answer1[$counter] = $input_answer;
}
// Prepare an update statement
$sql = 'UPDATE "PDPC".consideration SET answer=:answer WHERE consideration_no = :consideration_no';
if($stmt = $pdo->prepare($sql)){
$stmt->bindParam(":answer", $param_answer);
$stmt->bindParam(":consideration_no", $param_consideration_no);
//Set Parameter
$param_answer = $answer1[$counter];
$param_consideration_no = $consideration_no[$counter];
// Attempt to execute the prepared statement
$stmt->execute();
$counter++;
}
}
if($stmt->execute()){
// Records updated successfully. Redirect to landing page
header("location: home1.php?dm_no=".$_GET["dm_no"]);
exit();
} else{
echo "Something went wrong. Please try again later.";
}
// Close statement
unset($stmt);
}
// Close connection
unset($pdo);
} else{
// Check existence of dg_no parameter before processing further
if(isset($_GET["dg_no"]) && !empty(trim($_GET["dg_no"]))){
// Get URL parameter
$dg_no = trim($_GET["dg_no"]);
// Prepare a select statement
$sql = 'SELECT * FROM "PDPC".consideration WHERE (dg_fkey = :dg_no AND code_no = 1)';
if($stmt = $pdo->prepare($sql)){
// Bind variables to the prepared statement as parameters
$stmt->bindParam(":dg_no", $param_no);
// Set parameters
//$param_no = $dg_no;
$param_no = trim($_GET["dg_no"]);
// Attempt to execute the prepared statement
if($stmt->execute()){
if($stmt->rowCount() > 0){
SubSection($subsection1_1); //Collection Purpose Section
while($row = $stmt->fetch()){
// Retrieve individual field value
$consideration_no = $row["consideration_no"];
$question = $row["question"];
$answer = $row["answer"];
echo "<a href='considerationupdate.php?consideration_no=". $row['consideration_no'] ."' title='Update Data Map' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
//...time to show the questions and answers with the while loop...
?>
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div class="form-group <?php echo (!empty($answer_err)) ? 'has-error' : ''; ?>">
<label><?php echo $question; ?></label>
<input type="text" name="answer" class="form-control" value="<?php echo $answer; ?>">
<span class="help-block"><?php echo $answer_err;?></span>
<input type="hidden" name="answer1[]" id = "$answer1" value="<?php echo $answer; ?>"/>
<input type="hidden" name="consideration_no[]" id = "consideration_no" value="<?php echo $consideration_no; ?>"/>
<input type="hidden" name="dg_no" value="<?php echo $dg_no; ?>"/>
</div>
<input type="submit" class="btn btn-primary" value="Submit">
<?php
}
?>
<input type="submit" class="btn btn-primary" value="Submit">
<a href="javascript:history.go(-1)" class="btn btn-default">Cancel</a>
</form>
</div>
<?php
}
}
else{
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
unset($stmt);
// Close connection
unset($pdo);
}
else{
// URL doesn't contain dg_no parameter. Redirect to error page
header("location: error.php");
exit();
}
}
?>
它应该从数据库表中读取问题和答案,将其显示在表单上作为标签和文本字段(有效),并且用户应该能够在编辑文本字段并单击提交后更新表单(不工作正常)。
答案 0 :(得分:1)
您的分支if(isset($_GET["dg_no"]) && !empty(trim($_GET["dg_no"]))){
取决于get
参数,但是,您<form method="post">
中的<input type="hidden" name="dg_no" value="<?php echo $dg_no; ?>"/>
使用发布参数。
因此,除非该页面是由另一个GET请求(如链接或其他形式)请求的,否则该分支将永远不会执行。
如果参数可以同时在POST和GET这两种方法中出现,则您可能需要检查$_REQUEST
数组。请注意,$_REQUEST
中列出的参数会根据.ini
设置request_order
和variables_order
的不同而变化。
根据您的评论“因为每种形式都用于一个表行。” 在另一个答案中,可能是XY problem。
考虑采取常见的方法,而不是生成多种形式,而是使用与您类似的数组参数
<input type="hidden" name="answer1[]" id = "$answer1" value="<?php echo $answer; ?>"/>
这里,您依赖于自动生成的密钥。您还可以指定一个单独的键:
<input type="text" name="some_parameter[<?php echo $answer; ?>]">
更多
循环中有一个HTML行,其元素上具有静态id
:
<input type="hidden" name="consideration_no[]" id="consideration_no" value="<?php echo $consideration_no; ?>"/>
这不会破坏PHP,但是,违反HTML规范说,每个文档的ID必须唯一。
答案 1 :(得分:1)
因为是您的表单,并且您正在使用SQL进行获取,而目的是通过一次提交来更新数据?
为什么不只使用一种形式?我相信您只需一次输入即可完成所有您需要的工作。
答案 2 :(得分:0)
我已解决问题!这是脚本的逻辑流程。具体来说,应该将顶部的POST变量更改为dg_no,并随其一起添加dg_no参数,循环这些参数而不是整个准备脚本,并将执行脚本也包括在whie循环中。
感谢您的帮助和指导,非常感谢!