使用xmlhttprequest将JavaScript变量发送到PHP以查询MySQL。不起作用
我正在尝试查询我的MySQL数据库。我的HTML文件中的JavaScript代码是...
<script>
function getGameNumberOfPlays() {
var sentValue1 = Number;
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("gameNumberOfPlays").innerHTML = this.responseText;
}
};
xhttp.open("GET", "numberOfPlays.php", true);
xhttp.send(sentValue1);
}
</script>
'numberOfplays.php'代码是...
<?php
$dbhost = 'localhost';
$dbuser = 'xxx';
$dbpwd = 'xxx';
$dbname = 'xxx';
$conn = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );
$p1 = $_GET['sentValue1'];
$sql= "SELECT COUNT(ID) FROM `data` WHERE `gameNumber` = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $p1);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_array();
echo "$user[0]";
$conn->close();
?>
上面的代码在我的HTML页面中产生0。
但是,如果我将'numberOfplays.php'的代码更改为...
<?php
$dbhost = 'localhost';
$dbuser = 'xxx';
$dbpwd = 'xxx';
$dbname = 'xxx';
$conn = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );
$p1 = $_GET['sentValue1'];
$p2 = 171; // or any number that's in the database
$sql= "SELECT COUNT(ID) FROM `data` WHERE `gameNumber` = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $p2);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_array();
echo "$user[0]";
$conn->close();
?>
我在HTML页面中获得了正确的数字,这样看来编码还可以,除了如何发送和接收变量'sentValue1'。
如果有人能让我知道如何更改代码,以便MySQL查询与变量“ SentValue1”一起使用,我将非常感激。
数据库中的“ GameNumber”列是这样配置的...
column'gamenumber'gamenumber'gamenumber'
非常感谢。
I'm trying to query my MySQL database. The Javascript code in my html file is ...
<script>
function getGameNumberOfPlays() {
var sentValue1 = Number;
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("gameNumberOfPlays").innerHTML = this.responseText;
}
};
xhttp.open("GET", "numberOfPlays.php", true);
xhttp.send(sentValue1);
}
</script>
The 'numberOfPlays.php' code is ...
<?php
$dbhost = 'localhost';
$dbuser = 'xxx';
$dbpwd = 'xxx';
$dbname = 'xxx';
$conn = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );
$p1 = $_GET['sentValue1'];
$sql= "SELECT COUNT(ID) FROM `data` WHERE `gameNumber` = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $p1);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_array();
echo "$user[0]";
$conn->close();
?>
The code above produces 0 in my html page.
However, if I change the code of 'numberOfPlays.php' to ...
<?php
$dbhost = 'localhost';
$dbuser = 'xxx';
$dbpwd = 'xxx';
$dbname = 'xxx';
$conn = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );
$p1 = $_GET['sentValue1'];
$p2 = 171; // or any number that's in the database
$sql= "SELECT COUNT(ID) FROM `data` WHERE `gameNumber` = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $p2);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_array();
echo "$user[0]";
$conn->close();
?>
I get the correct number in my html page so it appears that the coding is OK except for how the variable 'sentValue1' is sent and received.
I'd be very grateful if anyone could let me know how to change the code so that MySQL query works with the variable 'sentValue1'.
The 'gameNumber' column in the database is configured like this ...
Screenshot of column 'gameNumber'
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有在XHTTP请求
更改中发送
sentValue1
:to:
or:
您可以检查此 link
You did not send the
sentValue1
in your xhttp requestchange:
to:
or:
You can check this link