我无法获取要动态插入mysqli查询的开关ID。如果我在SQL中插入文本,则返回结果,但$ _GET不起作用。发件人:
function format_switch(sw_id)
{
var xhttp = new XMLHttpRequest();
var sw_id = (sw_id);
xhttp.open("GET", "return.php?sw_id="+sw_id, true);
console.log(sw_id);
xhttp.send();
}
正确的sw_id(switch_1)出现在控制台日志中。
但是此PHP代码:
<?php
ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$switchnumber = $_GET['sw_id'];
$address = "localhost";
$user = "root";
$password = "CBR1000f";
$database = "pinetdb";
$con = new mysqli($address,$user,$password,$database);
$result = mysqli_query($con, "SELECT switchnumber,state FROM pinetdb.Switches WHERE switchnumber = '$switchnumber'");
if (mysqli_num_rows($result) > 0){
if ($row = mysqli_fetch_assoc($result)) {
echo $row["switchnumber"]. ", " .$row["state"] ."<br>";
}
}
?>
返回:
Notice: Undefined index: swt_id in /var/www/pinet/html/return.php on line 4
mysqli_result Object ( [current_field] => 0 [field_count] => 2 [lengths] => [num_rows] => 0 [type] => 0 )
如果我在SQL中输入sw_id的期望值,则代码将返回我的期望结果:
<?php
ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$switchnumber = $_GET[sw_id];
$address = "localhost";
$user = "root";
$password = "CBR1000f";
$database = "pinetdb";
$con = new mysqli($address,$user,$password,$database);
$result = mysqli_query($con, "SELECT switchnumber,state FROM pinetdb.Switches WHERE switchnumber = 'switch_1'");
if (mysqli_num_rows($result) > 0){
if ($row = mysqli_fetch_assoc($result)) {
echo $row["switchnumber"]. ", " .$row["state"] ."<br>";
}
}
?>
返回:
Notice: Undefined index: swt_id in /var/www/pinet/html/return.php on line 4
switch_1, 1
mysqli_result Object ( [current_field] => 0 [field_count] => 2 [lengths] => [num_rows] => 1 [type] => 0 )
符合预期。
我已经从其他响应中得出了这一点(开始安装新的linux),但是找不到解决方法。执行更新查询的非常相似的php可以工作,但是我无法回显已更新的字段。请让我知道我的脚本出了什么问题。
答案 0 :(得分:0)
在您的php中更改此行
$switchnumber = $_GET[sw_id];
到
$switchnumber = $_GET['sw_id'];
答案 1 :(得分:0)
我将一些从W3Schools复制的代码添加到我的js中,并在按钮下而不是在另一页上更新字段。够好了!我的php没什么问题-我想它在脚本完成后不会保留$ _GET详细信息,因此在浏览器中打开.php文件时不会出现。
这是我修改的javascript:
function return_switch(swt_id)
{
var xhttp = new XMLHttpRequest();
var swt_id = (swt_id);
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("switchstate").innerHTML = this.responseText;
}
};
xhttp.open("GET", "return.php?swt_id="+swt_id, true);
console.log(swt_id);
xhttp.send();
}
这是带有<p><\p>
的html,其中包含动态返回的数据库字段:
<!doctype html>
<html>
<head>
<script src="change_switch.js"></script>
</head>
<body>
<h1> Click the button to turn on the Switch</h1>
<br>
<button type="button" onclick="update_switch(this.id); return_switch(this.id)" id="switch_1">Switch 1</button>
<button type="button" onclick="update_switch(this.id); return_switch(this.id)" id="switch_2">Switch 2</button>
<button type="button" onclick="update_switch(this.id); return_switch(this.id)" id="switch_3">Switch 3</button>
<button type="button" onclick="update_switch(this.id); return_switch(this.id)" id="switch_4">Switch 4</button>
<button type="button" onclick="return_switch(this.id); update_switch(this.id)" id="switch_5">Switch 5</button>
<br>
<p id="switchstate"></p>
<br>
<button type="button" onclick="location.href='/index.html'">Home</button>
</body>
</html>