图像单击会创建特定的php查询,但不会返回正确的结果

时间:2015-03-23 10:29:47

标签: php html mysql

您好我有一个搜索查询页面。当用户输入关键字,然后按搜索,它将转到jobsearch.php并从MySQL数据库中打印出作业列表。它很棒。

但是我有一个可点击的图片而不是文本查询(见下文)enter image description here

因此,当用户点击技术作业时,它会打印出一份技术作业列表,当用户点击服务作业时......它会打印出服务作业等。

然而,它打印出所有工作,而不仅仅是适用的工作。我已将表中的工作分类。我是否需要创建一个新的PHP查询页面才能打印出不同的类别?或者是否可以添加要添加到的关键字:

<a class="white-bg" href="/jobsearch.php"><img alt="" src="images/clients/client-1.png"></a></div>

index.html的这一部分

<h3 class="main-color">Search categories</h3>
                                                                     <div class="clients">







                                                                     <div> <a class="white-bg" href="/jobsearch.php"><img alt="" src="images/clients/client-1.png"></a></div>


                                                                    <div> <a class="white-bg" href="#"><img alt="" src="images/clients/client-2.png"></a></div>
                                                                    <div> <a class="white-bg" href="#"><img alt="" src="images/clients/client-7.png"></a></div>
                                                                <div> <a class="white-bg" href="#"><img alt="" src="images/clients/client-3.png"></a></div>
                                                                <div> <a class="white-bg" href="#"><img alt="" src="images/clients/client-4.png"></a></div>
                                                                <div> <a class="white-bg" href="#"><img alt="" src="images/clients/client-9.png"></a></div>
                                                                <div> <a class="white-bg" href="#"><img alt="" src="images/clients/client-5.png"></a></div>
                                                                <div> <a class="white-bg" href="#"><img alt="" src="images/clients/client-5.png"></a></div>
                                                                <div> <a class="white-bg" href="#"><img alt="" src="images/clients/client-8.png"></a></div>
                                                                         <div> <a class="white-bg" href="#"><img alt="" src="images/clients/client-6.png"></a></div>
                                                                    <div></div>

这是jobsearch.php

<form action="jobsearch.php" method="GET" form class="form-wrapper cf">
        <input type="text" name="query" placeholder="Job Search." />
        <button type="submit" value="Search" /><span class="fa fa-search"></span></button>
</form>
<div class="info-block"><h2>
<?php
    mysql_connect("******", "root", "***********") or die("Error connecting to database: ".mysql_error());
    /*

        if connection fails it will stop loading the page and display an error
    */

    mysql_select_db("jobslist") or die(mysql_error());
    /* tutorial_search is the name of database we've created */


    $query = $_GET['query'];
    // gets value sent over search form

   // $min_length = 3;
    // you can set minimum length of the query if you want

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

        $query = htmlspecialchars($query);
        // changes characters used in html to their equivalents, for example: < to &gt;

        $query = mysql_real_escape_string($query);
        // makes sure nobody uses SQL injection

        $raw_results = mysql_query("SELECT * FROM jobs_list
            WHERE (`job_title` LIKE '%".$query."%') OR (`job_location` LIKE '%".$query."%')          OR (`job_category` LIKE '%".$query."%')  ") or die(mysql_error());

        // * means that it selects all fields, you can also write: `id`, `title`, `text`
        // jobs_list is the name of our table

        // '%$query%' is what we're looking for, % means anything, for example if $query is Hello
        // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
        // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'

        if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

            while($results = mysql_fetch_array($raw_results)){
            // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo
        '<p>'
      .$results['id'] . ' '
     . $results['job_title'] . ' '
     . $results['job_description']. ' '
     . $results['job_location']. ' '
     . $results['job_category'] . ' '
     . '</p>';
                // posts results gotten from database(title and text) you can also show id ($results['id'])
            }

        }
        else{ // if there is no matching rows do following
            echo "No results";
        }

    }
    else{ // if query length is less than minimum
        echo "Minimum length is ".$min_length;
    }
?>

1 个答案:

答案 0 :(得分:2)

您应该将参数作为GET变量(url参数)提供给jobsearch.php

所以它应该是:

<a href="/jobsearch.php?query=Technical+Jobs"><img alt="" src="images/clients/client-1.png"></a>