简体   繁体   English

使用提交表单更新数据库

[英]Update database with submission form

I've been trying to figure out why my database won't update! 我一直在试图弄清楚为什么我的数据库不会更新! I know i'm doing something wrong but I don't know what's wrong! 我知道我做错了什么,但我不知道怎么了! here's my code! 这是我的代码! please help. 请帮忙。

        $user_id = (int)$user['id'];
        $editName = $user['full_name'];
        $editEmail = $user['email'];
        $editPermissions = $user['permissions'];

        if(isset($_POST['editUser'])){

        $editName = $_POST['editname'];
        $editEmail = $_POST['editemail'];
        $editPermissions = $_POST['editpermissions'];

            $db->query("UPDATE users SET full_name = '$editName',email = '$editEmail', permissions = '$editPermissions' WHERE id = '$user_id'");

            }

     ?>
        <form action="users.php" method="post">
        <td><input type="text" name="editname" id="editname" class="form-control col-sm-2" value="<?=$editName;?>"></td>
        <td><input type="text" name="editemail" id="editemail" class="form-control col-sm-2" value="<?=$editEmail;?>"></td>
        <td><?=pretty_date($user['join_date']);?></td>
        <td><?=(($user['last_login'] == '0000-00-00 00:00:00')?'never':pretty_date($user['last_login']));?></td>
        <td><input type="text" name="editpermissions" id="editpermissions" class="form-control col-sm-2" 
        value="<?=$editPermissions;?>"></td>    
        <div class="form-group col-md-6 text-right button">
        <a href="users.php" class="btn btn-default">Cancel</a>
        <input type="submit" name="editUser" value="Edit User" class="btn btn-success">
        </div>
        </form>

It looks like you're missing the id of the user in the form. 看来您在表单中缺少用户ID。

Add

<input type="hidden" name="id" value="<?= $user_id ?>" />

Add this to your second if(){} 将此添加到第二个if(){}

$user_id = $_POST['id'];

As a side note, you should not be using <td> in a <form> if they're not in a <tr> and the non-existant <tr> is not in a <table> . 附带说明一下,如果它们不在<tr>并且不存在的<tr>不在<table>则不应在<form>使用<td> <table> Have a look here 在这里看看

Next up, you're <input> 's also do not have a <label> (matched together using for and id attributes). 接下来,您是<input> ,也没有<label> (使用forid属性for匹配)。 Look at this 看这个

Last up, you're <input type="submit" name="editUser" value="Edit User" class="btn btn-success"> could be replaced using a <button> element. 最后,您可以<input type="submit" name="editUser" value="Edit User" class="btn btn-success">可以使用<button>元素替换。 As you're using Bootstrap you could find a proper submit button here 使用Bootstrap时,您可以在此处找到合适的提交按钮

Lastly, the way the code is written is pretty unreadable. 最后,编写代码的方式非常难以理解。 Please read this , commit it to memory and use it ;) 请阅读此内容 ,将其提交到内存中并使用它;)

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

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