Ok, I feel like I'm so close, but could use some help. I am trying to retrieve data from mySQL database via use of a form with checkboxes. This form is working somewhat except that it is only pulling one selection, not all that are selected. Can someone look at this and let me know how to fix this? Would greatly appreciate it!!

<html>
<head>
<title>Checkbox retrieval test</title>
</head>
<body>

<form method='post'>
<input type=checkbox name=fc[] value="red" class=2> red<br> 
<input type=checkbox name=fc[] value="yellow" class=2> yellow<br> 
<input type=checkbox name=fc[] value="orange" class=2> orange<br> 
<input type=checkbox name=fc[] value="burgundy" class=2> burgundy<br> 
<input type=submit name=submit> 
</form> 

<? 

if(isset($_POST['submit'])) 
{ 
  foreach($_POST['fc'] As $fc) 
    printf("%s<br>", $fc); 
} 

$fcList = "";

foreach($_POST['fc'] As $fc) 
$fcList .= $fc . " ";

 
// Make a MySQL Connection
	mysql_connect("", "", "") or die(mysql_error());
	mysql_select_db("") or die(mysql_error());


$query = "SELECT * FROM findplantsdb WHERE Color='$fc'";


$result= mysql_query($query); 
$num_results = mysql_num_rows($result); 

for ($i=0; $i <$num_results; $i++) 
{ 
$row = mysql_fetch_array($result); 


echo "<h4> ", $row['Name'], " &nbsp; ", $row['Patent'], "</h4> ",$row['Common'], "<p> Height:  ", $row['Hname'], "<br> Spread:  ", $row['Sname'], "<br> Color:  ", $row['Color'], "<br> Light:  ", $row['Light'], "<br> Zone:  ", $row['Zone'], "<p> <img src=http://www.domain.com/mobile/",$row['Picname'],  " /> <p><p>", $row['Notes'], "<p><p> <hr width='50%' size='1' color='#A3A3A3'> <p>";
} 

  
?> 
 
</body>
</html>

Dani AI

Generated

had the right idea with checkboxes named fc[] (that produces an array on submit). was also right to suggest while() for fetching rows. The actual bug is that the SQL uses a single variable that ends up holding only the last checked color, so the query returns rows for just one color instead of all selected colors.

A robust fix is to build a single query that matches all chosen values (an SQL IN list) and use prepared statements to avoid injection. Example with PDO (replace connection details):

if (!empty($_POST['fc'])) {
    $colors = array_values($_POST['fc']);          // ensure numeric keys
    $placeholders = implode(',', array_fill(0, count($colors), '?'));
    $sql = "SELECT * FROM findplantsdb WHERE Color IN ($placeholders)";

    $pdo = new PDO('mysql:host=HOST;dbname=DB', 'USER', 'PASS', [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ]);
    $stmt = $pdo->prepare($sql);
    $stmt->execute($colors);

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        // escape before output
        echo htmlspecialchars($row['Name'], ENT_QUOTES);
        // more output...
    }
}

If the Color column actually stores comma-separated values, use a normalized many-to-many table (recommended) or, as a stopgap, FIND_IN_SET() or LIKE '%color%' (less reliable and slower). Also migrate away from mysql_* functions — they were removed in PHP 7 — and always escape HTML output with htmlspecialchars().

Quick checklist: confirm name="fc[]" and isset($_POST['fc']) before using; prefer a single IN query instead of running one query per selected color; validate inputs; and normalize the schema if plants can have multiple colors.

Member Avatar for Member #120589

just a quick look try changing this:

for ($i=0; $i <$num_results; $i++) 
{ 
$row = mysql_fetch_array($result); 
 
 echo "<h4> ", $row['Name'], " &nbsp; ", $row['Patent'], "</h4> ",$row['Common'], "<p> Height:  ", $row['Hname'], "<br> Spread:  ", $row['Sname'], "<br> Color:  ", $row['Color'], "<br> Light:  ", $row['Light'], "<br> Zone:  ", $row['Zone'], "<p> <img src=http://www.domain.com/mobile/",$row['Picname'],  " /> <p><p>", $row['Notes'], "<p><p> <hr width='50%' size='1' color='#A3A3A3'> <p>";

}

to:

while($row = mysql_fetch_array($result)) 
{ 
echo "<h4> ". $row['Name']. " &nbsp; ". $row['Patent']. "</h4> ".$row['Common']. "<p> Height:  ". $row['Hname']. "<br> Spread:  ". $row['Sname']. "<br> Color:  ". $row['Color']. "<br> Light:  ". $row['Light']. "<br> Zone:  ". $row['Zone']. "<p> <img src=http://www.domain.com/mobile/".$row['Picname'].  " /> <p><p>". $row['Notes']. "<p><p> <hr width='50%' size='1' color='#A3A3A3'> <p>";

}

All I've done is change the , for a .
However, the html markup in the loop is a little odd. Don't usually place <p><p> together. Also <br /> instead of <br> these days - although it'll still work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.