2022-11-29 15:48:18 +00:00
< ? php
session_start ();
require_once 'config.php' ;
2022-12-03 12:57:42 +00:00
header ( 'Content-Type: application/json' );
2022-11-29 15:48:18 +00:00
if ( isset ( $_SESSION [ " username " ]))
{
$searchType = $_POST [ " searchType " ];
$searchField = $_POST [ " searchField " ];
$conn = dbConn ();
if ( $searchType === " dln " )
{
$stmt = $conn -> prepare ( " SELECT People_name, People_address, People_licence FROM People WHERE People_name LIKE :name OR People_licence = :searchField " );
$name = " % " . $searchField . " % " ;
$stmt -> bindParam ( " :name " , $name );
2022-12-16 12:19:38 +00:00
$logSQL = " INSERT INTO Logs (Logs_type, Users_username, Logs_date) VALUES ('Search by driving licence number', :username, NOW()) " ;
$logStmt = $conn -> prepare ( $logSQL );
$logStmt -> bindParam ( " :username " , $_SESSION [ " username " ]);
$logStmt -> execute ();
2022-11-29 15:48:18 +00:00
}
else
{
$stmt = $conn -> prepare ( " SELECT P.People_name, Vehicle_type, Vehicle_colour, Vehicle_licence FROM Vehicle LEFT JOIN Ownership O on Vehicle.Vehicle_ID = O.Vehicle_ID LEFT JOIN People P on O.People_ID = P.People_ID WHERE Vehicle_licence = :searchField " );
2022-12-16 12:19:38 +00:00
$logSQL = " INSERT INTO Logs (Logs_type, Users_username, Logs_date) VALUES ('Search by plate number', :username, NOW()) " ;
$logStmt = $conn -> prepare ( $logSQL );
$logStmt -> bindParam ( " :username " , $_SESSION [ " username " ]);
2022-11-29 15:48:18 +00:00
}
$stmt -> bindParam ( " :searchField " , $searchField );
$stmt -> execute ();
$result = $stmt -> fetchAll ( PDO :: FETCH_ASSOC );
if ( count ( $result ) > 0 )
{
echo json_encode ( array ( " message " => " ok " , " data " => $result ));
}
else
{
echo json_encode ( array ( " message " => " No results found " ));
}
}
else
{
echo json_encode ( array ( " message " => " Not logged in " ));
2022-12-16 12:19:38 +00:00
}