我有一个带有多个复选框的页面,每个框对应于每个成分(图1)
数据库被称为“食谱”
(图1)
表名='成分'
依旧......
这张桌子是主要的,随着网站的增长,配料和复选框的数量也会匹配。
我还有几张桌子,每张桌子都是食谱(图2)
(图2)
表名='烤宽面条'
ingredient_id
依旧......
对于每个食谱,ingredient_id匹配主表中的一个ID(图1)。
我想拥有它,以便有人可以根据需要选择多个复选框。当他们点击提交时,它会返回每个配方,从最接近匹配的成分开始,然后是顶部的az .........我知道当你输入echo时它打印出你在屏幕上说的内容。 ..如果我在search.php页面上有一个表,那么如上所述,每个配方结果是否可以按顺序回显到表中的每一行?
这就是我到目前为止所做的......
的index.html ..
<form action="search.php" method="post" name="search_form" onsubmit="return checked boxes" >
<table width="571" border="0">
<tr>
<td width="183" valign="top"><input type="checkbox" name="search[]" value="Olive oil" id="1"/> Olive oil<br/>
<input type="checkbox" name="search[]" value="black pepper" id="2"/> Black pepper<br/>
<input type="checkbox" name="search[]" value="beef stock" id="3"/> Beef stock<br/>
<input type="checkbox" name="search[]" value="lean steak mince" id="4"/> Lean steak mince<br/>
<input type="checkbox" name="search[]" value="mushrooms" id="5"/> Mushrooms<br/>
<input type="checkbox" name="search[]" value="red onion" id="6"/> Red onion<br/>
<input type="checkbox" name="search[]" value="garlic" id="7"/> Garlic<br/>
<input type="checkbox" name="search[]" value="tomato concentrate" id="8"/> Tomato concentrate<br/>
<input type="checkbox" name="search[]" value="lasagna sheets" id="9"/> Lasagna sheets<br/>
<input type="checkbox" name="search[]" value="milk" id="10"/> Milk<br/></td>
<td width="171" valign="top"><input type="checkbox" name="search[]" value="cheese" id="11"/> Cheese<br/>
<input type="checkbox" name="search[]" value="butter" id="12"/> Butter<br/>
<input type="checkbox" name="search[]" value="plain flour" id="13"/> Plain flour<br/>
<input type="checkbox" name="search[]" value="chopped tomatos" id="14"/> Chopped tomato<br/></td>
<td width="195"> </td>
</tr>
</table>
<input type="submit" value="search"/>
</form>'
的search.php
$username = "root";
$password = "";
$hostname = "localhost";
$dbhandle = mysql_connect("localhost", "root","" )
or die("Unable to connect to MySQL");
$selected = mysql_select_db("recipes",$dbhandle)
or die("Could not select recipes");'
答案 0 :(得分:0)
除非您永远不会在系统中添加或删除食谱,否则您的整体设计存在缺陷。大多数人会使用的模式更像是:
根据您的设计,您需要为每个食谱创建一个新表。此外,要查找使用成分的配方,您需要在单独的查询中搜索几十个表(或者使用大量UNION查询一个非常混乱的查询)。
使用此设计,您只需搜索RECIPE_INGREDIENTS并加入RECIPES即可打印出配方名称。
无论哪种方式,您都希望通过依次检查每个成分选项并将其附加到子句中来在查询中构建WHERE子句。在伪代码中:
$whereClause="WHERE ingredient in ("
for each ingredient {
if(user selected $ingredient) {
$whereClause .= "'" . $ingredient . "',"
}
}
$whereClause .= ")"
答案 1 :(得分:-1)
也许你可以在MySQL中使用LIKE命令:
SELECT * FROM recipes WHERE agent LIKE something