我想用字符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”中的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>
答案 0 :(得分:0)
由于您发送的密钥为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'");
}