I'm needing to be able to get a search bar to work in PHP using HTML. I have done some coding, but cannot fully grasp how to do it. What can I do to get the search bar to work? I am needing to be able to search for a last name and have it display the name. There is no set first name or last name, just the name of the person all in one (if that makes sense)
I have attached the files that I am using.
Chloe_6
0
Newbie Poster
<?php
function get_customers() {
global $db;
$query = 'SELECT * FROM customers
ORDER BY first_name';
$statement = $db->prepare($query);
$statement->execute();
$customer = $statement->fetchAll();
$statement->closeCursor();
return $customers;
}
function get_customers_by_last_name($last_name) {
global $db;
$query = 'SELECT customers.customerID, customers.firstName
FROM customers
INNER JOIN registrations ON customers.customerID = registrations.customerID
WHERE customers.lastName = :last_name';
$statement = $db->prepare($query);
$statement->bindValue(':lsat_name', $last_name);
$statement->execute();
$customer = $statement->fetchAll();
$statement->closeCursor();
return $customers;
}
function get_customer($customer_id) {
global $db;
$query = 'SELECT * FROM customers
WHERE customerID = :customer_id';
$statement = $db->prepare($query);
$statement->bindValue(':customer_id', $customer_id);
$statement->execute();
$customer = $statement->fetch();
$statement->closeCursor();
return $customers;
}
function get_customer_by_email($email) {
global $db;
$query = 'SELECT customers.customerID, customers.firstName
FROM customers
INNER JOIN registrations ON customers.customerID = registrations.customerID
WHERE customers.email = :email';
$statement = $db->prepare($query);
$statement->bindValue(':email', $email);
$statement->execute();
$customer = $statement->fetchAll();
$statement->closeCursor();
return $customers;
}
function delete_customer($customer_id) {
global $db;
$query = 'DELETE FROM customers
WHERE customerID = :customer_id';
$statement = $db->prepare($query);
$statement->bindValue(':customer_id', $customer_id);
$statement->execute();
$statement->closeCursor();
}
function add_customer($first_name, $last_name,
$address, $city, $state, $postal_code, $country_code,
$phone, $email, $password) {
global $db;
$query = 'INSERT INTO customers
(firstName, lastName, address, city, state, postalCode, countryCode, phone, email, password)
VALUES
(:first_name, :last_name, :address, :city, :state, :postal_code, :country_code, :phone, :email, :password)';
$statement = $db->prepare($query);
$statement->bindValue(':first_name', $first_name);
$statement->bindValue(':last_name', $last_name);
$statement->bindValue(':address', $address);
$statement->bindValue(':city', $city);
$statement->bindValue(':state', $state);
$statement->bindValue(':postal_code', $postal_code);
$statement->bindValue(':country_code', $country_code);
$statement->bindValue(':phone', $phone);
$statement->bindValue(':email', $email);
$statement->bindValue(':password', $password);
$statement->execute();
$statement->closeCursor();
}
function update_customer($customer_id, $first_name, $last_name,
$address, $city, $state, $postal_code, $country_code,
$phone, $email, $password) {
global $db;
$query = 'UPDATE customers
SET first_name = :first_name,
last_name = :last_name,
address = :address,
city = :city,
state = :state,
postal_code = :postal_code,
country_code = :country_code,
phone = :phone,
email = :email,
password = :password
WHERE customerID = :customer_id';
$statement = $db->prepare($query);
$statement->bindValue(':first_name', $first_name);
$statement->bindValue(':last_name', $last_name);
$statement->bindValue(':address', $address);
$statement->bindValue(':city', $city);
$statement->bindValue(':state', $state);
$statement->bindValue(':postal_code', $postal_code);
$statement->bindValue(':country_code', $country_code);
$statement->bindValue(':phone', $phone);
$statement->bindValue(':email', $email);
$statement->bindValue(':password', $password);
$statement->execute();
$statement->closeCursor();
}
?>
<?php include '../view/header.php'; ?>
<main>
<h2>Customer Search</h2>
<!-- display a search form -->
<form action="customer_search.php" method="post">
<input type="hidden" name="action" value="search_customers">
<input type="text" name="name">
<input type="submit" value="Search">
</form>
<?php if (isset($message)) : ?>
<p><?php echo $message; ?></p>
<?php elseif ($customers) : ?>
<h2>Results</h2>
<table>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>City</th>
<th> </th>
</tr>
<?php foreach ($customers as $customer) : ?>
<tr>
<td><?php echo htmlspecialchars($customer['name']); ?></td>
<td><?php echo htmlspecialchars($customer['email']); ?></td>
<td><?php echo htmlspecialchars($customer['city']); ?></td>
<td><form action="index.php" method="post">
<input type="hidden" name="action"
value="display_customers">
<input type="hidden" name="name"
value="<?php echo htmlspecialchars($customer['name']); ?>">
<input type="submit" value="Select">
</form></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
</main>
<?php include '../view/footer.php'; ?>
<?php
require('../model/database.php');
require('../model/customer_db.php');
$action = filter_input(INPUT_POST, 'action');
if ($action === NULL) {
$action = filter_input(INPUT_GET, 'action');
if ($action === NULL) {
$action = 'list_customers';
}
}
//instantiate variable(s)
$last_name = '';
$customers = array();
if ($action == 'search_customers') {
// ???
} else if ($action == 'display_customers') {
$name = filter_input(INPUT_POST, 'name');
if ($name != false) {
$query = 'SELECT FROM customers WHERE name = :name';
$statement = $db->prepare($query);
$statement->bindValue(':name', $name);
$success = $statement->execute();
$statement->closeCursor();
}
include('customer_display.php');
} else if ($action == 'display_customer') {
$code = filter_input(INPUT_POST, 'code');
$name = filter_input(INPUT_POST, 'name');
$version = filter_input(INPUT_POST, 'version', FILTER_VALIDATE_FLOAT);
$release_date = filter_input(INPUT_POST, 'release_date');
} else if ($action == 'update_customer') {
$name = filter_input(INPUT_POST, 'name');
if ($name != false) {
$query = 'SELECT FROM customers WHERE name = :name';
$statement = $db->prepare($query);
$statement->bindValue(':name', $name);
$success = $statement->execute();
$statement->closeCursor();
}
include('customer_search.php');
}
?>
Recommended Answers
Jump to PostCan you please explain what isn't working as you would expect it to? Are you getting any error messages?
I see that in customer_search.php you have the code:
<form action="customer_search.php" method="post">
However, it looks like customer_search.php is meant to be called from within index.php, and never called …
All 2 Replies
Dani
4,084
The Queen of DaniWeb
Administrator
Featured Poster
Premium Member
Can you please explain what isn't working as you would expect it to? Are you getting any error messages?
I see that in customer_search.php you have the code:
<form action="customer_search.php" method="post">
However, it looks like customer_search.php is meant to be called from within index.php, and never called directly. Therefore, the form action should be index.php.
Then on index.php, you would do something like:
if ($action == 'search_customers') {
$name = filter_input(INPUT_POST, 'name');
$customers = get_customers_by_last_name($name);
}
Chloe_6
0
Newbie Poster
When I go to do the changes that you said about then click on the button it shows this (picture below). I'm trying to get the search bar to search for a customers last name and then show in a table below their full name (which in the sql database is under first name and last name), their email address and their city. From there I should be able to click on a select button that is beside the customers information and it'll take me to the customer_display.php page where I can view all of their information.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.