PHP 中 foreach 循环每次迭代的提交按钮

我对编码很陌生,所以请轻轻地去...


我正在创建一个管理页面,其中包含需要验证的帐户列表。我所需要的只是在单击按钮时将数据库“已验证”列中的值从0更改为1。问题在于,它为循环中所有其他返回的结果触发相同的操作,每个结果都有自己的按钮,而不仅仅是循环的特定迭代。任何帮助将不胜感激。代码目前如下所示:


<?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='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>";



        // query to change validated in customer table to 1

          $sql ="UPDATE customer SET validated = 1 WHERE customer_id = '$clientid'";


        // validate account when button is clicked

          if(isset($_POST['validate'])) {

          $dbh->query($sql);

          } 

         }


萧十郎
浏览 124回答 4
4回答

皈依舞

首先,您的表单在提交时不会提供有关要验证的客户ID的任何线索。在我的示例代码中,我不会使用 echo,因为像这样回显 HTML 输出是很丑陋的。相反,您可以像这样操作:?><div class='valid_name_btn'>&nbsp; <form method='post'>&nbsp; &nbsp; <input type="hidden" name="client" value="<?= $clientid ?>">&nbsp; &nbsp; <input type='submit' class='btn btn-outline-primary' value ='Validate' name='validate' id='validate'></input>&nbsp; </form>&nbsp; <div class='valid_name'>&nbsp; &nbsp; <?= $row["First_Name"] . " " . $row["Last_Name"]." - Account No. ".$row["account_no"] ?><br/>&nbsp; </div></div><?php我已将一个隐藏的输入字段添加到表单中,然后您可以在文件开头的行之前对其进行评估:$tobe_validated = ..if (isset($_POST['validate']) && (isset($_POST['client'])) {&nbsp; $clientid = $_POST['client'];&nbsp; $sql = "UPDATE customer SET validated = 1 WHERE customer_id = '$clientid'";&nbsp; $dbh->query($sql);}您还应该正确准备语句,或者至少对$client值进行转义。由于我不知道$dbh是什么(你的代码没有告诉),我不会在这里这样做。

小唯快跑啊

$sql&nbsp;="UPDATE&nbsp;customer&nbsp;SET&nbsp;validated&nbsp;=&nbsp;1&nbsp;WHERE&nbsp;customer_id&nbsp;=&nbsp;$clientid";$clientid应不带“”

素胚勾勒不出你

你有逻辑错误,因为你的形式只是在传递值,而不是确切的。您必须从循环中移出操作,并在每个表单中添加一个隐藏字段,并相应地:validateclientid$_POSTclientid<?php&nbsp; &nbsp; $sql = "SELECT customer.First_Name, customer.Last_Name, account.account_no, account.client_id, customer.username&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FROM customer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; INNER JOIN account ON customer.customer_id=account.client_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WHERE validated = 0";&nbsp; &nbsp; $tobe_validated = $dbh->query($sql);&nbsp; &nbsp; foreach ($tobe_validated as $row) {&nbsp; &nbsp; &nbsp; &nbsp; //creating variable for account number to put in query&nbsp; &nbsp; &nbsp; &nbsp; $clientid = $row["client_id"];&nbsp; &nbsp; &nbsp; &nbsp; echo "<div class='valid_name_btn'>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<form method='post'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type='hidden' name='clientid' value='".$clientid."'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type='submit' class='btn btn-outline-primary' value ='Validate' name='validate' id='validate'></input>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </form>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<div class='valid_name'>"; //div for name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $row["First_Name"] . " " . $row["Last_Name"] . " - Account No. " . $row["account_no"] . "<br/>"; //show name and account number of client&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</div>";&nbsp; &nbsp; &nbsp; &nbsp; echo "</div>";&nbsp; &nbsp; }&nbsp; &nbsp; if (isset($_POST['validate']) && isset($_POST['clientid'])) {&nbsp; &nbsp; &nbsp; &nbsp; $clientid = $_POST['clientid'];&nbsp; &nbsp; &nbsp; &nbsp; $sql = "UPDATE customer SET validated = 1 WHERE customer_id = '$clientid'";&nbsp; &nbsp; &nbsp; &nbsp; $dbh->query($sql);&nbsp; &nbsp; }

慕田峪4524236

您可以根据键制作动态表单,并可以通过如下所示的适当条件来检查按钮是否具有特定值<?php$sql ="SELECT customer.First_Name, customer.Last_Name, account.account_no, account.client_id, customer.usernameFROM customer&nbsp;&nbsp;INNER JOIN account&nbsp;&nbsp;ON customer.customer_id=account.client_id&nbsp;&nbsp;WHERE validated = 0";&nbsp;$tobe_validated = $dbh->query($sql);foreach ($tobe_validated as $key => $row) {&nbsp; &nbsp; &nbsp; &nbsp; //creating variable for account number to put in query&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $clientid=$row["client_id"];&nbsp; &nbsp; &nbsp; &nbsp; echo "<div class='valid_name_btn'>";&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 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>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<div class='valid_name'>"; //div for name&nbsp; &nbsp; &nbsp; &nbsp; echo $row["First_Name"] . " " . $row["Last_Name"]." - Account No. ". $row["account_no"]."<br/>"; //show name and account number of client&nbsp; &nbsp; &nbsp; &nbsp; echo "</div>";&nbsp; &nbsp; &nbsp; &nbsp; echo "</div>";&nbsp; &nbsp; &nbsp; &nbsp; // validate account when button is clicked&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isset($_POST['validate_'.$key]) && $_POST['validate_'.$key] == 'Validate') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // query to change validated in customer table to 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $updateSql ="UPDATE customer SET validated = 1 WHERE customer_id = '$clientid'";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $dbh->query($updateSql);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}如果您需要任何帮助,请告诉我
打开App,查看更多内容
随时随地看视频慕课网APP