gpt4 book ai didi

php - html 值和准备好的语句

转载 作者:行者123 更新时间:2023-11-29 10:50:31 26 4
gpt4 key购买 nike

MySQL 数据库由以下列名组成:id、store、results,有 350 行。

我有一个名为“随机播放”的按钮。当我单击该按钮时,我会从 MySQL 数据库(selectshuffle.php)打印 3 个随机行。每行结果都有一个“选择”按钮(getdata.php)

当我单击一行上的“选择”按钮时,列名“记录”应更新为 +1。

我可以在我发布的代码中使用 +1 更新该行,并收到消息“Updated Succesfull”,但前提是我使用如下数字设置值:

<input type="hidden" value="333" name="id" />

但我需要更新列名“结果”,在该 ID 上我单击“选择”。我想我可以这样做:<input type="hidden" value="<?php $_POST['id']; ?>" name="id" /> 。使用此代码,我仍然收到消息“Updated Succesfull”,但没有行受到影响。

有人能看出我做错了什么吗?

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">&times;</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>

最佳答案

给你的id input一个id,这将用于设置从getdata.php返回的id

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

关于php - html 值和准备好的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43824155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com