samedi 4 septembre 2021

How to search multiple inputs and checkboxes with php pdo?

I have a search form with multiple "text" inputs currently functioning which I am trying to add checkboxes to.. After adding the checkboxes, I'm getting the following:

ERROR: Array to String Conversion

<form action='s-response.php method='get'>
<div class='cusname'><input type='text' name='cusname' class='EdtTxt' /></div>
<div class='street'><input type='text' name='street' class='EdtTxt' /></div>

Checkboxes

<?php while ($row=$query->fetch()){?>
    <input type='checkbox' name='groupid[]' value='<?php echo $row['id'];?>'><?php echo $row['title'];?><?php }?>

RESPONSE PAGE

$wheres = array();
$params = array();
/* Find all Sent fields, If the field is Not Empty, add the field to the SQL query */
if (!empty($_GET['cosname'])) { $wheres[] = 'c.cname LIKE :cosname'; $params[':cosname'] = '%'.$_GET['cosname'].'%'; }
if (!empty($_GET['street'])) { $wheres[] = 'a.street LIKE :street'; $params[':street'] = '%'.$_GET['street'].'%'; }

Checkbox Part

if(!empty($_GET['groupid'])) {

foreach($_GET['groupid'] as $value){
    $wheres[] = 'g.groupid = :groupid'; $params[':groupid'] = $_GET['groupid'];
}}

Query Part

/* Create the Start and End (static) parts of the query */
$q1="SELECT c.fname,a.street FROM customers c LEFT JOIN cus_address a ON c.id=a.custid WHERE c.act=1";
$q3=" GROUP BY c.id";
/* Create the Middle (dynamic) part of the query */
if (!empty($wheres)) { $q1 .= " AND " . implode(' AND ', $wheres); }
/* Combine into one query and execute */
$sql=$q1.$q3;
$query=$con->prepare($sql);
$query->execute($params);

Any idea what is causing the error?




Aucun commentaire:

Enregistrer un commentaire