1

I am very new to coding so please go gently...

I'm creating an admin page with list of accounts which require validation. All I require is for the value in the 'validated' column in the DB to change from 0 to 1 when the button is clicked. The problem is that it triggers the same thing for all the other returned results in the loop, who each have their own buttons, rather than just for that particular iteration of the loop. Any help would be greatly appreciated. Code looks as follows currently :

<?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);
          } 
         }
1
  • What exactly you are expecting to happen when your forms are just passing the submit button but not the exact $clientid upon submit? Commented Jan 16, 2020 at 15:14

4 Answers 4

3

You have logical mistake as your forms are just passing validate value, but not the exact clientid. You have to move out the $_POST action from your loop and to add a hidden field in each form with corresponding clientid:

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

Sign up to request clarification or add additional context in comments.

1 Comment

this one worked exactly as I needed. Thanks so much, and also for explaining so clearly where I was going wrong
0

You can make dynamic form based on key and can check whether button have particular value or not by making proper condition like below

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

let me know if you need any help on this

1 Comment

I don't see any reason to introduce an additional form element by using $key instead of using directly clientid and to keep the validation into the loop.
0

First of all, your form - when submitted - does not give any clue about the client's ID to validate. In my example code, I will not be using echo because echoing HTML output like this is ugly. Instead, you can do it like so:

?>
<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

I've added a hidden input field to the form that you can then evaluate at the beginning of the file, before the $tobe_validated = .. line:

if (isset($_POST['validate']) && (isset($_POST['client'])) {
  $clientid = $_POST['client'];
  $sql = "UPDATE customer SET validated = 1 WHERE customer_id = '$clientid'";
  $dbh->query($sql);
}

You should also properly prepare the statement, or at least escape the $client value. Since I do not know what $dbh is (your code doesn't tell) I will not do this here.

Comments

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

$clientid should be without ''

3 Comments

That shouldn't matter. MySQL will compensate for it.
No, $clientid will be properly interpolated in the query, even in single quotes. example
Thank you very much for all the replies (and patience, with what presumably is for most of you a very basic mistake!). I won't have a chance to actually work with my code again until tomorrow morning (I'm in France) but thank you again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.