0

我想用字符的 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);
        }
    }
});
}

php

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”中的 1=$cid1、5=$cid2 和 2=cid3,是我的 php 代码错误吗?html:

 <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>
4

1 回答 1

0

由于您发送的 csv 的键为i,因此您需要$_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'");
}
于 2013-10-12T03:36:48.530 回答