Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何制作用户有三列要搜索的搜索表单。使用PHP、SQL和HTML_Php_Html_Mysql - Fatal编程技术网

如何制作用户有三列要搜索的搜索表单。使用PHP、SQL和HTML

如何制作用户有三列要搜索的搜索表单。使用PHP、SQL和HTML,php,html,mysql,Php,Html,Mysql,我想知道如何制作一个用户有3个选项可供搜索的搜索表单 按年龄搜索(下拉列表18-25和26-40) 按性别(男性或女性)搜索 按姓名搜索 在我的代码中,当我单击带有空白字段的“提交”时,它会抛出所有我不需要的数据: <?php $output = NULL; if (isset ( $_POST ['submit'] )) { // Connect to database $mysqli = new Mysqli ( "localhost", "root", "2222"

我想知道如何制作一个用户有3个选项可供搜索的搜索表单

  • 按年龄搜索(下拉列表18-25和26-40)
  • 按性别(男性或女性)搜索
  • 按姓名搜索
  • 在我的代码中,当我单击带有空白字段的“提交”时,它会抛出所有我不需要的数据:

    <?php
    $output = NULL;
    
    if (isset ( $_POST ['submit'] )) {
        // Connect to database
        $mysqli = new Mysqli ( "localhost", "root", "2222", "matrimonialPortal" );
    
        $search = $mysqli->real_escape_string ( $_POST ['search'] );
    
        // Query the databse
        $resultSet = $mysqli->query ( "SELECT * FROM mp_user WHERE name LIKE '%$search%' OR email LIKE '%$search%' OR salutation LIKE '%$search%' OR id LIKE '%$search%'" );
    
        if ($resultSet->num_rows > 0) {
            while ( $rows = $resultSet->fetch_assoc () ) {
                $name = $rows ['name'];
                $email = $rows ['email'];
    
    
                $output .= "::<strong>The Details of your search</strong> ::<br /> Name: $name<br /> Email:$email<br /><br /> ";
            }
        } else {
            $output = "Oops No results Found!!";
        }
    }
    ?>
    
    <!-- The HTML PART -->
    <form method="POST">
        <div>
            <p>
                Search By name: <input type="TEXT" name="search" /> <input
                    type="SUBMIT" name="submit" value="Search >>" />
            </p>
        </div>
    
        <div>Search By Age :
            <select name="age">
                <option></option>
                <option value="18-20">18-20</option>
                <option value="20-25">20-25</option>
            </select><input type="SUBMIT" name="submit" value="Search >>" />
        </div>
        <br />
        <div>
            Search By Gender: 
            <select name="salutation">
                <option></option>
                <option value="0">--- Male ---</option>
                <option value="1">--- Female ---</option>
            </select> <input type="SUBMIT" name="submit" value="Search >>" />
        </div>
        <br> <br>
    </form>
    <?php echo $output; ?>
    

    您可以替换代码

    if ($resultSet->num_rows > 0) {
    
    用这个

    if ($resultSet->num_rows > 0 and trim($search) != "") {
    
    因此,如果输入框为空,则不会显示所有结果 希望这对你有帮助

    编辑

    这里有一个例子,你可以得到的想法

    $qry = "SELECT * FROM test WHERE 1=1";
    
    if($purpose!="")
      $qry .= " AND purpose='$purpose'";
    
    if($location!="")
      $qry .= " AND location='$location'";
    
    if($type!="")
      $qry .= " AND type='$type'";
    
    and for age
    
    if ($age!='') {
      $qry .= " AND age between ".str_replace('-',' and ',$age);
    }
    

    我不能给你全部的代码,但这里有一些解决方案

  • 使用3个不同的表单和3个不同的提交按钮

  • 使用html表单上的单选按钮,在PHP端进行检查,并根据选择的单选项执行操作

  • 使用按钮代替提交、单选按钮、隐藏字段,并在表单提交中将数据传递到不同的php页面(这可能会很长)


  • 您可以选择。

    当您发布一个空白变量并使用%$search%和“或”其他条件进行查询时,sql会在列名中匹配所有带空格的记录!所以你必须使用一些变量

    If(empty($POST['search']){ ['Query witbout Name parameter']} else{['Query with Name parameter']} 
    
    至于转换出生日期以匹配年龄范围。你必须使用

    选择时间差

    回答这里


    看来您对PHP还是新手。这里有一个解决方案

    第一个HTML部分。这里使用“action”,这意味着页面将定位文件和流程数据。例如action=“search\u process.php”。但是,如果您正在处理来自同一页面的数据,请使用$_SERVER['PHP_SELF']

           <!-- The HTML PART -->
            <form method="POST" action="$_SERVER['PHP_SELF']"> // here it will load the self page
                <div>
                    <p>
        Search By name: <input type="text" name="search_name" />
        Search By age: <input type="text" name="search_age" />
        Search By gender: <input type="TEXT" name="search_gender" /> 
    <input type="submit" name="submit_name" value="Search >>" />
                    </p>
                </div>
    
    
    //在这里,它将加载self页面
    
    按姓名搜索:
    按年龄搜索:
    按性别搜索:
    

    现在是PHP部分:

        <?php 
    
               if(isset($_POST['submit_name'])
               {
                 //What happens after you submit? We will now take all the values you submit in variables
    
                  $name = (!empty($_POST['search_name']))?mysql_real_escape_string($_POST['search_name']):null; //NOTE: DO NOT JUST USE $name = $_POST['search_name'] as it will give undefined index error (though your data will be processed) and will also be open to SQL injections. To avoid SQL injections user mysql_real_escape_string.
                  $age = (!empty($_POST['search_age']))?mysql_real_escape_string($_POST['search_age']):null;
                  $gender = (!empty($_POST['search_gender']))?mysql_real_escape_string($_POST['search_gender']):null;
    
               //Now we will match these values with the data in the database
    
            $abc = "SELECT * FROM table_name WHERE field_name LIKE '".$name."' or field_gender LIKE '".$gender."' or field_age LIKE '".$age."'"; // USE "or" IF YOU WANT TO GET SEARCH RESULT IF ANY OF THE THREE FIELD MATCHES. IF YOU WANT TO GET SEARCH RESULT ONLY WHEN ALL THE FIELD MATCHES THEN REPLACE "or" with "and"
            $def = mysql_query($abc) or die(mysql_error())// always use "or die(mysql_error())". This will return any error that your script may encounter
    
       //NOW THAT WE HAVE GOT THE VALUES AND SEARCHED THEM WE WILL NOW SHOW THE RESULT IN A TABLE
            ?>    
            <table cellspacing="0" cellpadding="0" border"0">
              <tr>
                <th>Name</th>
                <th>Age</th>
                <th>Gender</th>
              </tr>
            <?php while($row = mysql_fetch_array($def)) {  // I HAD MISSED OUT A WHILE LOOP HERE. SO I AM EDITING IT HERE. YOU NEED TO USE A WHILE LOOP TO DISPLAY THE DATA THAT YOU GOT AFTER SEARCHING.
              <tr>
                <td><?php echo $row[field_name]; ?></td>
                <td><?php echo $row[field_age]; ?></td>
                <td><?php echo $row[field_gender]; ?></td>
              </tr>
            <?php } ?>
            </table>
        <?php } ?>
    

    您所说的“抛出所有数据”是什么意思?请澄清您的问题。当您提交空白字段时,脚本将返回所有记录,因为您的MySQL查询-所有变量基本上都是空白,因此不会过滤任何内容。您可以为查询中返回的行添加限制,也可以添加执行不同查询或根本不执行查询的条件。此解决方案在mysql中,但如果需要,可以将其转换为mysqli。此外,我已将结果显示在表格中,但您可以按任何方式显示它。,你猜对了,我对php很陌生,我想感谢你的代码,它帮助我写了很多好代码,也学到了一些东西。如果你能帮我添加代码,用存储用户出生日期的“dob”列来计算年龄,我将不胜感激。我尝试了几段代码,但都做不到。我想要的一切就是计算年龄并存储它,当用户尝试搜索时,例如(年龄在18到25岁之间的男性)。然后它应该显示相应的注册用户。嘿,首先很高兴听到你学到了一些东西。现在,我已经编辑了答案中的表格部分,因为我遗漏了一个必要的WHILE循环。我也可以帮你计算年龄,但以后。。我现在很忙。。另外,如果你想执行搜索时选择而不是任何时候,这也会很好。另外,请接受答案,如果有帮助,请加1:)