Mysql UPDATE错误,只影响第一行



当我使用sql UPDATE查询时,它只影响我的成员的第一行。

<?php
$query = mysqli_query($db, "SELECT * FROM `members` ORDER BY `siteRank`");
$result = mysqli_fetch_assoc($query);
?>
<?php
if($_POST['AccountUpdate'])
 {
        //mysqli_query($db, "UPDATE members SET username='$Username' WHERE id='$$IdentifcationNumber' ORDER BY id DESC LIMIT 1");
        echo $result['id'];
        echo $result['username'];
        echo 'separeator';
        echo $IdentifcationNumber;
 }
?>
<form method="post" action="viewprofile.php">
<table border="1px">
    <tr>
        <th>ID</th>
        <th>Username</th>
        <th>Password</th>
        <th>Email</th>
        <th>Browser</th>
        <th>IP</th>
        <th>Site Rank</th>
        <th>PyroCoins</th>
        <th>PIN</th>
        <th>Status</th>
        <th>Date Joined</th>
        <th>Update Account</th>
    </tr>
    <?php
        while($result = mysqli_fetch_assoc($query)){
    ?>
    <form method="post" action="viewprofile.php">
    <tr>
        <td><input readonly="readonly" value="<?php echo $result['id'];?>" /></td>
        <td><?php echo $result['username'];?></td>
        <td><input text="text" name="ChangePassword" value="<?php echo $result['password'];?>" /></td>
        <td><?php echo $result['email'];?></td>
        <td><?php echo $result['browser'];?></td>
        <td><?php echo $result['ip'];?></td>
        <td><?php echo $result['siteRank'];?></td>
        <td><input type="text" name="ChangePyroCoins" value="<?php echo $result['PyroCoins'];?>" /></td>
        <td><?php echo $result['pin'];?></td>
        <td>
            Current Status:
            <input readonly="readonly" value="<?php echo $result['status'];?>" />

            <select name="ChangeStatus">
                <option value="<?php echo $result['status'];?>"><?php echo $result['status'];?></option>
                <option value="[ Content Deleted ]">[ Content Deleted ]</option>
            </select>
        </td>
        <td><?php echo $result['date'];?></td>
        <td><input type="submit" name="AccountUpdate" value="Update Account"></td>
    </tr>
    <?php
        }
    ?>
</table>
</form>

我的$_POST数据应该是不言自明的,但它只影响第一行,而不是我试图影响的行。当我单击html更新按钮时,它只显示第一个ID,而不是我试图更新的ID或帐户凭据。例如,当我尝试编辑ID为28的一些详细信息时,它会影响第一个表ID,即1

查询中不需要出现' ORDER BY id DESC LIMIT 1 '。

UPDATE members SET username='$Username' WHERE id='$IdentifcationNumber'

@Isaak Markopoulos -这是没有测试,但没有嵌套的形式,所以希望有应该有更少的混乱时,张贴的形式。html中的id字段已经命名,所以至少在发布的变量

中应该有一个可用的id
<html>
    <head>
       <title>crazy little monkey</title>
    </head>
    <body>
    <?php
        /* This only gets executed if it is a POST request */
        if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['AccountUpdate'] ) && isset( $_POST['IdentifcationNumber'] ) ){
            /* I assume this is the correct id - there was no name for the form element previously */
            $IdentifcationNumber=$_POST['IdentifcationNumber'];
            $sql="UPDATE members SET username='$Username' WHERE id='$IdentifcationNumber';";
            mysqli_query( $db, $sql );
            echo $result['id'];
            echo $result['username'];
            echo 'separeator';
            echo $IdentifcationNumber;
        }   
        /* This gets executed for any request */
        $query = mysqli_query( $db, "SELECT * FROM `members` ORDER BY `siteRank`;" );
        $result = mysqli_fetch_assoc( $query );
    ?>
    <!-- parent table -->
    <table border="1px">
        <tr>
            <th>ID</th>
            <th>Username</th>
            <th>Password</th>
            <th>Email</th>
            <th>Browser</th>
            <th>IP</th>
            <th>Site Rank</th>
            <th>PyroCoins</th>
            <th>PIN</th>
            <th>Status</th>
            <th>Date Joined</th>
            <th>Update Account</th>
        </tr>
        <tr>
            <td colspan=12>
            <!-- begin a form / table for each row in rs -->
            <?php
                $rc=0;
                while( $result = mysqli_fetch_assoc( $query ) ){
                    $rc++;
                    echo "
                        <!-- unique name for each form - not essential -->
                        <form name='vpf_{$rc}' action='viewprofile.php' method='post'>
                            <!-- nested child table fully contained within it's parent form element -->
                            <table>
                                <td><input type='text' name='IdentifcationNumber' readonly='readonly' value='".$result['id']."' /></td>
                                <td>".$result['username']."</td>
                                <td><input text='text' name='ChangePassword' value='".$result['password']."' /></td>
                                <td>".$result['email']."</td>
                                <td>".$result['browser']."</td>
                                <td>".$result['ip']."</td>
                                <td>".$result['siteRank']."</td>
                                <td><input type='text' name='ChangePyroCoins' value='".$result['PyroCoins']."' /></td>
                                <td>".$result['pin']."</td>
                                <td>
                                    Current Status:
                                    <input readonly='readonly' value='".$result['status']."' />
                                    <select name='ChangeStatus'>
                                        <option value='".$result['status']."'>".$result['status']."
                                        <option value='[ Content Deleted ]'>[ Content Deleted ]
                                    </select>
                                </td>
                                <td>".$result['date']."></td>
                                <td><input type='submit' name='AccountUpdate' value='Update Account'></td>                                  
                            </table>
                        </form>";
                }/* Close the while loop - note that the form(s) is/are FULLY contained within the loop - NO nesting */
            ?>
            </td>
        </tr>
    </table>
    </body>
</html>

最新更新