【问题标题】:XMLHttpRequest GET value will not input into mysqli_query SQLXMLHttpRequest GET 值不会输入到 mysqli_query SQL
【发布时间】:2019-02-23 12:32:18
【问题描述】:

我无法获取开关 ID 以动态插入 mysqli 查询。如果我在 SQL 中插入文本 if 返回结果,但 $_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 有效,但我无法回显更新的字段。请让我知道我的脚本出了什么问题。

【问题讨论】:

  • 去掉print_r ($result); 那里没有功能。您已经从获取的结果集中回显了数据
  • 而且看起来查询只有一个结果,您不需要 while 循环来获取结果集
  • $switchnumber = $_GET[sw_id]; 应该是$switchnumber = $_GET['sw_id'];
  • $result_free(); 也没有意义,是语法错误。
  • 你似乎没有使用 SESSION 所以我也不确定为什么这条线在这里session_unset();

标签: javascript php html mysqli xmlhttprequest


【解决方案1】:

在你的 php 中更改这一行

$switchnumber = $_GET[sw_id];

$switchnumber = $_GET['sw_id'];

【讨论】:

    【解决方案2】:

    我将从 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();
      }
    

    这是包含 &lt;p&gt;&lt;\p&gt; 以包含动态返回的数据库字段的 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>
    

    【讨论】:

      猜你喜欢
      • 2013-03-16
      • 2015-11-10
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      • 2018-06-30
      • 2013-04-08
      • 2017-06-22
      • 1970-01-01
      相关资源
      最近更新 更多