Php - 当[data]循环时,$ Post仅返回第一个值

时间:2018-02-01 12:55:35

标签: php forms post mysqli while-loop

我有这个代码,他从数据库中检索AD帐户,邮件。 显示没问题,我在每行添加一个更新按钮(UpdateAccount),允许更新帐户信息。

但是,当我提交更新时,$ Post值总是来自第一个帐户的值(不是我选择的帐户)。

例如:

https://imgur.com/a/GUCOd

问题:

当我想更新第二个帐户时,$ post会从第一个帐户发送数据。

当我想更新第三个帐户时,$ post会向我发送第一个帐户的数据。

如果您有任何解决方案,我认为我需要在包含我选择的数据的地方传递“$ post ID”...

代码:

<?php
$data = '';

# Retrieving account in database

$sql = "SELECT ".USERAD_ID.",".USERAD_ENABLED.",".USERAD_ACCOUNT.",".USERAD_EMAIL." FROM ".USERAD_TABLE."";
$mydb = new DB();
$result = $mydb->runQuery($sql);

# Template Column

echo "
<div class='col-sm-12'>
        <div class='form-group'>
            <div class='col-sm-1'><label class='BoxSmall col-sm-12 control-label'>ID</label></div>
            <div class='col-sm-2'><label class='BoxSmall col-sm-12 control-label'>Compte AD</label></div>
            <div class='col-sm-3'><label class='BoxSmall col-sm-12 control-label'>Adresse Mail</label></div>
            <div class='col-sm-1'><label class='BoxSmall col-sm-12 control-label'>Etat</label></div>
        </div>
</div>          

";

# While Account in $result, display data

while($data = mysqli_fetch_assoc($result)) {

$id     =   $data[USERAD_ID];
$account    =   $data[USERAD_ACCOUNT];
$email      =   $data[USERAD_EMAIL];
$enabled    =   $data[USERAD_ENABLED];

echo "  
    <div class='col-sm-12'>
        <div class='form-group'>
            <div class='col-sm-1'><input type='textsmall' required name='id' id='id' value='".$id."' '></div>
            <div class='col-sm-2'><input type='textsmall' required name='account' id='account' value='".$account."' '></div>
            <div class='col-sm-3'><input type='textsmall' required name='email' id='email' value='".$email."' '></div>
            <div class='col-sm-1'><input type='textsmall' required name='enabled' id='enabled' value='".$enabled."' '></div>

        <form id='updatead' role='form' method='post'>

                    <input type='hidden' name='id' value='".$id."'>
                    <input type='hidden' name='account' value='".$account."'>
                    <input type='hidden' name='email' value='".$email."'>
                    <input type='hidden' name='enabled' value='".$enabled."'>
        </form>

        <INPUT type='submit' class='form-updatead' value='updatead' name='submit'></INPUT>
        </div>
    </div>

";
}   

?>

2 个答案:

答案 0 :(得分:1)

如果您在一个页面上有多个表单,那么您的提交按钮将默认发布第一个表单。考虑删除INPUT并使用按钮或div。像这样:

echo "  
<div class='col-sm-12'>
    <div class='form-group'>
        <div class='col-sm-1'><input type='textsmall' required name='id' id='id' value='".$id."' '></div>
        <div class='col-sm-2'><input type='textsmall' required name='account' id='account' value='".$account."' '></div>
        <div class='col-sm-3'><input type='textsmall' required name='email' id='email' value='".$email."' '></div>
        <div class='col-sm-1'><input type='textsmall' required name='enabled' id='enabled' value='".$enabled."' '></div>

    <form id='".$id."' role='form' method='post' action='/doSomething.php'>

                <input type='hidden' name='id' value='".$id."'>
                <input type='hidden' name='account' value='".$account."'>
                <input type='hidden' name='email' value='".$email."'>
                <input type='hidden' name='enabled' value='".$enabled."'>
    </form>

    <button onclick='document.getElementById('".$id."').submit()'>Submit</button>
    </div>
</div>
";

注意:我还添加了&#34; action =&#39; /doSomething.php'&#34;。

答案 1 :(得分:0)

谢谢,它的工作非常完美。 这是最终的代码:

<?php

while($data = mysqli_fetch_assoc($result)) {

$id         =   $data[USERAD_ID];
$enabled    =   $data[USERAD_ENABLED];
$account    =   $data[USERAD_ACCOUNT];
$email      =   $data[USERAD_EMAIL];

echo "  
<div class='col-sm-12'  >
    <div class='form-group'>
        <form id='".$id."' role='form' method='post' action='./admin/permissionsform.php?page=updatead'>
            <input type='hidden' name='id' value='".$id."'>
            <div class='col-sm-2'><input type='textsmall' required name='account' id='account' value='".$account."' style='width:75%'></div>
            <div class='col-sm-3'><input type='textsmall' required name='email' id='email' value='".$email."' style='width:75%'></div>
            <div class='col-sm-1'><input type='textsmall' required name='enabled' id='enabled' value='".$enabled."' style='width:75%'></div>
        </form>

        <button class='buttoneditsrv' onclick='document.getElementById(".$id.").submit()';window.location='./?view=list&search='>Mise &agrave jour</button>
    </div>
</div>
";
}
?>