我正在使用Jquery Bootstrap将数据添加到MYSQL,但它不允许值为零。
这是我的剧本:
<script type="text/javascript">
jQuery(document).ready(function() {
$.fn.editable.defaults.mode = 'popup';
$('.xedit').editable();
$(document).on('click','.editable-submit',function(){
var x = $(this).closest('.editable-container').prev().attr('data-id');
var key = $(this).closest('.editable-container').prev().attr('id');
var y = $('.input-sm').val();
var z = $(this).closest('.editable-container').prev();
$.ajax({
url: "add_preds.php?id="+x+"&pred="+y+"&key="+key,
type: 'GET',
success: function(s){
if(s == 'status'){
$(z).html(y);}
if(s == 'error') {
alert('Error Processing your Request!');}
},
error: function(e){
alert('Error Processing your Request!!');
}
});
});
});
</script>
这是add_preds.php:
$id = $mysqli->real_escape_string($_GET['id']);
$pred_org = $mysqli->real_escape_string($_GET['pred']);
$key = $mysqli->real_escape_string($_GET['key']);
//Only numbers
$pred = preg_replace('/\D/', '', $pred_org);
//Fetch user id
$qus = "SELECT id, admin FROM fxweb_users WHERE username = '".htmlentities($_SESSION['username'])."'";
$rus = mysqli_query($mysqli, $qus);
$rowus = mysqli_fetch_array($rus);
$userid = $rowus['id'];
$is_admin = $rowus['admin'];
//Check if match exists
$qm = "SELECT * FROM fxweb_predictiondata WHERE matchid = ".$id." AND userid = ".$userid."";
$rm = mysqli_query($mysqli, $qm);
$rowm = mysqli_fetch_array($rm);
$predhome = $rowm['homescore'];
$predaway = $rowm['awayscore'];
//Define date and time
$date = date('Y-m-d H:i:s');
if ($key == 1 && $pred) {
if ($predhome >= 0) {
//a real user posted a real reply
if ($insert_stmt = $mysqli->prepare("UPDATE fxweb_predictiondata SET homescore=? WHERE matchid=? AND userid=?")) {
$insert_stmt->bind_param('sss', $pred, $id, $userid);
// Execute the prepared query.
$insert_stmt->execute();
}
}
}
if ($key == 1 && !$predhome && !$predaway && $pred) {
if ($insert_stmt = $mysqli->prepare("INSERT INTO fxweb_predictiondata (userid, matchid, predtime, homescore) VALUES (?, ?, ?, ?)")) {
$insert_stmt->bind_param('ssss', $userid, $id, $date, $pred);
// Execute the prepared query.
$insert_stmt->execute();
}
}
if ($key == 2 && $pred) {
if ($predaway >= 0 ) {
//a real user posted a real reply
if ($insert_stmt = $mysqli->prepare("UPDATE fxweb_predictiondata SET awayscore=? WHERE matchid=? AND userid=?")) {
$insert_stmt->bind_param('sss', $pred, $id, $userid);
// Execute the prepared query.
$insert_stmt->execute();
}
}
}
if ($key == 2 && !$predaway && !$predhome && $pred) {
if ($insert_stmt = $mysqli->prepare("INSERT INTO fxweb_predictiondata (userid, matchid, predtime, awayscore) VALUES (?, ?, ?, ?)")) {
$insert_stmt->bind_param('ssss', $userid, $id, $date, $pred);
// Execute the prepared query.
$insert_stmt->execute();
}
}
//Admin enter match results
if ($key == 3 && $pred && $is_admin = 1) {
if ($predhome >= 0) {
//a real user posted a real reply
if ($insert_stmt = $mysqli->prepare("UPDATE fxweb_matches SET homescore=? WHERE id=?")) {
$insert_stmt->bind_param('ss', $pred, $id);
// Execute the prepared query.
$insert_stmt->execute();
}
}
}
if ($key == 4 && $pred && $is_admin = 1) {
if ($predaway >= 0 ) {
//a real user posted a real reply
if ($insert_stmt = $mysqli->prepare("UPDATE fxweb_matches SET awayscore=? WHERE id=?")) {
$insert_stmt->bind_param('ss', $pred, $id);
// Execute the prepared query.
$insert_stmt->execute();
}
}
}
该脚本运行良好,直到我尝试向输入添加“0”,它不允许它并抛出错误警告!
有人知道解决这个问题吗?
答案 0 :(得分:0)
我找到了解决方案:
我在add_preds.php文件中有一个“if”语句,用于搜索预测值:
if ($key == 1 && $pred)
但是如果在输入字段中输入零(0),似乎jquery / Bootstrap没有传递值,因此数据库查询不成功!
我只是从if语句中删除了预测值:
if ($key == 1)
然后它运作正常!
我还在jquery脚本中将“GET”更改为“POST”,如评论中所述。