2013-10-12 84 views
0

我想用字符的ID來更新我的數據庫,但是當我將它們放入插槽中時,它不更新我希望它更新的行。我的問題是,你能指出我如何正確編寫代碼或糾正錯誤的正確方向嗎?Ajax調用不會更新我的行

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'"); 
} 

當我檢查與谷歌瀏覽器的元素,它說我:1,5,2。顯示如何更新我的行,以便「i」= $ cid1,5 = $ cid2和2 = cid3中的1是我的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> 

回答

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'"); 
}