AJAX调用和UPDATE查询无法正常运行

时间:2015-08-27 17:58:33

标签: javascript php jquery mysql ajax

我正在尝试通过AJAX调用向我的update_divisions.php文件发送UPDATE查询以更新播放器的部门。我有一个选择框,我可以从中选择用户,然后是另一个选择框,我可以选择我要发送的部门。我没有包括这个,但是我有一个AJAX调用,它从一个php文件中获取信息,显示用户名和他们当前所在的部门。这很有效。所以,我现在只是想更新该部门。我有php错误代码,我没有得到任何错误。我点击提交发送以下内容后,我的网络标签中出现错误:

我的AJAX调用中的这一行没有语法错误...

"username="+$user,

我做错了什么?

这是我的完整代码。

try {
//Prepare
if ($division_stmt= $con->prepare("SELECT * FROM team_rankings WHERE user_id=user_id")) {

    $division_stmt->execute();
    $division_stmt->bind_result($division_id, $division_user_id, $division_firstname, $division_username, $division_division, $division_wins, $division_losses); 

    //var_dump($division_stmt);

    if (!$division_stmt) {
        throw new Exception($con->error);
    }

    $division_stmt->store_result();
    echo "<span class='top_bottom_margin'>Select a user to modify their team rank</span>". "<br>";
    echo "<select id = 'member_division'>";
    while ($division_row = $division_stmt->fetch()) {
    echo "<option value='{$division_username}' data-username='{$division_username}'>{$division_username}</option>";
    }
    echo "</select>";


} else {
    echo "<p>There are not any team players yet.</p>";
}
}
catch (Exception $e)
{
    echo "Error: " . $e->getMessage();
}
?>

            <label>Current Division
                <input type="text" id="current_division">
            </label>
            <form name="update_group_form" action="" type="POST">   
                <select name="division_name">
                    <option value="1">East</option>
                    <option value="2">West</option>
                </select>
                <input type="submit" value="submit" name="division_update_button">
            </form> 

AJAX调用

$(document).ready(function(){ 
        $("#update_group_form").on("change", function(){
            $user = this.value;
            $.ajax({ 
                url: "update_division.php", 
                type: "POST",
                data: {
                "username="+$user,
                division_name: $(this).find('select[name="group_id"]').val()
                },
                success: function(text){ 
                     alert(data);
            },
             error: function(jqXHR, textStatus,errorThrown )
            {
              // alert on an http error 
              alert( textStatus +  errorThrown );
            }
        });
        return false;
    });
}); 

PHP文件 - update_division.php

$update_division = $_POST['division_name'];


$con = mysqli_connect("localhost","","","");
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    $stmt = $con->prepare("UPDATE division FROM team_rankings WHERE username = :user");
    if ( !$stmt || $con->error ) {
     // Check Errors for prepare
        die('User Group update prepare() failed: ' . htmlspecialchars($con->error));
    }
    if(!$stmt->bind_param('i', $update_division)) {
    // 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));
    }

1 个答案:

答案 0 :(得分:0)

我正试图合并php和jquery,我可以看到。

尝试改变这一点:

 $user = this.value;

为此:

var user = this.value;

而且:

"username="+$user,

为此:

"username="+user,

告诉我它是否有效。