0

首先,如果这是一个愚蠢的问题,我深表歉意 - 我刚刚开始学习我的 php/mysql 技能。我正在制作一个包含 3 个下拉菜单的下拉表单。我希望能够从表单中触发查询。您选择 Part Type、Make、Model hit submit 并显示结果表。

我的表单填充了 3 个数组,当您点击提交时,我可以将每个选定项目的键回显到页面:

    echo '<form action="dbBrowse.php" method="post">';

    $mak = array (0 => 'Make', 'Ford', 'Freightliner', 'Peterbilt', 'Sterling', 'Mack', 'International', 'Kenworth', 'Volvo');
    $mod = array (0 => 'Model', 'Argosy', 'Midliner');
    $p = array (0 => 'Part', 'Radiator', 'Charge Air Cooler', 'AC');                        



    echo '<select name="drop1">';
    foreach ($p as $key => $value) {
    echo "<option value=\"$key\">
    $value</option>\n";

    }


    echo '</select>';




    echo '<select name="drop2">';
    foreach ($mak as $key => $value) {
    echo "<option value=\"$key\">
    $value</option>\n";



    }

    echo '<select/>';



    echo '<select name="drop3">';
    foreach ($mod as $key => $value)  {
    echo "<option value=\"$key\">
    $value</option>\n";



    }

    echo '<select/>';
    echo '</form>';


            //echo keys of selection
    echo $_POST['drop1'];
    echo "<br />";
    echo $_POST['drop2'];
    echo "<br />";
    echo $_POST['drop3'];
            //these echo something like 1, 1, 3 etc. to my page

我迷路的地方是我正在寻找选择的选项并将它们插入到这样的查询中:

     $dropSearch = mysql_query('SELECT * FROM parts WHERE part= "$partTypeVar" . AND  WHERE make = "$makeTypeVar" . AND WHERE model = "$modelTypeVar"');    

            $partTypeVar being the corresponding value to the key that is being returned from the array.        

我正在让自己发疯,试图弄清楚如何做到这一点。最终我想进一步扩展它,但只要能够创建一个带有所选值的 mysql 语句就可以让我现在很开心。我理解需要发生什么的概念,但我不确定如何完成它。任何帮助或朝着正确的方向推动将不胜感激。

4

3 回答 3

0

如果我理解了您的问题,那么在form提交时,您希望query使用所选值进入数据库。

使用示例AND

// Prepare the Query
$query = sprintf(
           "SELECT * FROM parts WHERE part='%s' AND make='%s' AND model='%s'",
           mysql_real_escape_string($_POST['drop1']),
           mysql_real_escape_string($_POST['drop2']),
           mysql_real_escape_string($_POST['drop3'])
         );

// Execute the Query
mysql_query($query);

这将从表部分中选择与这三个值匹配的所有行。

使用示例OR

// Prepare the Query
$query = sprintf(
           "SELECT * FROM parts WHERE part='%s' OR make='%s' OR model='%s'",
           mysql_real_escape_string($_POST['drop1']),
           mysql_real_escape_string($_POST['drop2']),
           mysql_real_escape_string($_POST['drop3'])
         );

// Execute the Query
mysql_query($query);

这将从表部分中选择与这三个值中的任何一个匹配的所有行。

您可以阅读更多相关信息:

mysql_query

mysql_real_escape_string

MySQL 5.6 参考手册:: 12 函数和运算符:: 12.3 运算符

于 2012-06-04T19:58:36.953 回答
0

首先,您必须删除 . char 在您的 SQL 查询中,现在不需要使用它,当然可以为变量分配正确的值。

$partTypeVar = mysql_real_escape_string($_POST['$drop1']);
$makeTypeVar = mysql_real_escape_string($_POST['$drop2']);
$modelTypeVar = mysql_real_escape_string($_POST['$drop3']);

$dropSearch = mysql_query('SELECT * FROM parts WHERE part= "$partTypeVar" AND WHERE make = "$makeTypeVar" AND WHERE model = "$modelTypeVar"');

我假设这是你的变量的正确顺序。

我希望这有帮助!

于 2012-06-04T19:58:53.993 回答
0
<select name="myFilter">
<option value="Chooseafilter" name="default">Choose a filter...</option>
<option value ="Lastname" name="opLastName">Last Name</option>
<option value="Firstname" name="opFirstName">First Name</option>
<select>
<li><!--TEXT SEARCH INPUT--><input type="text" name="search_filter" /></li> 
...
dbconnection();#assume that all connection data is here        
...
$filter = $_POST['myFilter']; #
...

switch($filter)
    {
        case "Lastname":
        $selectedoption = "profile_name";
        break;

        case "Firstname":
        $selectedoption="profile_first_name";
        break;

        case "Chooseafilter":
        $selectedoption = "";
        break;  
    }           

$result = pg_query($db_con, "SELECT * FROM profile WHERE ".$selectedoption." LIKE'%$_POST[search_filter]%'"); 
$row = pg_fetch_assoc($result);  
if (isset($_POST['submit']))
    {   
        while($row = pg_fetch_assoc($result))
        {
        echo"<ul>  
                <form name='update' action='' method='POST'>
                <li>Guest Last Name:</li>
                <li><input type='text' name='profile_name_result' value='$row[profile_name]' /></li>  
                <li>Guest First Name:</li>
                <li><input type='text' name='profile_first_name_result' value='$row[profile_first_name]' /></li>  
                <li><input type='submit' value='Update' name='update'></input></li>
    ...               
于 2013-12-30T18:32:59.717 回答