-3

我想在我的网站中创建一个搜索部分,用户可以在其中选择 3 种搜索类型:他可以按名称搜索、按专业搜索或按位置搜索。

输出将是名字、姓氏和个人资料图片。

问题是我不知道如何编写此代码的结构和查询。

这是我试图写的,但它给了我很多错误:

  • 注意:未定义索引:地区
  • 注意:未定义索引:村庄
  • 注意:未定义索引:sql
  • 警告:mysql_fetch_array() 期望参数 1 是资源,null

搜索.php

<?php
session_start();
if($_SESSION['login'] != 'true'){
        header("location:index.php");
    }


   $login = ($_SESSION['login']);
   $userid = ($_SESSION['user_id']);
   $login_user = ($_SESSION['username']);
   $fname = ($_SESSION['first_name']);
   $lname = ($_SESSION['last_name']);
   $sessionaddres =($_SESSION['address']);


require_once('for members/scripts/connect.php'); 

// function for selecting names
function nameQuery(){

  $nameData = mysql_query("SELECT * FROM user") or die("could not select database");

  while($record = mysql_fetch_array($nameData)){

     echo'<option value="' . $record['user_name'] .  '">' . $record['user_name'] . '</option>';

  }

}



// function for select by specialization

function specializationQuery(){

$specData = mysql_query("SELECT * FROM specialization");

  while($recordJob = mysql_fetch_array($specData)){

     echo'<option value="' . $recordJob['specialization_name'] .  '">' . $recordJob['specialization_name'] . '</option>';

  }


}

if(isset($_POST['search']))
{
    $Sname =$_POST['name']; 
    $Sspec = $_POST['specialization'];
    $Sgov = $_POST['governorate'];
    $Sdist = $_POST['district'];
    $Svillage = $_POST['village'];

    // query  search by name
    if($Sname !=0)
    $sql = mysql_query("SELECT first_name, last_name, profile_pic FROM user WHERE user_name ='$Sname'")or die(mysql_error());
    while($getrow = mysql_fetch_array($sql))
    {
        $firstname = $getrow['first_name'];
        $lastname = $getrow['last_name'];
        $profilepic = $getrow['profile_pic'];

        var_dump($firstname);
        var_dump($lastname);
        var_dump($profilepic);


        echo "<ul>
                 <li>
                    '.$firstname'  '' '.$lastname'
                 </li>
                 <li>
                  '.$profilepic'
                 </li>
              </ul>";
    }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>search page</title>
<link href="style/stylesheet.css" rel="stylesheet" type="text/css" />
<script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<script type="text/javascript">

 $(document).ready(function(){
                $("select#district").attr("disabled","disabled");
                $("select#village").attr("disabled","disabled");
                $("select#governorate").change(function(){
                $("select#district").attr("disabled","disabled");
                $("select#district").html("<option>wait...</option>");
                var id = $("select#governorate option:selected").attr('value');
                $.post("select_district.php", {id:id}, function(data){
                    $("select#district").removeAttr("disabled");
                    $("select#district").html(data);
                });
            });
            $("select#district").change(function(){ 
            id = $(this).val(); 
            $("select#village").attr("disabled","disabled");
            $("select#village").html("<option>wait...</option>");
            $.post("select_village.php", {id:id}, function(data){


            $("select#village").removeAttr("disabled");
            $("select#village").html(data);
         });
        });
            $("form#registerform").submit(function(){
                var cat = $("select#governorate option:selected").attr('value');
                var type = $("select#district option:selected").attr('value');
                var village = $("select#village option:selected").attr('value');

            });
    });


</script>
</head>


<body>
<div class="container">
<!--<?php require_once('header.php'); ?>-->
<br />
<br />
<br />
<br />

  <!-- <?php require_once('leftsideBar2.php'); ?>-->


   <div id="search-title">Search section</div>
    <div id="search-form">
    <?php include "select.class.php"; ?>
        <form action="search.php" method="post">
         Search By Name:<br />
         <select name="name" >
            <?php nameQuery(); ?>
             <option id="0">-- select By UserName --</option>
             </select>
           <br/><br/>
           Search By Governorate:<br />

        <select id="governorate" name = 'governorate'>
                <?php echo $opt->ShowGovernorate(); ?>
                </select>
           <br /><br/>
           Search by District:<br />

      <select id="district" name="district">
                    <option value="0">choose...</option>
                </select>

           <br /><br/>
           Search by Cities:<br />
           <select id="village" name="village">
                    <option value="0">choose...</option>
                </select>
           <br /><br/>
           Search By Specialization:<br />
           <select name="specialization">
             <option id="0" disabled="disabled">-- select Job --</option>

          <?php specializationQuery(); ?>
          </select>
          <input type="submit" name="search" value="Search"  />
        </form>
    </div>
</div>
<?php require_once('footer.php'); ?>
</body>
</html>
4

1 回答 1

0

您应该检查 $_POST 中的请求变量是否使用 isset 正确设置:

if (isset($_POST["district"])) { $district = $_POST["district"]; }
//etc

我没有在此代码中看到对名为“sql”的索引的引用,您可能需要检查稍后包含在 search.php 中的“select.class.php”

对于最后一个问题,您应该检查所有查询是否有效;如果不是,mysql_query() 将不会返回结果(mysql_query() 返回类型资源),然后 mysql_fetch_array() 将无法获取数组(mysql_fetch_array() 需要资源,但您有 null)。

于 2013-04-26T02:08:38.500 回答