dpsfay2510 2015-07-15 14:52
浏览 189

通过获取用户ID并发送AJAX调用来更新数据库

I have a page that I accept or deny new users. When someone is accepted they go to a section called Accepted Users and in that section the admin have to change their permission level or group#. So in the following I am getting the user_id from the user_request table for that specific user and wanting to update my users table by the id. I am only wanting to update the 'group' by the option box through an AJAX call to my php file.

Does anyone see what I'm doing wrong? Nothing is being updated into my db. I get no errors in the console,

<?php
$con2 = mysqli_connect("localhost", "root", "", "db");
$run2 = mysqli_query($con2,"SELECT * FROM user_requests ORDER BY id DESC");
$runUsers2 = mysqli_query($con2,"SELECT * FROM users ORDER BY id DESC");
$numrows2 = mysqli_num_rows($run2);

    if( $numrows2 ) {
        while($row2 = mysqli_fetch_assoc($run2)){
            if($row2['status'] == "Approved"){

                $approved_id        = $row2['user_id'];
                $approved_firstname = $row2['firstname'];
                $approved_lastname  = $row2['lastname'];
                $approved_username  = $row2['username'];

    if ($approved_firstname == true) {
        echo "Name - ". $approved_firstname . " " . $approved_lastname . "</br>" . 
                "Username - ". $approved_username . "</br></br>"
?>
<div class="change_group_button"> 
     <a class="change_group" href="javascript:void(0)">Change User Permission</a>
</div><br>
<div id="light" class="change_group_popup">
    <a class="close" href="javascript:void(0)">Close</a>

    <form id="update_group" name="Group" action="" method="POST" accept-charset="utf-8">
       <div class="field">
        <label for="group">Group</label>
        <select value='<?php echo $approved_id; ?>' id='approved_id' name='group' required>
            <option value=''><?php echo htmlentities($group); ?></option>
            <option value="1">Bench</option>
            <option value="2">Spectator</option>
            <option value="3">Team Member</option>
            <option value="4">Commissioner</option>
        </select>
    </div>
    <input type="submit" value="submit">
    </form>

AJAX call. I am not sure if my data part is right..

//AJAX call for updating the group
$(document).ready(function () {

    $('#update_group').on('submit', function (event) {
    event.preventDefault();
        $.ajax({
            url: 'user_group_update.php',
            type: 'POST',
            data: {
                id: $(this).val(), //id
                update_group: $(this).val() //group level
            },
            success: function (data) {
                //do something with the data that got returned
                $("#success").fadeIn();
                $("#success").show();
                $('#success').html('User Permission Level Changed!');
                $('#success').delay(5000).fadeOut(400);
            },
             error: function(jqXHR, textStatus,errorThrown )
            {
              // alert on an http error 
              alert( textStatus +  errorThrown );
            }
        });
        return false;
    });
});

user_group_update file

$approved_id = $_POST['id'];
$change_group = $_POST['update_group'];

$con = mysqli_connect("localhost","root","","db");
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s
", mysqli_connect_error());
        exit();
    }
    $stmt = $con->prepare("UPDATE users SET group=? WHERE id=?");
    if ( false===$stmt ) {
     // Check Errors for prepare
        die('User Group update prepare() failed: ' . htmlspecialchars($con->error));
    }
    $stmt->bind_param('si', $change_group, $approved_id);
    if ( false===$stmt ) {
    // Check errors for binding parameters
        die('User Group update bind_param() failed: ' . htmlspecialchars($stmt->error));
    }
    $stmt->execute();
    if ( false===$stmt ) {
        die('User Group update execute() failed: ' . htmlspecialchars($stmt->error));
    }
  • 写回答

1条回答 默认 最新

  • drxvjnx58751 2015-07-15 15:04
    关注

    I see some problems:

    1) you are combining the group id and approved id into one select element. that's not how forms work. you need a separate field for approved id, something like:

    <input type="hidden" value="<?php echo $approved_id; ?>" id="approved_id" name="id" />
    <select id='group_id' name='group' required>
        <option value=''><?php echo htmlentities($group); ?></option>
        <option value="1">Bench</option>
        <option value="2">Spectator</option>
        <option value="3">Team Member</option>
        <option value="4">Commissioner</option>
    </select>
    

    2) your data array should look like this:

    data: {
        id: $("#approved_id").val(), //id
        update_group: $("#group_id").val() //group level
    }
    

    3) error checking is wrong in user_group_update, should be:

    $stmt = $con->prepare("UPDATE users SET group=? WHERE id=?");
    if ( !$stmt || $con->error ) {
     // Check Errors for prepare
        die('User Group update prepare() failed: ' . htmlspecialchars($con->error));
    }
    if(!$stmt->bind_param('si', $change_group, $approved_id)) {
    // Check errors for binding parameters
        die('User Group update bind_param() failed: ' . htmlspecialchars($stmt->error));
    }
    if(!$stmt->execute()) {
        die('User Group update execute() failed: ' . htmlspecialchars($stmt->error));
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题