How can I submit a search to MySQL that contains multiple keywords from multiple text inputs in a form using PHP and MYSQL?

  mysql, php

I am trying to create a project that allows the end user to search for a "patient’s" full name, date of birth, phone number, and so on.

I have created the form to do so in HTML, but I am having a problem with the PHP and was hoping to get some tips and/or see some code to guide me in the correct way. I am relatively new to PHP, so I am still learning new things everyday, so please excuse my coding mess.

The PHP code I tried just lists all "patients" in the database. It is my hope that if, for example, the end user puts in the date of birth 01/01/2000, the system will show all "patients" with that date of birth. I would also hope to make it where if the user puts in a first name and date of birth, it will list just that "patient". I have attached everything I have so far.

This is my HTML,

  <!DOCTYPE html>
  <html lnag="en">
  <head>
    <meta charset="UTF-8">
    <title><?php echo htmlspecialchars($_SESSION["username"]); ?></title>
    <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div class="topnav">
        <a href="logout.php">Logout</a>
        <a href="#admin">Administration</a>
        <a href="#resources">Resources</a>
        <div class="dropdown">
            <button class="dropbtn">Patient Actions
            <i class="fa fa-caret-down"></i>
            </button>
            <div class="dropdown-content">
             <a href="new_enroll.php">New Member Enrollment</a>
             <a href="cmr_home.php">Patient Search</a>
             <a href="#">Link 3</a>
        </div>
    </div>
        <a class="active" href="home.php">Home</a>
    </div>
    <form name="search"  action="search.php" method="GET">
    <h2>Begin by searching a patient</h2>
            <label>First Name:</label>
            <input type="text" name="search">
    <br>
    <br>
            <label>Middle Initial:</label>
            <input type="text" name="middle_init">
    <br>
    <br>
            <label>Last Name:</label>
            <input type="text" name="last_name">
    <br>
    <br>
            <label>Date of birth</label>
            <input type="date" name="dob">
    <br>
    <br>
            <label>Phone Number</label>
            <input type="text" name="phone_number">
    <br>
    <br>
            <label>Patient Identification Number</label>
            <input type="text" name="pt_id_num">
    <br>
    <br>
            <input type="submit" name="submit" class="submit" value="Search">
            <input type="reset" class="cancelbutton" value="Clear">
    </form>

</body>

This is the PHP I have that is just printing the data from the tables,

<?php
// Attempt MySQL server connection.
$mysqli = new mysqli("localhost", "root", "", "safeguardrx_patients");

// Check connection
if($mysqli === false){
die("ERROR: Could not connect. " . $mysqli->connect_error);
}

// Attempt select query execution
$sql = "SELECT * FROM patients";
if($result = $mysqli->query($sql)){
if($result->num_rows > 0){
    echo "<table>";
        echo "<tr>";
            echo "<th>Patient ID Number</th>";
            echo "<th>First Name</th>";
            echo "<th>Last Name</th>";
            echo "<th>Email</th>";
        echo "</tr>";
    while($row = $result->fetch_array()){
        echo "<tr>";
            echo "<td>" . $row['pt_id_num'] . "</td>";
            echo "<td>" . $row['first_name'] . "</td>";
            echo "<td>" . $row['last_name'] . "</td>";
            echo "<td>" . $row['email'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
    // Free result set
    $result->free();
   } else{
      echo "No records matching your query were found.";
  }
 } else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}

// Close connection
$mysqli->close();
?>

Source: Ask PHP

LEAVE A COMMENT