MySQL数据库由以下列名组成:id,store,results,并且有350行。
我有一个名为" Shuffle"的按钮。当我单击该按钮时,我从MySQL数据库中打印出3个随机行(choiceshuffle.php)。每行结果都有一个" Select"按钮(访问getdata.php)
当我点击按钮"选择"在一行,列名"记录"应该以+1更新。
我可以在我发布的代码中使用+1更新该行,并获取消息"更新Succesfull",但仅当我使用如下数字设置值时:
<input type="hidden" value="333" name="id" />
但我需要更新列名&#34;结果&#34;,在该ID上点击&#34;选择&#34;上。我以为我可以这样做:<input type="hidden" value="<?php $_POST['id']; ?>" name="id" />
。使用此代码,我仍然收到消息&#34;更新了Succesfull&#34;,但没有行受到影响。
有人能看出我做错了什么吗?
的index.php
<form action="updaterecords.php" method="post">
<input type="hidden" value="333" name="id" />
<button type="submit" name="selectStore" >Select</button>
<button type="button" data-dismiss="modal">Close</button>
</form>
updaterecords.php
<?php error_reporting(E_ALL); ini_set('display_errors', 1);
if (mysqli_connect_errno()) { echo "Error: no connexion allowed : " . mysqli_connect_error($mysqli); }
?>
<?php
include 'dbconnection.php';
if(isset($_POST['selectStore'])) {
$id = $_POST['id'];
$stmt = $mysqli->prepare("UPDATE stores SET records = records + 1 WHERE id = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->close();
$mysqli->close();
if($stmt) {
echo "Updated Succesfull";
} else {
echo "Failed: " . $stmt->error;
}
}
?>
访问getdata.php
<?php
include 'dbconnection.php';
if(isset($_POST['post_id'])) {
$id = $_POST['post_id'];
$sql = "SELECT id, store, results FROM stores WHERE id = ".$id;
$res = $mysqli->query($sql);
if($res){
$data = $res->fetch_assoc();
echo json_encode($data);
}else{
echo "no results: <br>";
echo $sql;
}
}
?>
selectshuffle.php
<?php
include 'dbconnection.php';
$sql = "SELECT id, store, results FROM stores WHERE 1=1 ORDER BY RAND ( ) LIMIT 3";
$res = $mysqli->query($sql);
if ($res->num_rows > 0) {
while($row = $res->fetch_assoc()) {
echo "Id: " . $row["id"]. "<br>" .
"Store: " . $row["store"]. "<br>" .
"Result: " . $row["results"]. "<br><br>".
'<div class="btn btn-warning btn-open-modal" data-id="'.$row["id"].'">See Details '.'</div><br><br>';
}
} else {
echo "There is no results";
}
?>
<div class="modal fade" id="editBox" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"><center>Get Heading</center></h4>
</div>
<div class="modal-body">
<div class="form-data"></div>
<p>Get Description</p>
</div>
<div class="modal-footer msg">
<form action="updaterecords.php" method="post">
<input type="hidden" value="333" name="id" />
<button type="submit" name="selectStore" >Select</button>
<button type="button" data-dismiss="modal">Close</button>
</form>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script>
$(document).ready(function() {
$(".btn-open-modal").click(function() {
var id = $(this).data("id");
$.ajax({
type: 'post',
url: 'getdata.php',
data: {
post_id: id
},
success: function(data) {
console.log(data);
var jdata = JSON.parse(data);
if (jdata) {
console.log("is json");
$("#editBox").modal().show();
$("#editBox .modal-title").html(jdata.headline);
$("#editBox .modal-body").html("Store: " + jdata.store + "<br><br>Result: " + jdata.results); // + " " + $query $query => query
} else {
console.log("not valid json: " + data);
}
}
});
});
});
</script>
答案 0 :(得分:1)
将您的身份input
设为ID,这将用于设置从getdata.php
<form action="page.php" method="post">
<input type="hidden" id="store-id" value="" name="id" />
<button type="submit" name="selectStore" >Select</button>
<button type="button" data-dismiss="modal">Close</button>
</form>
然后在你的ajax调用中设置输入的值。
$(".btn-open-modal").click(function() {
var id = $(this).data("id");
$.ajax({
type: 'post',
url: 'getdata.php',
data: {
post_id: id
},
success: function(data) {
console.log(data);
var jdata = JSON.parse(data);
if (jdata) {
$("#editBox").modal().show();
$('#store-id').val(jdata.id); // set the value from the returned data
$("#editBox .modal-title").html(jdata.headline);
$("#editBox .modal-body").html("Store: " + jdata.store + "<br><br>Result: " + jdata.results); // + " " + $query $query => query
} else {
console.log("not valid json: " + data);
}
}
});
});
答案 1 :(得分:0)
您将$ id作为int
传递。从POST获取值时,它将返回为字符串。
替换:
$stmt->bind_param('i', $id);
与
$stmt->bind_param('s', $id);
或强>
您可以将字符串转换为int并将其作为整数传递。
编辑:检查UPDATE
查询是否有任何错误:How to display errors for my mysqli query