Select does not work when clicking button

0

I'm looking for a contract at the bank, but clicking submit nothing is returned:

Follow the code below:

class Config {

// specify your own database credentials
private $host = "localhost";
private $db_name = "biodata";
private $username = "root";
private $password = "";
public $conn;

// get the database connection
public function getConnection(){

    $this->conn = null;

    try{
        $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
    }catch(PDOException $exception){
        echo "Connection error: " . $exception->getMessage();
    }

    return $this->conn;
}

} ? >

                                                                                            
<?php 
// Search from MySQL database table
$search= isset($_POST['search']) ? $_POST['search'] : '';
$query = $db->prepare("select from crudpdo2 where nt_pdo LIKE '%$search%' ORDER BY id_pdo");
$query->bindValue(1, "%$search%", PDO::PARAM_STR);
$query->execute();
// Display search result

if (!$query->rowCount() == 0) {

echo "Search found :<br/>";
echo "<table style=\"font-family:arial;color:#333333;\">";  
echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">Title Books</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">Author</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">Price</td></tr>";             
while ($results = $query->fetch()) {
echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">";            
echo $results['id_pdo'];
echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">";
echo $results['nt_pdo'];
echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">";
echo $results['nc_pdo'];
echo "</td></tr>";              
        }
    echo "</table>";        
    } else {
    echo 'Nothing found';
   }
?>
    
asked by anonymous 14.01.2016 / 14:17

2 answers

2

I looked deeply into the code and was full of error and bad practices:

  • Starting in the form, where name="search" was in submit when it should be in input with type text .
  • Another error was when putting '%$search%' in sql, when you have to have bind and the variable of the value containing the expression of LIKE (%%).
  • Use fetch() within while when you can use fetchAll() to get all results at once.
  • Bad practice of writing HTML within echo .
  • Put the entire style inside the HTML tag. The page code looks like this:

Class:

dbname = $ db_name, $ this-> username, $ this-> password);             } catch (PDOException $ exception) {                 echo "Connection error:". $ exception-> getMessage ();             }             return $ this-> conn;         }     }     ? >

Form:

<form method="post" action="#">
    <div class="row">
        <div class="col-sm-4 col-sm-offset-7">
            <div class="input-group">
                <span class="input-group-btn">
                    <button class="btn btn-default" type="submit" ><i class="glyphicon glyphicon-search"></i></button>
                </span>
                <input type="text" name="search" class="form-control" placeholder="Pesquisar...">
            </div>
        </div>
    </div>
</form>

Query (after form):

<?php
// Search from MySQL database table
$cfg = new Config();
$db = $cfg->getConnection();
$search = isset($_POST['search']) ? $_POST['search'] : '';
$search = "%" . $search . "%";
$query = $db->prepare("SELECT * FROM 'crudpdo2' WHERE 'nt_pdo' LIKE :search ORDER BY id_pdo ASC");
$query->bindParam(":search", $search, PDO::PARAM_STR);
$query->execute();
$row = $query->fetchAll(PDO::FETCH_ASSOC);
// Display search result
if (!empty($row)) {
    ?>

    <p>Search found :</p>
    <table style="font-family:arial;color:#333333;">
        <tr>
            <td style=border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;>
                Title Books
            </td>
            <td style="border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;">
                Author
            </td>
            <td style="border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;">
                Price
            </td>
        </tr>

        <?php for ($i = 0; $i < count($row); $i ++) { ?>
            <tr>
                <td style="border-style:solid;border-width:1px;border-color:#98bf21;">
                    <?php echo $row[$i]['id_pdo']; ?>
                </td>
                <td style="border-style:solid;border-width:1px;border-color:#98bf21;">
                    <?php echo $row[$i]['nt_pdo']; ?>
                </td>
                <td style="border-style:solid;border-width:1px;border-color:#98bf21;">
                    <?php echo $row[$i]['nr_pdo']; ?>
                </td>
            </tr>
        <?php } ?>
    </table>
    <?php
} else {
    echo 'Nothing found';
}
?>
    
14.01.2016 / 16:11
-1

You are making the wrong select. To use Like do this:

$query = $db->prepare("select from crudpdo2 where nt_pdo LIKE ':search' ORDER BY id_pdo");
$query->bindValue(':search', "%".$search."%", PDO::PARAM_STR);
$query->execute();

You commented that you had problems submitting, but in the last issue of your question it looks like the form and button code has been removed, have you solved it yet?

    
14.01.2016 / 16:08