按姓氏PHP

时间:2017-02-01 03:48:41

标签: php search

Yahallo!我在搜索数据库中的用户并将其显示到文本字段中时遇到问题。我已经将所有内容设置为通过键入lastname来查询和搜索用户,但它给了我一个错误

  

致命错误:未捕获的异常' mysqli_sql_exception'带有消息'您的SQL语法中有错误;查看与您的MariaDB服务器版本对应的手册,以获得在''附近使用的正确语法。在第1行'



<?php
$host = "localhost";
$user = "root";
$password ="";
$database = "ntmadb";

$id = "";
$firstname = "";
$lastname = "";
$username = "";

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// connect to mysql database
try{
    $connect = mysqli_connect($host, $user, $password, $database);
} catch (mysqli_sql_exception $ex) {
    echo 'Error';
}

// get values from the form
function getPosts()
{
    $posts = array();
    $posts[0] = $_POST['id'];
    $posts[1] = $_POST['firstname'];
    $posts[2] = $_POST['lastname'];
    $posts[3] = $_POST['username'];
    return $posts;
}

// Search

if(isset($_POST['search']))
{
    $data = getPosts();
    
    $search_Query = "SELECT * FROM members WHERE lastname = $data[2]";
    
    $search_Result = mysqli_query($connect, $search_Query);
    
    if($search_Result)
    {
        if(mysqli_num_rows($search_Result))
        {
            while($row = mysqli_fetch_array($search_Result))
            {
                $id = $row['id'];
                $firstname = $row['firstname'];
                $lastname = $row['lastname'];
                $username = $row['username'];
            }
        }else{
            echo 'No Data For This Id';
        }
    }else{
        echo 'Result Error';
    }
}
?>
<!DOCTYPE Html>
<html>
    <head>
        <title>PHP INSERT UPDATE DELETE SEARCH</title>
    </head>
    <body>
        <form action="index44.php" method="post">
            <input type="text" name="id" placeholder="Id" value="<?php echo $lastname;?>"><br><br>
            <input type="text" name="firstname" placeholder="First Name" value="<?php echo $firstname;?>"><br><br>
            <input type="text" name="lastname" placeholder="Last Name" value="<?php echo $lastname;?>"><br><br>
            <input type="text" name="username" placeholder="User Name" value="<?php echo $username;?>"><br><br>
            <div>
             
                <!-- Input For Edit Values -->
                <input type="submit" name="update" value="Update">
                
              
                
                <!-- Input For Find Values With The given ID -->
                <input type="submit" name="search" value="Find">
            </div>
        </form>
    </body>
</html>
&#13;
&#13;
&#13;

有人可以协助修复吗?谢谢!

2 个答案:

答案 0 :(得分:2)

尝试以下查询。

$search_Query = "SELECT * FROM members WHERE lastname = '".$data[2]."'";

答案 1 :(得分:0)

试试这个:

$temp_lastname = $data[2];
$search_Query = "SELECT * FROM `members` WHERE `lastname` = '$temp_lastname'";