【问题标题】:Ajax request returns "mysqli_stmt_execute(): Property access is not allowed"Ajax 请求返回“mysqli_stmt_execute(): Property access is not allowed”
【发布时间】:2021-10-23 20:22:09
【问题描述】:

我有一个输入表单,当输入某个公司的名称 (onkeyup) 时,会从 SQL 数据库中获取颜色值。

HTML:

<form id="changeForm" action="includes/tri-inc.php" method="post" style="width: 205px;">
<input id="hiddenId" type="hidden" name="verseid" value="23">
<input id="hiddenArea" type="hidden" name="hiddenArea" value="detail">
<input name="kategorie" type="text" placeholder="Kategorie" value="GA/MSRL"><br>
<input name="firma" onkeyup="showColor(this.value)" type="text" placeholder="Firmenname" value=""><br>
<input id="color" name="color" type="color" value="#FF22FF"><br>
<input name="person" type="text" placeholder="Kontaktperson" value=""><br>
<input name="adresse" type="text" placeholder="Adresse" value=""><br>
<input name="email" type="text" placeholder="Email-Adresse" value=""><br>
<input name="telefon" type="text" placeholder="Telefonnummer" value=""><br>
<input type="submit" name="submit">
</form>

javascript:

    function showColor(str) {
    if (str.length == 0) {
        document.getElementById('color').value = "#808080";
        return;
    } else {
        const xmlhttp = new XMLHttpRequest();
        xmlhttp.onload = function() {
            document.getElementById("color").value = this.responseText;
        }
    xmlhttp.open("GET", "includes/getColor.php?c=" + encodeURIComponent(str));
    xmlhttp.send();
    }
}

PHP:

<?php
    $c =$_REQUEST["c"];
    require 'database.php';

    if ($c !== "") {
        $sql = "SELECT color FROM dreiecke WHERE firma = '" .urldecode($c). "'";
        $stmt = mysqli_stmt_init($conn);
        mysqli_stmt_execute($stmt);
        $result = mysqli_stmt_get_result($stmt);
        $result = $result[0];
        echo $result === null ? "#ff22ff" : $result;
    } else {
        echo "#ff22ff";
    }
?>

该命令未正确触发并将默认 #000000 返回到颜色输入字段的值。

控制台显示:“mysqli_stmt_execute(): Property access is not allowed”

我哪里错了?

【问题讨论】:

  • 警告:您对SQL Injections 持开放态度,应该使用参数化的prepared statements,而不是手动构建查询。它们由PDOMySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your dataEscaping is not enough!
  • 您使用的是哪个 PHP 版本?
  • 问题很简单。你忘了准备准备好的陈述。您已经定义了 SQL 字符串,但您从未调用过 prepare
  • 我使用的是 PHP 版本 7.3.28

标签: php mysql ajax forms mysqli


【解决方案1】:

感谢 Dharman 的 cmets,我设法弄明白了:

<?php
    $c =$_REQUEST["c"];
    require 'database.php';
    $defaultColor = "@808080";

    if ($c !== "") {
        $c = urldecode($c);
        $stmt = $conn->prepare("SELECT color FROM dreiecke WHERE firma=?");

        $stmt->bind_param("s", $c);

        $stmt->execute();

        $stmt->bind_result($result);
        $stmt->fetch();

        $result = substr($result, 0, 7);
        if ($result != null) {
            echo $result == null ? "#ffffff" : $result;
        } 
        return;
    } 
    echo $defaultColor;
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-20
    • 2018-05-01
    • 2016-12-04
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    相关资源
    最近更新 更多