使用PHP进行MySQL搜索

使用PHP进行MySQL搜索

问题描述:

I'm writing a code for MySQL search but it is not working

im using PHP class and this is the code:

data_model.php

<?php
$data = new Data();
if(isset($_GET['q'])) {
    $data->$_GET['q']();
}
class Data {

    function __construct(){
        if(!isset($_SESSION['id'])){
            header('location:../../');   
        }
    }


    function getSubject($search){
        global $con;
        $q = "select * from subject where code like '%$search%' or title like '%$search%' order by code asc";
        $r = mysqli_query($con, $q);
        return $r;
    }]

subject.php

<?php
include "data/data_model.php";

$search = isset($_POST["search"]); 
$subject = $data->getSubject($search);
?>

<form action="subject.php" method="post">
   <label>
       <input type="text" class="form-control" name="search" placeholder="Search Subject..." >
   </label>
   <button type="submit" name="search" class="btn btn-success" value="search"><i class="fa fa-search"></i> Search</button>
</form>

is there anything wrong in my code? I also use the getSubject in getting data from the db table to my html table and it has no problems but in searching it is not working?

Can anyone help me? Thanks in advance.

Shouldn't it be like this?

$data = new Data();
if(isset($_GET['q'])) {
    $data->getSubject($_GET['q']);
}

Also, this:

<?php
include "data/data_model.php";

$search = isset($_POST["search"]); 
$subject = $data->getSubject($search);
?>

Won't work as expected, because isset() returns TRUE/FALSE depending if there's "search" in $_POST. This is what works:

    <?php
        include "data/data_model.php";


        $search = isset($_POST["search"]) ? $_POST["search"]:  '';
        $subject = $data->getSubject($search);
        ?>

I think there problem in your connection object So it not running properly if you want to do operations using class then here is my suggested code for you.

Create your connection class first

class DBConnection {

        private $dbConnection = false;

        function __construct() {

        }

        public function connect() {
            $this->dbConnection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
            if (mysqli_connect_error()) {
                die(error_db_not_conntected);
            }
            return $this->dbConnection;
        }

        public function disconnect() {
            $this->connect()->close();
        }
    }

Extends that class in Data Class Like this

you have to include the DBConnection file here

include 'DBConnection.php';

 class Data extends DBConnection{
       function __construct(){
            if(!isset($_SESSION['id'])){
                header('location:../../');   
            }
        }

        function getSubject($search){
            $q = "select * from subject where code like '%$search%' or title like '%$search%' order by code asc";
            $results = $this->connect()->query($q);
            $this->disconnect();
            return $results;
        }     
    }

Use this Class like this, if you are using POST Method in your subject.php

$search = array();  
     if(isset($_POST['search'])){
        $search = (new Data())->getSubject($_POST['search']);
    }    
    print_r($search);