0

I have tried out some code were i need to store user data given via submit button which are generated dynamically..but my problem is all dynamically generated buttons carry's same data and stores a static i.e same data in data base, i have come to know that creating a array might solve my issue but because am new to this platform i ask you to please help me to resolve my issue i badly need an answer for this..please pardon me if i have gone wrong..thank you!

.php

    <form id="form" name ="form" method = "POST" action="move_ppl.php" class="wizard-big" autocomplete = "off" enctype="multipart/form-data">
                            <!--<h1>Account</h1>-->
                            <fieldset>
                                <!--<h2>Account Information</h2>-->
                                <div class="row">
                                    <!--<div class="col-lg-12">
                                        <div id="entry1" class="clonedInput ">

                                            <h4 id="reference" name="reference" class="heading-reference col-sm-12">Movable Investments #1</h4> --> 

                                           <div class="container">
                                          <ul class="nav nav-tabs">
                                          <li class="active"><a href="pro_ppl.php">Registered users </a></li>
                                            <li><a href="macro_ppl.php">Macro</a></li>
                                            <li><a href="#">Micro</a></li>
                                            <li><a href="nano_ppl.php">Nano</a></li>
                                            </ul>
                                          <br>
       <?php
        $con = mysqli_connect("localhost","***","****","****");
                $query = ("SELECT * FROM profile");
                    $result = mysqli_query($con, $query);
                            while ($row = $result->fetch_assoc())
                            {
                                echo '
                                          <div class="col-md-12">



                                        <div class="col-sm-3 form-group">
                                                <h4>' . $row['via'] . '</h4>
                                                </div>

                                                <div class="col-sm-3 form-group">
                                                <input class="form-control " placeholder="Institution" type="hidden" name = "ppl" id = "ppl" value="' . $row['via'] . '">
                                                </div>

                                                <div class="col-sm-2 form-group">
                                            <input style="width:100%" type="submit" name = "macro" alt="macro" id = "submit" value = "Add to macro"  class="btn btn-success">
                                            </div>

                                    </div>';



                                        }
                                    ?>
                   </div>


                                    </div>
                            </fieldset>

                        </form>

And middle-ware and data base gose like this

.php

<?php
    session_start();
    define('HOST','localhost');
    define('USER','***');
    define('PASS','***');
    define('DB','***');

    $response = array();

    $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');

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

        //receiving post parameters
             $ppl =$_POST['ppl'];

        // create a new user profile
                                    $sql = "INSERT INTO macro (via, vault_no, gname, ppl, created_at) VALUES ('".$_SESSION['via']."', '".$_SESSION['vault_no']."', '".$_SESSION['gname']."', '$ppl', NOW())";
                                    if(mysqli_query($con,$sql)){
                                        header('Location: macro_ppl.php');

                                    }else{
                                        $response["error"] = true;
                                        $response["error_msg"] = "INSERT operation failed";
                                        echo json_encode($response);
                                    }

    }

?>
  • 2
    Just use `name="macro[]"` and in your PHP, read it with a loop `foreach($_POST['macro'] AS $index=>$macro){..}` – Yuri Jun 13 '16 at 07:42
  • 1
    You might want to read this question and its answers: http://stackoverflow.com/questions/3314567/how-to-get-form-input-array-into-php-array – Salketer Jun 13 '16 at 07:47
  • 1
    Also, please read about how to safely run queries, you are using unsecure user input without escaping it correctly, this may cause big troules. – Salketer Jun 13 '16 at 07:48
  • Okay thanks for sharing link..! –  Jun 13 '16 at 07:49
  • Sure I'll go through it.. –  Jun 13 '16 at 07:50
  • but am bit unclear how it can solve my issue..!! can someone please provide me an answer with explanation of process.? –  Jun 13 '16 at 08:52
  • What data is been inserted into your database again and again? – Faisal Mohmand Jun 13 '16 at 09:45
  • I displayed the list of users with a submit button dynamically but for every submit button same data or user($ppl) is being saved..! –  Jun 13 '16 at 09:49

1 Answers1

0

For different submit button i think you may used same form. Try to use different form tag like below. Give the action url here...

<?php
$con = mysqli_connect("localhost","***","****","****");
$query = ("SELECT * FROM profile");
$result = mysqli_query($con, $query);

    while ($row = $result->fetch_assoc())
    {
    echo '
        <form action="" method="post">
            <div class="col-md-12">

                <div class="col-sm-3 form-group">
                <h4>' . $row['via'] . '</h4>
                </div>

                <div class="col-sm-3 form-group">
                <input class="form-control " placeholder="Institution" type="hidden" name = "ppl" id = "ppl" value="' . $row['via'] . '">
                </div>

                <div class="col-sm-2 form-group">
                <input style="width:100%" type="submit" name = "macro" alt="macro" id = "submit" value = "Add to macro"  class="btn btn-success">
                </div>

            </div>
        </form>
    ';
    }
?>
Mani
  • 2,675
  • 2
  • 20
  • 42