为每个动态生成的字段和提交按钮存储静态数据

时间:2016-06-13 07:33:31

标签: php mysqli

我已经尝试了一些代码,我需要存储通过提交按钮给出的动态生成的用户数据..但我的问题是所有动态生成的按钮携带相同的数据并在数据库中存储静态即相同的数据,我有知道创建一个阵列可能会解决我的问题,但因为我对这个平台很陌生,我请你帮我解决我的问题我非常需要这个答案..请原谅我,如果我出错了..谢谢你!

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

中间件和数据库就像这样

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

    }

?>

1 个答案:

答案 0 :(得分:1)

对于不同的提交按钮,我认为您可能使用相同的表单。尝试使用如下所示的不同表单标记。在这里给动作网址......

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