创建一个搜索表单,我哪里出错了?

lndjwyie  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(266)

我是新来的php和创建一个搜索表单,其中的形式被包括在一个头文件,并在每个页面调用。基本上,当用户键入一个单词并单击搜索时,我希望结果显示在一个新页面中。
这是我的html代码的形式

<form action="search.php" method="GET">
    <input type="text" name="query" />
    <input type="submit" value="Search" />
</form>

以下是我数据库连接表单中的内容:

<?php
class DBController {
    private $host = "localhost";
    private $user = "root";
    private $password = "mypass123";
    private $database = "topgrub";
    private $conn;

    function __construct() {
        $this->conn = $this->connectDB();
    }

    function connectDB() {
        $conn = mysqli_connect($this->host,$this->user,$this->password,$this->database);
        return $conn;
    }

    function runQuery($query) {
        $result = mysqli_query($this->conn,$query);
        while($row=mysqli_fetch_assoc($result)) {
            $resultset[] = $row;
        }       
        if(!empty($resultset))
            return $resultset;
    }

    function numRows($query) {
        $result  = mysqli_query($this->conn,$query);
        $rowcount = mysqli_num_rows($result);
        return $rowcount;   
    }

    function updateQuery($query) {
        $result = mysqli_query($this->conn,$query);
        if (!$result) {
            die('Invalid query: ' . mysqli_error($this->conn));
        } else {
            return $result;
        }
    }

    function insertQuery($query) {
        $result = mysqli_query($this->conn,$query);
        if (!$result) {
            die('Invalid query: ' . mysqli_error($this->conn));
        } else {
            return $result;
        }
    }

    function deleteQuery($query) {
        $result = mysqli_query($this->conn,$query);
        if (!$result) {
            die('Invalid query: ' . mysqli_error($this->conn));
        } else {
            return $result;
        }
    }
}
?>

下面是我在search.php中的php代码

<?php
//connect to database, which I have already done
// get value from form
$query = $_GET['query'];
//compare value against sql database  
    mysql_query("SELECT * FROM products
    WHERE (`name` LIKE '%".$query."%') OR (`description` LIKE '%".$query."%')") or die(mysql_error());

//if result found  
 //   loop the below code x amount of times
//else
 //   return message item not found"

if (!empty($product_array)) { 
        foreach($product_array as $key=>$value){
//code to be looped
<div>
            <img src="<?php echo $product_array[$key]["image"]; ?>"   alt="" width=100% height="200" />
            <h4 style="font-size: 1.1vw; text-align:center"><?php echo $product_array[$key]["name"]; ?></h4>
            <p><?php echo  $product_array[$key]["description"]; ?></p>
              <p style="text-align:right"><strong><?php echo "£".$product_array[$key]["price"]; ?></strong>
                  <?php echo "  Serves ".$product_array[$key]["serves"]; ?></p>  
              <form method="post" class="form-item" action="?action=add&code=<?php echo $product_array[$key]["code"]; ?>">
            <label>QTY</label>
            <input type="text" class="product-quantity" name="quantity" value="1"/>
            <button type="submit" value="Add to Cart" id="launch-modal" >Add to Cart</button>
            </form>

 </div>
}
 }
        else{ // if there is no matching rows do following
            echo "No results";
        }

?>

正如我所说,我是一个非常新的人,意识到有些地方缺失/没有实现,但运气不佳。
谢谢你的帮助

2q5ifsrm

2q5ifsrm1#

我认为您没有将查询赋给变量 product_array . $product_array=mysql_query("SELECT * FROM products WHERE ( 名称 LIKE '%".$query."%') OR ( descriptionlike“%$query.”“%”)或die(mysql\u error()); 将查询结果存储在$product_array`

相关问题