Hello
I have a search page with two text field and one radio button group (three buttons). You must fill in at least one value - none of the radio buttons is checked as default.
The form fields are named: productname, productgroup (radio btn) and keyword.
On the result page I have this code calling a method in class Inc_images:
My class method looks like this:PHP Code:$productname = $_POST['productname'];
if (!isset($_POST['productgroup'])) {
$productgroup = "";
} else {
$productgroup = $_POST['productgroup'];
}
$keyword = $_POST['keyword'];
$result = $image->findImage($productname, $productgroup, $keyword);
if (count($result) <= 0) {
echo 'Finns inga bilder som matchar din sökning';
} else {
foreach ($result as $item) {
echo $item->pNamn;
echo '<br>';
echo $item->pGrupp;
echo '<br>';
echo $item->keywords;
echo '<br>';
echo '<br>';
}
}
For some reason the radio buttons are not ignored if none of them are selected. To have the query to return any rows I must select one of the radio buttons – which I don't want to be necessary.PHP Code:public function findImage($productname, $productgroup, $keyword)
{
try {
$productname = "%$productname%";
$productgroup = $productgroup;
$keyword = "%$keyword%";
$sqlwhere = "1 = 1 ";
if (!empty($productname)) {
$sqlwhere .= " AND pNamn LIKE :productname";
}
if (isset($productgroup) || (!empty($productgroup))) {
$sqlwhere .= " AND pGrupp = :productgroup";
}
if (!empty($keyword)) {
$sqlwhere .= " AND keywords LIKE :keyword";
}
$sql = "SELECT * FROM images WHERE " . $sqlwhere;
$stmt = $this->pdoLink->pdoConnectLink->prepare($sql);
$stmt->bindParam(':productname', $productname, PDO::PARAM_STR);
$stmt->bindParam(':productgroup', $productgroup, PDO::PARAM_STR);
$stmt->bindParam(':keyword', $keyword, PDO::PARAM_STR);
$stmt->execute();
$result = array();
while ($row = $stmt->fetchObject()) {
$result[] = $row;
}
return $result;
}
catch (PDOException $e)
{
echo 'Ett fel inträffade: ' . $e->getMessage();
}
}
Any tips on how to fix this?
Regards, Magnus








Bookmarks