繁体   English   中英

PHP Mysql更新查询不起作用

[英]PHP Mysql Update Query not working

我有一个非常愚蠢的问题。 在开始之前,我知道我正在使用不建议使用的mysql版本,但是我还没有来学习MySqli或PDO,但是我很快就会来。

我一直在研究整个管理系统,现在已经到了为管理员创建功能来编辑其他帐户的地步。 我已经将整个系统放在一页上,并且我的代码变得有些混乱(这可能就是为什么我出错了)。

好吧,我现在从我的问题开始:我有一个更新查询,但是它似乎没有用。 我的表格如下:

    <form action="" method="post">
    <table class="table table-profile">
        <thead>
            <tr>
                <th colspan="2">
                    <h4><?php echo $username; ?> <small><?php echo $fullname; ?></small></h4>
                </th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td colspan="2"><input type="hidden" name="id" value="<?php echo $id; ?>" /></td>
            </tr>
            <tr>
                <td class="field">Username</td>
                <td><input type="text" class="form-control input-xs" name="username" placeholder="" value="<?php echo $username; ?>" /></td>
            </tr>
            <tr>
                <td class="field">Password</td>
                <td><a href="#">Password Resets must be done through James/Hamish/Cryptic</a></td>
            </tr>
            <tr>
                <td class="field">Full name</td>
                <td><input type="text" class="form-control input-xs" name="fullname" placeholder="" value="<?php echo $fullname; ?>" /></td>
            </tr>
            <tr>
                <td class="field">Email</td>
                <td><input type="text" class="form-control input-xs" name="email" placeholder="" value="<?php echo $email; ?>" /></td>
            </tr>
            <tr>
                <td class="divider" colspan="2"></td>
            </tr>
            <tr>
                <td class="field">Membership</td>
                <td>
                    <select class="form-control input-inline input-xs" name="membership">
                        <option value="Administrator"<?php if($membership == 'Administrator') { echo ' selected'; } ?>>Administrator</option>
                        <option value="Moderator"<?php if($membership == 'Moderator') { echo ' selected'; } ?>>Moderator</option>
                        <option value="Supporter"<?php if($membership == 'Supporter') { echo ' selected'; } ?>>Supporter</option>
                        <option value="PM|T"<?php if($membership == 'PM|T') { echo ' selected'; } ?>>Platinum Modding Team</option>
                        <option value="Paid"<?php if($membership == 'Paid') { echo ' selected'; } ?>>Paid User</option>
                        <option value="Free"<?php if($membership == 'Free') { echo ' selected'; } ?>>Free User</option>
                        <option value="None"<?php if($membership == 'None') { echo ' selected'; } ?>>None</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td class="field">Plan Desription</td>
                <td><input type="text" class="form-control input-xs" name="membership_type" placeholder="Membership Type" value="<?php echo $membership_type; ?>" /></td>
            </tr>
            <tr>
                <td class="field">MaxTime</td>
                <td>
                    <select class="form-control input-inline input-xs" name="membership_maxtime">
                        <option value="600"<?php if($membership_maxtime == '600') { echo ' selected'; } ?>>600 Seconds</option>
                        <option value="1800"<?php if($membership_maxtime == '1800') { echo ' selected'; } ?>>1800 Seconds</option>
                        <option value="3600"<?php if($membership_maxtime == '3600') { echo ' selected'; } ?>>3600 Seconds</option>
                        <option value="7200"<?php if($membership_maxtime == '7200') { echo ' selected'; } ?>>7200 Seconds</option>
                        <option value="4147200"<?php if($membership_maxtime == '4147200') { echo ' selected'; } ?>>4147200 Seconds</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td class="field">Concurrents</td>
                <td>
                    <select class="form-control input-inline input-xs" name="membership_concurrents">
                        <option value="1"<?php if($membership_concurrents == '1') { echo ' selected'; } ?>>1 Concurrent</option>
                        <option value="2"<?php if($membership_concurrents == '2') { echo ' selected'; } ?>>2 Concurrents</option>
                        <option value="3"<?php if($membership_concurrents == '3') { echo ' selected'; } ?>>3 Concurrents</option>
                        <option value="4"<?php if($membership_concurrents == '4') { echo ' selected'; } ?>>4 Concurrents</option>
                        <option value="4147200"<?php if($membership_concurrents == '4147200') { echo ' selected'; } ?>>4147200 Concurrents</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td class="field">Expiration</td>
                <td><input type="text" class="form-control input-xs" name="membership_expiration" placeholder="Membership Expiration: (format: yyyy-mm-dd)" value="<?php echo $membership_expiration; ?>" /></td>
            </tr>
            <tr>
                <td class="divider" colspan="2"></td>
            </tr>
            <tr>
                <td class="field">IP Address</td>
                <td><a href="http://whatismyipaddress.com/ip/<?php echo $location_ip; ?>" target="new"><?php echo $location_ip; ?></td>
            </tr>
            <tr>
                <td class="divider" colspan="2"></td>
            </tr>
            <tr>
                <td class="field">Warning</td>
                <td>
                    <select class="form-control input-inline input-xs" name="warning">
                        <option value="0"<?php if($warning == '0') { echo ' selected'; } ?>>Level 0 - No warning</option>
                        <option value="1"<?php if($warning == '1') { echo ' selected'; } ?>>Level 1 - First Offence</option>
                        <option value="2"<?php if($warning == '2') { echo ' selected'; } ?>>Level 2 - Strong Warning</option>
                        <option value="3"<?php if($warning == '3') { echo ' selected'; } ?>>Level 3 - Last Chance</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td class="field">Warning Note</td>
                <td><textarea class="form-control" rows="4" name="warning_note" placeholder="The warning note will be displayed after a level 1 warning."><?php echo $warning_note; ?></textarea></td>
            </tr>
            <tr>
                <td class="divider" colspan="2"></td>
            </tr>
            <tr<?php if ($banned == 'true') { echo ' class="danger"'; } ?>>
                <td class="field"><i class="fa fa-ban"></i> Banned</td>
                <td>
                    <label class="radio-inline">
                        <input type="radio" name="banned" value="false"<?php if ($banned == 'false') { echo ' checked'; } ?> />
                        False
                    </label>
                    <label class="radio-inline">
                        <input type="radio" name="banned" value="true"<?php if ($banned == 'true') { echo ' checked'; } ?> />
                        True
                    </label>
                </td>
            </tr>
            <tr<?php if ($banned == 'true') { echo ' class="danger"'; } ?>>
                <td class="field">Banned Reason</td>
                <td><textarea class="form-control" rows="4" name="banned_note" placeholder="Reason for account ban. (If the account is banned)"><?php echo $banned_note; ?></textarea></td>
            </tr>
            <tr>
                <td class="divider" colspan="2"></td>
            </tr>
            <tr>
                <td class="field">Acc Created</td>
                <td><?php echo $created; ?></td>
            </tr>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="2">
                    <button type="submit" name="uploadAccount" class="btn btn-success"><i class="fa fa-upload"></i> Upload new changes</button>
                    <button type="reset" class="btn btn-warning"><i class="fa fa-refresh"></i> Reset Fields</button>
                </td>
            </tr>
        </tfoot>
    </table>
</form>

我更新数据库的代码如下:

    if (isset($_REQUEST['uploadAccount'])) {
    $id = $POST['id'];
    $username = $_POST['username'];
    $fullname = $_POST['fullname'];
    $email = $_POST['email'];

    $membership = $_POST['membership'];
    $membership_type = $_POST['membership_type'];
    $membership_maxtime = $_POST['membership_maxtime'];
    $membership_concurrents = $_POST['membership_concurrents'];
    $membership_expiration = $_POST['membership_expiration'];

    $warning = $_POST['warning'];
    $warning_note = $_POST['warning_note'];

    $banned = $_POST['banned'];
    $banned_note = $_POST['banned_note'];

    $account_update = mysql_query("UPDATE members SET username='$username', fullname='$fullname', email='$email', membership='$membership', membership_type='$membership_type', membership_maxtime='$membership_maxtime', membership_concurrents='$membership_concurrents', membership_expiration='$membership_expiration', warning='$warning', warning_note='$warning_note', banned='$banned', banned_note='$banned_note' WHERE id='$id'", $con);

    header('location: dashboard.php?admin&accounts&success=Account, '.$username.' successfully updated');
} else { }

如果您让我知道我在做什么错,我将不胜感激。 预先感谢那些人。

不要使用不推荐使用的mysql_ *使用PDO。

这里有一个例子。

$pdo    = new PDO ($server, $user, $password);
$query  = 'UPDATE members SET username= :username, fullname= :fullname';
$stmt   = $pdo -> prepare($query);
$stmt->bindParam(":username", $username, PDO::PARAM_STR);
$stmt->bindParam(":fullname", $fullname, PDO::PARAM_STR);
$stmt->execute();

使用PDO,您可以将参数绑定到查询。

问题在于$id未被识别。 我将查询中的WHERE子句更改为$username ,它解决了问题。

谢谢所有帮助的人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM