用于搜索字符串中包含空格的字符串的php脚本

时间:2016-01-18 04:13:19

标签: php

我在搜索包含空格(manish pandey)的字符串时遇到问题。 php脚本用于搜索(manish)而不是全名(manish pandey) 我在这里使用获取功能:
imageimage2



<!doctype html>
<html lang="en-US">
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset="UTF-8">
</head>
<body>
<?php
/*
Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password)
*/
$link = mysqli_connect("localhost", "root", "root123", "sample");
 
// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
 
// Attempt select query execution
$sql = "SELECT * FROM qcdataa1 Where project_id='".$_GET['pid']."'";
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
        echo "<table>";
            echo "<tr>";
                echo "<th>sno</th>";
                echo "<th>project_id</th>";
                echo "<th>project_name</th>";
                echo "<th>application</th>";
                echo "<th>investigator</th>";
                echo "<th>created_by</th>";
                echo "<th>creation_date</th>";
                echo "<th>last_modified</th>";
            echo "</tr>";
        while($row = mysqli_fetch_array($result)){
            echo "<tr>";
                echo "<td>" .$row['sno'].  "</td>";
                echo "<td>" .$row['project_id'] . "</td>";
                echo "<td>" .$row['project_name']. "</td>";

echo "<td>" .$row['application']. "</td>";
                echo "<td><a href=fetchi2.php?iid=" .$row['investigator'].">" .$row['investigator']."</a></td>";
                echo "<td>" .$row['created_by']. "</td>";
                echo "<td>" .$row['creation_date']. "</td>";
                echo "<td>" .$row['last_modified']. "</td>";


            echo "</tr>";
        }
        echo "</table>";
        // Close result set
        mysqli_free_result($result);
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
 
// Close connection
mysqli_close($link);
?>
</body>
</html>
&#13;
<!doctype html>
<html lang="en-US">
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset="UTF-8">
</head>
<body>
<?php
/*
Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password)
*/
$link = mysqli_connect("localhost", "root", "root123", "sample");
 
// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
 
// Attempt select query execution
echo $_GET['iid'];
$sql = "SELECT * FROM qcdataa2 Where first_name='".$_GET['iid']."'";
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
        echo "<table>";
            echo "<tr>";
                echo "<th>sno</th>";
                echo "<th>first_name</th>";
                echo "<th>last_name</th>";
                echo "<th>userid</th>";
                echo "<th>password</th>";
                echo "<th>emailid</th>";
                echo "<th>creation_date</th>";
                echo "<th>last_modified</th>";
            echo "</tr>";
        while($row = mysqli_fetch_array($result)){
            echo "<tr>";
                echo "<td>" .$row['sno'].  "</td>";
                echo "<td>" .$row['first_name'] . "</td>";
                echo "<td>" .$row['last_name']. "</td>";
               echo "<td>" .$row['userid']. "</td>";
               echo "<td>" .$row['password']. "</td>";
               echo "<td>" .$row['emailid']. "</td>";
              echo "<td>" .$row['creation_date']. "</td>";
              echo "<td>" .$row['last_modified']. "</td>";
            echo "</tr>";
        }
        echo "</table>";
        // Close result set
        mysqli_free_result($result);
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
 
// Close connection
mysqli_close($link);
?>
</body>
</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

首先使您的搜索字词安全,然后使用LIKE代替=在查询中使用它。

$safe = mysqli_real_escape_string($link, $_GET['iid']); 
$safe = trim($safe);

然后查询可以是,

$sql = "SELECT * FROM qcdataa2 Where first_name LIKE '$safe%' ";

答案 1 :(得分:0)

尝试使用urlencode()发送数据并使用urldecode()

对其进行解码