我在向数据库添加记录时遇到问题。更具体地说,静态值正确地添加到表中,但通过查看文本字段中的值,它们是EMPTY。表中为0。我检查了SQL查询,发现我在某处有错误,因为从文本字段获取的值是空的。
下面我将展示与此案例相关的所有功能,希望您能发现我想要解决的错误。
包含SQL查询的函数。
function updateGuestSet($no_of_keys, $no_of_fobs) {
$today = date('Y-m-d H:i:s');
$keys = array();
try {
$transaction = new Transaction();
$query = 'INSERT INTO `keysets` (`noKeys`, `noFobs`, `masterSet`, `createdate`, `createby`, `keys_idkey`)VALUES("' .$no_of_keys. '","' .$no_of_fobs . '","0","' . $today . '","Jarek","2")';
echo($query);
die;
$keys = QueryExecutor::execute(new SqlQuery($query));
$transaction -> commit();
} catch(Exception $e) {
echo $e;
}
return $keys;
}
这是用于显示/添加/删除字段集
的函数function show_add_guestFields(){
$('#guestSet').append('<div class="fields"><label for="keys">No. of keys</label><input type="number" name="keys" class="keys" id="keys" onkeypress="return isNumberKey(event);" style="margin: 0px 0px 0px 65px;"/><br/><br/><label for="fobs">No. of fobs</label><input type="number" name="fobs" class="fobs" id="fobs" onkeypress="return isNumberKey(event);" style="margin: 0px 0px 0px 68px;"/><br/><br/><button onClick="valid();">Submit</button><button class="remove" style="margin:0px 0px 10px 0px;">Cancel</button></div>');
$('#guestSet').on('click', '.remove', function(){
$(this).parent().remove();
return false;
});
}
与PHP代码相关的功能
function updateGuestSet(){
var no_of_keys = $('#keys').val();
var no_of_fobs = $('#fobs').val();
$.ajax({
type: 'POST',
url: 'propertyId.php',
data:{
update:'keySet',
no_of_keys:no_of_keys,
no_of_fobs:no_of_fobs
},
async: true,
dataType:'json',
success: function(){
}
});
}
最后,这是PHP代码;
if($_POST['update'] == 'keySet'){
$no_of_keys = $_POST['keys'];
$no_of_fobs = $_POST['fobs'];
//echo("test");
$insert_keySet = updateGuestSet($no_of_keys, $no_of_fobs);
}
这是我在回显sql查询时得到的结果
INSERT INTO keysets (noKeys, noFobs, masterSet, createdate, createby, keys_idkey)VALUES("","","0","2014-11-18 17:37:37","Jarek","2")
感谢所有人的帮助!!
答案 0 :(得分:1)
所需的POST键:
keys
fobs
实际的POST键:
no_of_keys
no_of_fobs
让它们变得一样。