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

时间:2019-01-06 08:30:34

标签: php mysql sql

我是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";
        }

?>
就像我说的那样,我很新,意识到有些地方缺少/没有实现,但是没有运气。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我认为您没有将查询分配给变量product_array

$product_array=mysql_query("SELECT * FROM products WHERE (名称LIKE '%".$query."%') OR (描述`喜欢'%“。$ query。”%')“)或die(mysql_error());

将查询结果存储在$product_array