php / mysql显示/从结果中选择

时间:2016-09-23 01:00:10

标签: php mysql

我试图将查询结果显示为另一个可选查询 我使用一个简单的搜索表单,检查现有数据,然后显示它,我想要的是获得链接(一个href)与数据库中的实际id字段一起工作,用户搜索他/她的数据,弹出他/她的记录,然后添加客户'链接将使用' id'从上一个查询作为下一个的var。

index.php
<html xmlns="http://www.w3.org/1999/xhtml">
<?php include '/var/www/sitevars.php'; ?>
<head>
    <title>Search</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="<?php echo $siteurl; ?>/style.css"  />
</head>
<body>
    <form action="search.php" method="GET">
        <input type="text" name="query" />
        <input type="submit" value="Search" />
    </form>
 <h1>Search for either firstname or lastname, 3 characters minimum.</h1>
</body>
</html>

search.php
<!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">
<?php include '/var/www/sitevars.php'; ?>
<head>
    <title>Search results</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="<?php echo $siteurl; ?>/style.css"  />
</head>
<body>
<?php
    include '/var/www/dbcon.php';
    $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 = mysqli_real_escape_string($link,$query);
        // makes sure nobody uses SQL injection

        $raw_results = mysqli_query($link,"SELECT * FROM acis
            WHERE (`firstname` LIKE '%".$query."%') OR (`lastname` LIKE '%".$query."%')") or die(mysqli_error());

        // * means that it selects all fields, you can also write: `id`, `title`, `text`
        // articles 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(mysqli_num_rows($raw_results) > 0){ // if one or more rows are returned do following

            while($results = mysqli_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><a href=\"$link_address\">Add customer</a> <b>".$results['id']."  - ".$results['firstname']."  ".$results['lastname']."</b>  -  ".$results['address']."  ".$results['city']."  ".$results['zip']."  ".$results['state']."  ".$results['countyname']."  ".$results['trial_date']."  ".$results['charge1']."  ".$results['case_number']."</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 character length for search is ".$min_length;
    }
?>
</body>
</html>

0 个答案:

没有答案