使用xmlhttprequest将JavaScript变量发送到PHP以查询MySQL。不起作用

发布于 2025-02-09 02:21:20 字数 1916 浏览 1 评论 0原文

我正在尝试查询我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秉烛思 2025-02-16 02:21:20

您没有在XHTTP请求

更改中发送sentValue1

xhttp.open("GET", "numberOfPlays.php", true);
xhttp.send(sentValue1);

to:

let URL = "numberOfPlays.php?sentValue1=<number>"; 
xhttp.open("GET", URL, true); 
xhttp.send();

or:

xhttp.open("GET", "numberOfPlays.php", true);
xhttp.send(sentValue1=<number>);

您可以检查此 link

You did not send the sentValue1 in your xhttp request

change:

xhttp.open("GET", "numberOfPlays.php", true);
xhttp.send(sentValue1);

to:

let URL = "numberOfPlays.php?sentValue1=<number>"; 
xhttp.open("GET", URL, true); 
xhttp.send();

or:

xhttp.open("GET", "numberOfPlays.php", true);
xhttp.send(sentValue1=<number>);

You can check this link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文