The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
SEARCH box by last name post data to the form**
Discuss SEARCH box by last name post data to the form** in the PHP Development forum on Dev Shed. SEARCH box by last name post data to the form** PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 3rd, 2012, 07:10 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 24
Time spent in forums: 6 h 21 m 42 sec
Reputation Power: 0
|
|
|
SEARCH box by last name post data to the form**
On the following page I want the user to input a last name and pull that data and POST the results into the form below. I think one of my problems is I shouldn't have a foreach and it should just be an echo. Also, does anyone have any tips for me, I've been stuck on this for a couple days and can't solve it.
CUSTOMER_SEARCH.PHP:
PHP Code:
<?php include '../view/header.php'; ?>
<div id="main">
<h1>Customer Search</h1>
<div id="content">
<form action="." method="post" id="aligned"
<input type="hidden" name="action" value="get_customers" />
<label>Last Name</label>
<input type="text" name="last_name" />
<br />
<label> <label>
<input type="submit" value="Search" />
</div>
<div id="content">
<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 $customer['firstName'] .' '. $customer['lastName']; ?></td>
<td><?php echo $customer['email']; ?></td>
<td><?php echo $customer['city']; ?></td>
<td><form action="customer_display.php" method="post">
<input type="hidden" name="action" value="get_customer" />
<input type="hidden" name="customer_id"
value="<?php echo $customer['customer_ID']; ?>" />
<input type="submit" value="Select" />
</form>
<?php endforeach;?>
The customer_db.php page:
PHP Code:
<?php
function get_customers() {
global $db;
$query = 'SELECT * FROM customers
ORDER BY lastName';
$customers = $db->query($query);
return $customers;
}
function get_customers_by_last_name($last_name) {
global $db;
$query = "SELECT * FROM customers
WHERE lastName = '$last_name'
ORDER BY lastName";
$customers = $db->query($query);
return $customers;
}
function get_customer($customer_id) {
global $db;
$query = "SELECT * FROM customers
WHERE customerID = '$customer_id'";
$customers = $db->query($query);
return $customers;
}
function get_customer_by_email($email) {
global $db;
$query = "SELECT * FROM customers
WHERE email = '$email'";
$customers = $db->query($query);
return $customers;
}
function delete_customer($first_name) {
global $db;
$query = "DELETE FROM customers
WHERE firstName = '$first_name'";
$db->exec($query);
}
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')";
$db->exec($query);
}
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 firstName = '$first_name'
lastName = '$last_name'
address = '$address'
city = '$city'
state = '$state'
postalCode = '$postal_code'
countryCode = '$country_code'
phone = '$phone'
email = '$email'
password = '$password'
WHERE customerID = '$customer_id'";
$db->exec($query);
}
?>
This is the customer_display.php page which when the user SELECTS the results will display the CUSTOMER data. I'm just not sure how to fix this. Any help is appreciated, thanks guys.
PHP Code:
<?php include '../view/header.php'; ?>
<div id="main">
<div id="content">
<!-- display a table of customer info. -->
<h2>View/Update Customer</h2>
<form action="customer_display.php" method="post" id="aligned">
<input type="hidden" name="action" value="add_customer" />
<label>First Name:</label>
<input type="text" name="first_name" /> <br />
<label>Last Name:</label>
<input type="text" name="last_name" value="<?php echo $customer['lastName'];?>" /> <br />
<label>Address:</label>
<input type="text" name="address" value="<?php echo $customer['address'];?>" /> <br />
<label>City:</label>
<input type="text" name="city" value="<?php echo $customer['city'];?>"/><br />
<label>State:</label>
<input type="text" name="state" value="<?php echo $customer['state'];?>"/><br />
<label>Postal Code:</label>
<input type="text" name="postal_code" value="<?php echo $customer['postalCode'];?>"/><br />
<label>Country Code:</label>
<input type="text" name="country_code" value="<?php echo $customer['countryCode'];?>"/><br />
<label>Phone:</label>
<input type="text" name="phone" value="<?php echo $customer['phone'];?>"/><br />
<label>Email:</label>
<input type="text" name="email" value="<?php echo $customer['email'];?>"/><br />
<label>Password:</label>
<input type="text" name="password" value="<?php echo $customer['password'];?>"/><br />
<label> </label>
<input type="submit" name="action" value="Update Customer" />
<br />
</form>
<p><a href="index.php">Search Customers</a></p>
</div>
</div>
<?php include '../view/footer.php'; ?>
Here's the INDEX.PHP
PHP Code:
<?php
require('../model/database.php');
require('../model/customer_db.php');
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else if (isset($_GET['action'])) {
$action = $_GET['action'];
} else {
$action = 'list_customers';
}
if ($action == 'list_customers') {
$customers = get_customers();
// Display the customer list
include('customer_search.php');
} else if ($action == 'delete_customer') {
$customer_id = $_POST['customer_id'];
delete_customer($customer_id);
header("Location: .");
} else if ($action == 'show_add_form') {
$customers = get_customers();
include('customer_display.php');
} else if ($action == 'add_customer') {
$first_name = $_POST['firstName'];
$last_name = $_POST['lastName'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$postal_code = $_POST['postalCode'];
$country_code = $_POST['countryCode'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$password = $_POST['password'];
// Validate the inputs
if (empty($first_name) || empty($last_name) || empty($address) || empty($city) || empty($state)
|| empty($postal_code) || empty($country_code) || empty($phone) || empty($email)
|| empty($password)) {
$error = "Invalid product data. Check all fields and try again.";
include('../errors/error.php');
} else {
add_customer($first_name, $last_name, $address, $city, $state, $postal_code, $country_code, $phone,
$email, $password);
header("Location: .");
}
}
?>
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|