Ajax 请求 returns "mysqli_stmt_execute(): Property access is not allowed"
Ajax request returns "mysqli_stmt_execute(): Property access is not allowed"
我有一个输入表单,当输入某个公司的名称 (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";
}
?>
该命令未正确触发,returns 颜色输入字段的默认值 #000000。
控制台显示:“mysqli_stmt_execute(): 属性 不允许访问”
我哪里错了?
感谢 Dharman 的评论,我设法弄明白了:
<?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;
?>
我有一个输入表单,当输入某个公司的名称 (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";
}
?>
该命令未正确触发,returns 颜色输入字段的默认值 #000000。
控制台显示:“mysqli_stmt_execute(): 属性 不允许访问”
我哪里错了?
感谢 Dharman 的评论,我设法弄明白了:
<?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;
?>