Mysql UPDATE错误只影响第一行?

时间:2015-07-29 21:52:48

标签: php mysql sql

当我使用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

2 个答案:

答案 0 :(得分:0)

查询中的ORDER BY id DESC LIMIT 1'是不必要的。

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

答案 1 :(得分:0)

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