Select does not work when clicking button

Asked

Viewed 206 times

0

I’m looking for a contract at the bank, but when I click on 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';
   }
?>
  • I could not understand much the code, but I’ve seen a small error, usually this is done in separate files, one with the search form and the other with the mechanics. The error must be because Voce has the Submit button but has no form for this button, in case it should have a form with action="#"

  • I think you now agree with what you told me, but still it was not, thank you for the attention.

2 answers

2

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

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

Class:

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; } } ?>

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>

Consultation (after the 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';
}
?>
  • But the fetchAll() does not consume more memory than while($query->fetch())?

  • It consumes a little more, but the cost of doing fetch for all results ends up being the same or even greater than doing the fetchAll(), For deep down it would be the same thing.

-1

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

$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 were having problems with Ubmit, but in the last issue of your question it seems that the code of the form and the button has been removed, already managed to solve?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.