Checkboxes, SQL select statements & PHP
I am trying to display some rows from a database table based on choices submitted by the user. Here is my form code
<form action="choice.php" method="POST" >
<input type="checkbox" name="variable[]" value="Apple">Apple
<input type="checkbox" name="variable[]" value="Banana">Banana
<input type="checkbox" name=开发者_运维问答"variable[]" value="Orange">Orange
<input type="checkbox" name="variable[]" value="Melon">Melon
<input type="checkbox" name="variable[]" value="Blackberry">Blackberry
From what I understand I am placing the values of these into an array called variable. Two of my columns are called receipe name and ingredients(each field under ingredients can store a number of fruits). What I would like to do is, if a number of checkboxes are selected then the receipe name/s is displayed.
Here is my php code.
<?php
// Make a MySQL Connection
mysql_connect("localhost", "*****", "*****") or die(mysql_error());
mysql_select_db("****") or die(mysql_error());
$variable=$_POST['variable'];
foreach ($variable as $variablename)
{
echo "$variablename is checked";
}
$query = "SELECT receipename FROM fruit WHERE $variable like ingredients";
$row = mysql_fetch_assoc($result);
foreach ($_POST['variabble'] as $ingredients)
echo $row[$ingredients] . '<br/>';
?>
I am very new to php and just wish to display the data, I do not need to perform any actions on it. I have tried many select statements but I cannot get any results to display. My db connection is fine and it does print out what variables are checked.
Many thanks in advance.
EDIT:
Thanks a million for replying. However, I tried correcting my own code=blank page and both solutions above ==blank pages also. Grrrr!! Here is one solution I tried.
<?php
// Make a MySQL Connection
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());
$query = "SELECT receipename FROM fruit ";
$cond = "";
foreach($variable as $varname)$cond .= " $varname like 'ingredients' OR";
$cond = substr_replace($cond, '', -2);
$query .= " WHERE $cond";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
echo $row['receipename'],'<br />';
}
I also tried
<?php
// Make a MySQL Connection
mysql_connect("localhost", "24056", "project99") or die(mysql_error());
mysql_select_db("24056db2") or die(mysql_error());
$variable=$_POST['variable'];
foreach ($variable as $variablename)
{
$query = "SELECT receipename FROM horse WHERE ingredients = '".$variablename."'";
while($row = mysql_fetch_assoc($query))
{
echo $row['receipename']."<br/>";
}
}
?>
I suppose another way to say it, if the checkbox variable is equal to a record under the ingredients column, I wish to print out the receipename of that record. Im nearly getting confused here mysellf, haha.
Any other ideas I could try???
Correction in ur code
$query = "SELECT receipename FROM fruit WHERE $variable like ingredients";
$row = mysql_fetch_assoc($result);
Do u see the difference above?
Place "$query" in place of "$result" mysql_fetch_assoc($result)
My Solution
$variable=$_POST['variable'];
foreach ($variable as $variablename)
{
$query = "SELECT receipename FROM fruit WHERE ingredients = '".$variablename."'";
while($row = mysql_fetch_assoc($query))
{
echo $row['receipename']."<br/>";
}
}
Your question is not clear to me, you may try the following-
$query = "SELECT receipename FROM fruit ";
$cond = "";
foreach($variable as $varname)$cond .= " $varname like 'ingredients' OR";
$cond = substr_replace($cond, '', -2);
$query .= " WHERE $cond";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
echo $row['receipename'],'<br />';
}
NOTE: This code is not tested
精彩评论