4 回答
TA贡献1851条经验 获得超3个赞
首先,您的表单在提交时不会提供有关要验证的客户ID的任何线索。在我的示例代码中,我不会使用 echo,因为像这样回显 HTML 输出是很丑陋的。相反,您可以像这样操作:
?>
<div class='valid_name_btn'>
<form method='post'>
<input type="hidden" name="client" value="<?= $clientid ?>">
<input type='submit' class='btn btn-outline-primary' value ='Validate' name='validate' id='validate'></input>
</form>
<div class='valid_name'>
<?= $row["First_Name"] . " " . $row["Last_Name"]." - Account No. ".$row["account_no"] ?><br/>
</div>
</div>
<?php
我已将一个隐藏的输入字段添加到表单中,然后您可以在文件开头的行之前对其进行评估:$tobe_validated = ..
if (isset($_POST['validate']) && (isset($_POST['client'])) {
$clientid = $_POST['client'];
$sql = "UPDATE customer SET validated = 1 WHERE customer_id = '$clientid'";
$dbh->query($sql);
}
您还应该正确准备语句,或者至少对$client值进行转义。由于我不知道$dbh是什么(你的代码没有告诉),我不会在这里这样做。
TA贡献1863条经验 获得超2个赞
$sql ="UPDATE customer SET validated = 1 WHERE customer_id = $clientid";
$clientid
应不带“”
TA贡献1827条经验 获得超9个赞
你有逻辑错误,因为你的形式只是在传递值,而不是确切的。您必须从循环中移出操作,并在每个表单中添加一个隐藏字段,并相应地:validateclientid$_POSTclientid
<?php
$sql = "SELECT customer.First_Name, customer.Last_Name, account.account_no, account.client_id, customer.username
FROM customer
INNER JOIN account ON customer.customer_id=account.client_id
WHERE validated = 0";
$tobe_validated = $dbh->query($sql);
foreach ($tobe_validated as $row) {
//creating variable for account number to put in query
$clientid = $row["client_id"];
echo "<div class='valid_name_btn'>";
echo "<form method='post'>
<input type='hidden' name='clientid' value='".$clientid."'>
<input type='submit' class='btn btn-outline-primary' value ='Validate' name='validate' id='validate'></input>
</form>";
echo "<div class='valid_name'>"; //div for name
echo $row["First_Name"] . " " . $row["Last_Name"] . " - Account No. " . $row["account_no"] . "<br/>"; //show name and account number of client
echo "</div>";
echo "</div>";
}
if (isset($_POST['validate']) && isset($_POST['clientid'])) {
$clientid = $_POST['clientid'];
$sql = "UPDATE customer SET validated = 1 WHERE customer_id = '$clientid'";
$dbh->query($sql);
}
TA贡献1875条经验 获得超5个赞
您可以根据键制作动态表单,并可以通过如下所示的适当条件来检查按钮是否具有特定值
<?php
$sql ="SELECT customer.First_Name, customer.Last_Name, account.account_no, account.client_id, customer.username
FROM customer
INNER JOIN account
ON customer.customer_id=account.client_id
WHERE validated = 0";
$tobe_validated = $dbh->query($sql);
foreach ($tobe_validated as $key => $row) {
//creating variable for account number to put in query
$clientid=$row["client_id"];
echo "<div class='valid_name_btn'>";
echo "<form method='post' name="'validation_form_'.$key"><input type='submit' class='btn btn-outline-primary' value ='Validate' name="'validate_'.$key" id="'validate_'.$key"></input></form>";
echo "<div class='valid_name'>"; //div for name
echo $row["First_Name"] . " " . $row["Last_Name"]." - Account No. ". $row["account_no"]."<br/>"; //show name and account number of client
echo "</div>";
echo "</div>";
// validate account when button is clicked
if(isset($_POST['validate_'.$key]) && $_POST['validate_'.$key] == 'Validate') {
// query to change validated in customer table to 1
$updateSql ="UPDATE customer SET validated = 1 WHERE customer_id = '$clientid'";
$dbh->query($updateSql);
}
}
如果您需要任何帮助,请告诉我
- 4 回答
- 0 关注
- 123 浏览
添加回答
举报