rmdp
rmdp

Reputation: 23

Search on mysql php

I'm have been working on a website for a class context and I'm doing a search field that goes to mysql database table and return a matched result.

I make this code but its not working and the error is like "Array to string conversion" I have searched for hours and cant find a solution. Here is my code:

<?php
if(!empty($_POST) ){

    $searchstring['string'] = $_POST['string'];
    $productManager = new ProductManagement();

    $results = $productManager->searchProduct($searchstring);

    if (!$results) {
        echo "nothing found";
    } else {
        while($row = $results->fetch_array(MYSQLI_ASSOC)) {

            echo '


            <div class="col-sm-4 col-lg-4 col-md-4">
                <div class="thumbnail">
                    <img src="'. $row ['image'] .'" alt="" width="320px" weight="150px">
                    <div class="caption">
                        <h4 class="pull-right">€'. $row ['price'] .'</h4>
                        <h4><a href="#">'. $row ['name'] .'</a>
                        </h4>
                        <p><strong>Size:</strong> '. $row ['size'] .'</p>
                        <p class="pull-right"><a class="btn btn-info" href="view.php?id='. $row ['id_shirt'] .'" role="button">Ver</a></p>
                    </div>
                </div>
            </div>

            ';

        }
    }
}

and:

public function searchProduct($searchstring)
{
    $results = $this->_database->performQuery("SELECT * FROM `shirts` WHERE name LIKE '%".$searchstring."%'");
    return($results);

}

I hope you can help me thanks!

Upvotes: 2

Views: 49

Answers (2)

Digital Chris
Digital Chris

Reputation: 6202

As mentioned in my comment, you can fix this one of two ways.

Either make $searchstring a string like:

$searchstring = $_POST['string'];

or change your sql to use the array value you chose:

$results = $this->_database->performQuery("SELECT * FROM shirts WHERE name LIKE '%".$searchstring['string']."%'");

Upvotes: 0

Hossein
Hossein

Reputation: 1391

Simply use this:

$searchstring = $_POST['string'];

insted of this:

$searchstring['string'] = $_POST['string'];

Upvotes: 3

Related Questions