gpt4 book ai didi

php - 无法在我的数据库中搜索相关数据 ||错误 : Trying to get property of non-object

转载 作者:行者123 更新时间:2023-11-29 02:12:19 24 4
gpt4 key购买 nike

所以我正在尝试使用文本输入字段来搜索我的数据库,它们都具有名称和 ID 属性,一个具有这两个属性的产品,另一个具有两者的品牌。问题出在 PHP 上,它连接到数据库很好,它声称接受 sql 语句,但是一旦它到达某一行(在下面的代码中上面会有注释),它给了我错误“尝试获取非对象的属性”,我相信数据库搜索首先需要这一行。任何帮助将不胜感激。

<!--This is the form used to submit the query-->

<form action="searchDB.php" method="GET" id="searchForm">
<input type="text" name="product" id="product" placeholder="Product...">
<input type="text" name="brand" id="brand" placeholder="Brand...">
<input id="submit" type="submit" value="Search">

PHP/MySQL:

$product = mysqli_real_escape_string($conn, $_GET['product']);
$brand = mysqli_real_escape_string($conn, $_GET['brand']);
$conn = new mysqli($servername, $username, $password, $dbase);

if(empty($_GET['product']||$_GET['brand'])){ //fields contain no value
die('please input a search!'); // kill the database connection
}

if ($conn->connect_error) {
echo "Connection failed: ". $conn->connect_error;
}else{
$sql = "SELECT * FROM products WHERE product = $product AND brand = $brand";
$result = $conn->query($sql);
//echo $sql;
}

//THIS IS WHERE IT SAYS THE ERROR OCCURS, "if($result->num_rows>0"
//specifically
if ($result->num_rows > 0){
//output each row
while($row = $result->fetch_assoc()){
echo "Product: " . $row["product"]. " Brand: " . $row["brand"]. "
Age: " . $row["age"]. " Cost: " . $row["cost"]. " Specification: " .
$row["specification"]. " Image: " . $row["image"]. "<br>";
}
}else{
echo "Sorry, we could not find any products relating to your search.";
}

mysqli_close($conn);

最佳答案

正如我在评论中所说,在建立连接之前,您不能使用转义函数。

您的连接逻辑也需要修改,所以这就是我们最终的结果,以及一些修改,例如添加 empty()到另一个 GET 数组。

我还在 VALUES 中引用了变量,因为它们是字符串。

确保这两个值都得到满足,否则您可能需要更改 ANDOR运营商。

$conn = new mysqli($servername, $username, $password, $dbase);

if ($conn->connect_error) {
echo "Connection failed: ". $conn->connect_error;
}

if(empty($_GET['product']) || empty($_GET['brand']) ){
die('please input a search!');
}else{

$product = mysqli_real_escape_string($conn, $_GET['product']);
$brand = mysqli_real_escape_string($conn, $_GET['brand']);

$sql = "SELECT * FROM products WHERE product = '$product' AND brand = '$brand'";
$result = $conn->query($sql);

if(!$result){

echo "The query failed: " . mysqli_error($conn);

}

if ($result->num_rows > 0){
//output each row
while($row = $result->fetch_assoc()){
echo "Product: " . $row["product"]. " Brand: " . $row["brand"]. "
Age: " . $row["age"]. " Cost: " . $row["cost"]. " Specification: " .
$row["specification"]. " Image: " . $row["image"]. "<br>";
}
}else{
echo "Sorry, we could not find any products relating to your search.";
}

}

mysqli_close($conn);

而且正如我在评论中所述,如果您发布的是您的实际代码,则它缺少结尾 </form>标签。

使用 mysqli_error($conn) 检查查询错误.

注意:使用准备好的语句,它们可以更好地帮助防止 SQL 注入(inject)。

关于php - 无法在我的数据库中搜索相关数据 ||错误 : Trying to get property of non-object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48503941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com