Click here to Skip to main content
15,886,530 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to perform a live search using Ajax,PHP, and MYSQL. The code I have is working. However, the main problem is I can't redirect the user to their profile page once I found the user within the database. I checked the console.log and it has a link to the their profile page. This is the results https://imgur.com/a/I8tXMvH

HTML
HTML
<div class="inner-addon left-addon">
          
          <input class="form-control" type ="text" id ="search"  placeholder="Search for employees..." onkeyup="search(this.value)">
          <div id ="results"></div>
      </div


Ajax Code
JavaScript
      function search(value) {
      if(value.length == 0) 
      {
        $("#results").html("");
      } else {

        $.post("employees.php", {search:value}, function(data){
          $("#results").html(data);
        });
 }
}


PHP Code
PHP
    $search = $_POST['search'];
  $query =" SELECT * FROM employees WHERE firstName LIKE '%$search%' OR lastName LIKE '%$search%'";
   $query = mysqli_query($connect,$query);
  while($row= mysqli_fetch_array($query)){
    echo "<div>";
    echo $row['firstName']."  ".$row['lastName'];
    echo "<a href='profile.php?userId=" . $row["id"] . "'>"; //this is not working
    echo "</div>";

}


User Profile code
PHP
<?php
 $userId = ($_GET['userId']);
    
$query = "SELECT firstName, lastName, middleName, gender, ssn, dob, organization, department, iden, position, salary,health, dental,vision, addressOne, addressTwo, apt, city, _state, zipcode, phone, email FROM employees WHERE id = {$userId}";
$results = mysqli_query($connect, $query);

   while($row = mysqli_fetch_array($results)) {
       $firstName = $row['firstName'];
       $lasttName = $row['lastName'];
       $middletName = $row['middleName'];
       $gender = $row['gender'];     
?>


What I have tried:

I have tried everything and I just can't figure it out.
Posted
Updated 23-Oct-18 3:29am

PHP
$query =" SELECT * FROM employees WHERE firstName LIKE '%$search%' OR lastName LIKE '%$search%'";

Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
Comments
Member 14028652 22-Oct-18 15:42pm    
Is this the the cause to my problem?
Patrice T 22-Oct-18 15:46pm    
you have 6 links at the end of solution, have a good reading.
You have an empty <a> element, which the user will not be able to click on.

Move the opening tag before the name, and add a closing tag:
PHP
echo "<div>";
echo "<a href='profile.php?userId=" . htmlspecialchars($row["id"]) . "'>";
echo htmlspecialchars($row['firstName'])."  ".htmlspecialchars($row['lastName']);
echo "</a>";
echo "</div>";

NB: Use htmlspecialchars[^] to avoid persisted cross site scripting (XSS) attacks[^].

And don't ignore the SQL Injection vulnerabilities that Patrice mentioned.
 
Share this answer
 
Comments
Member 14028652 23-Oct-18 18:51pm    
Thank you so much. I figured it out after few hours.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900