Simple search MySQL database using php

172,326

Solution 1

You need to use $_POST not $_post.

Solution 2

First add HTML code:

<form action="" method="post">
<input type="text" name="search">
<input type="submit" name="submit" value="Search">
</form>

Now added PHP code:

<?php
$search_value=$_POST["search"];
$con=new mysqli($servername,$username,$password,$dbname);
if($con->connect_error){
    echo 'Connection Faild: '.$con->connect_error;
    }else{
        $sql="select * from information where First_Name like '%$search_value%'";

        $res=$con->query($sql);

        while($row=$res->fetch_assoc()){
            echo 'First_name:  '.$row["First_Name"];


            }       

        }
?>

Solution 3

I think its works for everyone

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Search</title>
</head>

<body>
    <form action="" method="post">
        <input type="text" placeholder="Search" name="search">
        <button type="submit" name="submit">Search</button>
    </form>
</body>

</html>
<?php


if (isset($_POST['submit'])) {
    $searchValue = $_POST['search'];
    $con = new mysqli("localhost", "root", "", "testing");
    if ($con->connect_error) {
        echo "connection Failed: " . $con->connect_error;
    } else {
        $sql = "SELECT * FROM customer_info WHERE name OR email LIKE '%$searchValue%'";

        $result = $con->query($sql);
        while ($row = $result->fetch_assoc()) {
            echo $row['name'] . "<br>";
            echo $row['email'] . "<br>";
        }

      
    }   
}



?>
Share:
172,326

Related videos on Youtube

TimelordViktorious
Author by

TimelordViktorious

Updated on September 15, 2022

Comments

  • TimelordViktorious
    TimelordViktorious over 1 year

    I currently have a small php script that searches a database based on user input. There is an html file that has one field that is used for entry of search strings into the database. Essentially, you can search for employees.

    It is supposed to retrieve the employee result, if found, and a 'no employee found' message, if not.

    For some reason, however, no matter the search, the query returns every employee in the database.

    I've been working on this for over an hour, and I am honestly stumped. It may be a simple error but I could do with some help.

    <?php
        $con= new mysqli("localhost","root","","Employee");
        $name = $_post['search'];
        //$query = "SELECT * FROM employees
       // WHERE first_name LIKE '%{$name}%' OR last_name LIKE '%{$name}%'";
    
        // Check connection
        if (mysqli_connect_errno())
          {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
          }
    
    $result = mysqli_query($con, "SELECT * FROM employees
        WHERE first_name LIKE '%{$name}%' OR last_name LIKE '%{$name}%'");
    
    while ($row = mysqli_fetch_array($result))
    {
            echo $row['first_name'] . " " . $row['last_name'];
            echo "<br>";
    }
        mysqli_close($con);
        ?>
    

    Any help appreciated.

    Thanks.