为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
萧十郎 2022-08-05 16:52:51
我对编码很陌生,所以请轻轻地去...我正在创建一个管理页面,其中包含需要验证的帐户列表。我所需要的只是在单击按钮时将数据库“已验证”列中的值从0更改为1。问题在于,它为循环中所有其他返回的结果触发相同的操作,每个结果都有自己的按钮,而不仅仅是循环的特定迭代。任何帮助将不胜感激。代码目前如下所示:<?php$sql ="SELECT customer.First_Name, customer.Last_Name, account.account_no, account.client_id, customer.usernameFROM 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);          }          }
查看完整描述

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是什么(你的代码没有告诉),我不会在这里这样做。


查看完整回答
反对 回复 2022-08-05
?
小唯快跑啊

TA贡献1863条经验 获得超2个赞

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

$clientid应不带“”


查看完整回答
反对 回复 2022-08-05
?
素胚勾勒不出你

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);

    }


查看完整回答
反对 回复 2022-08-05
?
慕田峪4524236

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);

          } 

         }

如果您需要任何帮助,请告诉我


查看完整回答
反对 回复 2022-08-05
  • 4 回答
  • 0 关注
  • 123 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信