Page is a not externally linkable
- Code, Content, and Presentation
-- Databases
---- checkbox form, php, and mysql code will kill me for sure


Battleship40 - 12:37 pm on Jul 14, 2011 (gmt 0)


I have been struggling with a plant search form for days...FYI: I am a beginner with PHP and mySQL database management. I am trying to pull data from mySQL database using a form with checkboxes and it is failing on me miserably. I just want whatever someone checks to pull from the database and display on an html page.

Here's the form:

<form name="form2" method="post" action="mSearch2.php">
<input type="hidden" name="check_submit" value="1" />
<h3 align="center">Light</h3>
<div align="center">
<input type="checkbox" name="light[]" value="Full Sun" checked="checked" /> Full Sun

<input type="checkbox" name="light[]" value="Part Sun Shade" /> Part Sun Shade
<input type="submit" name="Submit" value="GO" style="font-family: Arial; font-size: 13pt; color: #000000" />
</div>
</form>


Here's the first mSearch2.php file:


<?php
$host = "";
$user = "";
$pass = "";
$dbname = "";

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);

if (isset($_POST['light'])){$query = "SELECT * FROM findplantsdb WHERE Light IN (".implode(",", array_keys($_POST['light'])).")";

$result = mysql_query($query) or die (mysql_error());
$num_of_rows = mysql_num_rows($result);

if ($num_of_rows > 0)
{
// put while loop and all output here
}
else
{
echo "No results found";
}
while($row = mysql_fetch_assoc($result))
{
echo "<a href='categories.php?pullname=". $row['Name']. "'>".stripslashes(htmlspecialchars($row['Name'])).'</a><br />';

}
}
?>


And finally, here is the categories.php file to turn what I pull into links:


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

$query= "select * from findplantsdb where Name='" . $_GET['pullname'] . "'";
$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=",$row['Picname'], " /> <p><p>", $row['Notes'], "<p><p> <hr width='50%' size='1' color='#A3A3A3'><p>";
}

?>


Right now - what this is doing is pulling all plant material. For this form I just need it to pull whatever the person checks. Any assistance would be greatly appreciated and might just save me from throwing myself in front of a bus.


Thread source:: http://www.webmasterworld.com/databases_sql_mysql/4339453.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com