Ajax 调用不会更新我的行


Ajax call isn't update my rows

我想用字符的 id 更新我的数据库,但是当我将它们放入插槽时,它不会更新我希望它更新的行。我的问题是,您能否指出如何正确编码或修复错误的正确方向?

function updateTeam(){
var team = '', slot = [];
if($('input[name=s0]').val()!=""){
    slot.push($('input[name=s0]').val());
}
if($('input[name=s1]').val()!=""){
    slot.push($('input[name=s1]').val());
}
if($('input[name=s2]').val()!=""){
    slot.push($('input[name=s2]').val());
}
$.each(slot, function(i,e){
    if(i == 0) team = e;
    else team = team + ',' + e;
});
$.ajax({
       url : _path + "/core/ajax.php",
       type : 'POST',
       data : { f: 'setTeam', i: team},
       dataType : 'json',
       success :  function(data) {
        if(data.error){
            errorMessage('Error: ' + data.error, data.error, data.error);
        }
    }
});
}

菲律宾比索

function clean($content) {
    $content = mysql_real_escape_string(htmlspecialchars($content));
    return $content;
}
//Update the user team.
if (isset($_POST['f']) && $_POST['f'] == 'updateTeam')  {
if (isset($_POST['s0'])) {
        $cid1 = $secure->clean($_POST['s0']);
    } else {
        $cid1 = '1';
    }
if (isset($_POST['s1'])) {
        $cid2 = $secure->clean($_POST['s1']);
    } else {
        $cid2 = '2';
    }
if (isset($_POST['s2'])) {
        $cid1 = $secure->clean($_POST['s2']);
    } else {
        $cid1 = '3';
    }
$updateTeam = $db->query("UPDATE accounts SET cid1 = '$cid1', cid2 = '$cid2', cid3 = '$cid3' WHERE id = '$id'");
}

当我用谷歌浏览器检查元素时,它说 i:1,5,2。展示我将如何更新我的行,以便"i"= $cid 1、5=$cid 2 和 2= cid3 中的 1 是我的 php 代码错误吗?目录:

 <div id="droppable_slots" class="current_team">
                    <div class="slot 1">1</div>
                    <input type="hidden" name="s0" value="10">
                    <div class="slot 2">2</div>
                    <input type="hidden" name="s1" value="7">
                    <div class="slot 3">3</div>
                    <input type="hidden" name="s2" value="3">
                </div>

由于您发送的是一个带有 i 键的 csv,您需要通过在,上爆炸来从$_POST['i']中获取您的值。所以你的代码可以更新成类似的东西——

//Update the user team.
if (isset($_POST['f']) && $_POST['f'] == 'updateTeam')  {
//Explode the i post
if (isset($_POST['i'])) { 
        $vals = explode("," , $_POST['i'] );
    }
if (isset($vals[0])) {
        $cid1 = $secure->clean($vals[0]);
    } else {
        $cid1 = '1';
    }
if (isset($vals[1])) {
        $cid2 = $secure->clean($vals[1]);
    } else {
        $cid2 = '2';
    }
if (isset($vals[2])) {
        $cid1 = $secure->clean($vals[2]);
    } else {
        $cid1 = '3';
    }
$updateTeam = $db->query("UPDATE accounts SET cid1 = '$cid1', cid2 = '$cid2', cid3 = '$cid3' WHERE id = '$id'");
}