Last active
December 6, 2020 18:16
-
-
Save lukxxx/7fbb96eeb19bfd3aaeeca8cd4d59490e to your computer and use it in GitHub Desktop.
PHP PDO not returning anything if value isn't %
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$error = ""; | |
if(isset($_POST['submit'])){ | |
if(empty($_POST['A1'])){ $a1 = ""; } else { $a1 = '(^|,)A1(,|$)'; } | |
if(empty($_POST['A2'])){ $a2 = ""; } else { $a2 = '(^|,)A2(,|$)'; } | |
if(empty($_POST['A'])){ $a = ""; } else { $a = '(^|,)A(,|$)'; } | |
if(empty($_POST['B1'])){ $b1 = ""; } else { $b1 = '(^|,)B1(,|$)'; } | |
if(empty($_POST['B'])){ $b = ""; } else { $b = '(^|,)B(,|$)'; } | |
if(empty($_POST['BE'])){ $be = ""; } else { $be = '(^|,)BE(,|$)'; } | |
if(empty($_POST['C1'])){ $c1 = ""; } else { $c1 = '(^|,)C1(,|$)'; } | |
if(empty($_POST['C1E'])){ $c1e = ""; } else { $c1e = '(^|,)C1E(,|$)'; } | |
if(empty($_POST['C'])){ $c = ""; } else { $c = '(^|,)C(,|$)'; } | |
if(empty($_POST['CE'])){ $ce = ""; } else { $ce = '(^|,)CE(,|$)'; } | |
if(empty($_POST['D1'])){ $d1 = ""; } else { $d1 = '(^|,)D1(,|$)'; } | |
if(empty($_POST['D1E'])){ $d1e = ""; } else { $d1e = '(^|,)D1E(,|$)'; } | |
if(empty($_POST['D'])){ $d = ""; } else { $d = '(^|,)D(,|$)'; } | |
if(empty($_POST['DE'])){ $de = ""; } else { $de = '(^|,)DE(,|$)'; } | |
if(empty($_POST['T'])){ $t = ""; } else { $t = '(^|,)T(,|$)'; } | |
if(empty($_POST['tachograf'])){ $tachograf = "%";} | |
elseif($_POST['tachograf'] === "none") { $tachograf = "%";} | |
elseif($_POST['tachograf'] === "Platná") { $tachograf = "%Platná%";} | |
elseif($_POST['tachograf'] === "Nie"){ $tachograf = "%Nie%";} | |
if(empty($_POST['prehliadka'])){ $prehliadka = "%";} | |
elseif($_POST['prehliadka'] === "none"){ $prehliadka = "%";} | |
elseif($_POST['prehliadka'] === "Platná") { $prehliadka = "%Platná%";} | |
elseif($_POST['prehliadka'] === "Nie") { $prehliadka = "%Nie%";} | |
if(empty($_POST['kv_karta'])) { $kv_karta = "%";} | |
elseif($_POST['kv_karta'] === "none") { $kv_karta = "%";} | |
elseif($_POST['kv_karta'] === "Platná") { $kv_karta = "Platná";} | |
elseif($_POST['kv_karta'] === "Nie") { $kv_karta = "Nie";} | |
if(empty($_POST['psychotesty'])) { $psychotesty = "%";} | |
elseif($_POST['psychotesty'] === "none") { $psychotesty = "%";} | |
elseif($_POST['psychotesty'] === "Platné") { $psychotesty = "Platné";} | |
elseif($_POST['psychotesty'] === "Nie") { $psychotesty = "Nie";} | |
if(empty($_POST['uvazok'])) { $uvazok = "%";} | |
elseif($_POST['uvazok'] === "none") { $uvazok = "%";} | |
elseif($_POST['uvazok'] === "Zivnost") { $uvazok = "Zivnost";} | |
elseif($_POST['uvazok'] === "Skrateny uväzok") { $uvazok = "Skrateny uväzok";} | |
elseif($_POST['uvazok'] === "Trvaly pomer") { $uvazok = "Trvaly pomer";} | |
if(empty($_POST['nocna'])) { $nocna = "%";} | |
elseif($_POST['nocna'] === "none") { $nocna = "%";} | |
elseif($_POST['nocna'] === "Ano") { $nocna = "Áno";} | |
elseif($_POST['nocna'] === "Nie"){$nocna = "Nie";} | |
if(empty($_POST['mimo'])){$mimo = "%";} | |
elseif($_POST['mimo'] === "none"){$mimo = "%";} | |
elseif($_POST['mimo'] === "Ano"){$mimo = "Áno";} | |
elseif($_POST['mimo'] === "Nie"){$mimo = "Nie";} | |
// Define database connection parameters | |
$db_host = "localhost"; | |
$db_name = "vodici"; | |
$db_user = "root"; | |
$db_pass = ""; | |
// Create a connection to the MySQL database using PDO | |
$pdo = new pdo( | |
"mysql:host={$db_host};dbname={$db_name}", | |
$db_user, | |
$db_pass, | |
[ | |
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | |
PDO::ATTR_EMULATE_PREPARES => FALSE | |
] | |
); | |
$sql = " | |
SELECT * FROM users | |
WHERE vodicaky REGEXP ? | |
AND vodicaky REGEXP ? | |
AND vodicaky REGEXP ? | |
AND vodicaky REGEXP ? | |
AND vodicaky REGEXP ? | |
AND vodicaky REGEXP ? | |
AND vodicaky REGEXP ? | |
AND vodicaky REGEXP ? | |
AND vodicaky REGEXP ? | |
AND vodicaky REGEXP ? | |
AND vodicaky REGEXP ? | |
AND vodicaky REGEXP ? | |
AND vodicaky REGEXP ? | |
AND vodicaky REGEXP ? | |
AND vodicaky REGEXP ? | |
AND karta_vodica LIKE ? | |
AND lekarska_prehliadka LIKE ? | |
AND klasifikacna_karta LIKE ? | |
AND psycho_testy LIKE ? | |
AND pomer LIKE ? | |
AND nocna LIKE ? | |
AND cestovanie LIKE ? | |
"; | |
$query = $pdo->prepare($sql); | |
echo $kv_karta, $psychotesty, $uvazok, $nocna, $mimo; | |
// Using `?` as placeholders in the SQL means we can pass an array of parameters | |
// to the `execute` method and they will be applied to the query in the order they | |
// appear. I.e. the first value in the array is mapped to the first `?` in the SQL | |
$query->execute([$a1,$a2,$a,$b1,$b,$be,$c1,$c1e,$c,$ce,$d1,$d1e,$d,$de,$t,$tachograf,$prehliadka, | |
$kv_karta,$psychotesty,$uvazok,$nocna,$mimo]); | |
$post_count = $query->rowCount(); | |
$result = ""; | |
if($post_count > 0){ | |
while($row = $query->fetch(PDO::FETCH_ASSOC)){ | |
$num = "(".$post_count.")"; | |
$div = "<div style='border: 1px solid black; border-radius: 5px; margin-bottom:2%' class='row'>"; | |
$col1 = "<div style='border: 1px 0px 0px 0px solid black; padding: 1%; ' class'col-sm-12 col-md-6 col-lg-6'>"; | |
$vysledok = "<h2 style='text-decoration: underline; padding-left: 2%;'>".$row['meno']." ".$row['priezvisko']."</h2>"; | |
$mail = "<p style='padding-left : 2%; font-size: 11px;'><strong>Rok narodenia:</strong> ".$row['rok_narodenia']." <br><strong>E-mail: </strong> ".$row['email']."</p>"; | |
$mesto = "<p style='padding-left: 2%'><strong>Mesto:</strong> ".$row['mesto']." <strong>Kraj:</strong> ".$row['kraj']."</p>"; | |
$div_end = "</div>"; | |
$result .= $div. $col1. $vysledok. $mail.$mesto. $div_end. $col1. $div_end. $div_end ; | |
} | |
} else { | |
$error = "Vašemu vyhľadávaniu nezodpovedá žiaden vodič"; | |
echo $error; | |
} | |
$db = null; | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<link rel="stylesheet" href="styles/style.css"> | |
<title>Vodici.emtea</title> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> | |
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> | |
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> | |
<script src='https://kit.fontawesome.com/a076d05399.js'></script> | |
</head> | |
<body> | |
<div class="container"> | |
<header class="col-sm-0" style="background-color: #256F5B; margin-bottom: 5%;"> | |
<nav class="navbar navbar-light navbar-1 "> | |
<a style="color: white" class="navbar-brand" href="index.php"><img src="images/logo.png" width="90" height="90" | |
alt="logo-here">Hľadáme prácu vodičom...</a> | |
</nav> | |
</header> | |
</div> | |
<div class="container"> | |
<div class="row"> | |
<div class="col-sm-12 col-md-3 col-lg-2 "> | |
<div class="left-menu"> | |
<div class="menu-left">Registrovaní vodiči</div> | |
<div class="list"> | |
<form class="search-form" action="results.php" method="post"> | |
<button style="border: 0; background: transparent; cursor: pointer;" type="submit" name="bratislava"><p>Bratislavský kraj<strong>(112)</strong></p></a> | |
<button style="border: 0; background: transparent; cursor: pointer;" type="submit" class="side-btn" name="trnava"><p>Trnavský kraj<strong>(90)</strong></p></button> | |
<button style="border: 0; background: transparent; cursor: pointer;" type="submit" class="side-btn" name="trencin"><p>Trenčianský kraj<strong>(72)</strong></p></button> | |
<button style="border: 0; background: transparent; cursor: pointer;" type="submit" class="side-btn" name="nitra"><p>Nitrianský kraj<strong>(11)</strong></p></button> | |
<button style="border: 0; background: transparent; cursor: pointer;" type="submit" class="side-btn" name="zilina"><p>Žilinský kraj<strong>(28)</strong></p></button> | |
<button style="border: 0; background: transparent; cursor: pointer;" type="submit" class="side-btn" name="bystrica"><p>Banskobystrický kraj<strong>(112)</strong></p></button> | |
<button style="border: 0; background: transparent; cursor: pointer;" type="submit" class="side-btn" name="presov"><p>Prešovský kraj<strong>(62)</strong></p></button> | |
<button style="border: 0; background: transparent; cursor: pointer;" type="submit" class="side-btn" name="kosice"><p>Košický kraj<strong>(89)</strong></p></button> | |
</form> | |
<script type="text/javascript"> | |
function hide_r(){ | |
document.getElementById('heading_r').style.display='none'; | |
document.getElementById('r_btn').style.display='none'; | |
} | |
</script> | |
</div> | |
</div> | |
</div> | |
<div style="padding-bottom: 20%" class="col-sm-12 col-md-9 col-lg-10"> | |
<div class="main"> | |
<div class="results"> | |
<h5>Vyhľadajte vodičov podľa vaších predstáv...</h5> | |
<p>Nájdených výsledkov: <strong><?php if($post_count > 0) {echo $num;} else {echo 0;} ?></strong></p> | |
<hr> | |
<p><?php if(isset($_POST['submit']) && $error == "") {echo $result;}?></p> | |
<p><?php if(isset($_POST['submit']) && $error !== "") {echo $error;}?></p> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment