所以我有一个包含许多客户信息的数据库表。我想做一个CRUD,当在下拉菜单中选择客户时,它显示在我的HTML的输入字段中,以便我可以更新和删除它。我必须使用javascript吗?我有一个索引,一个数据库类和一个Customer类。
我的索引:
<?php
include_once("Customer.class.php");
include_once("DB_manager.class.php");
$db = new DB_manager();
$db->getAllCustomersName();
if(isset($_POST["button3"])){
if(isset($_POST["chkbox"])){
$newmember = "Yes";
}else{
$newmember = "No";
}
$info = array(
"customerNumber"=>$_POST["CustNumber"],
"customerName"=>$_POST["Middlename"],
"contactLastName"=>$_POST["Lastname"],
"contactFirstName"=>$_POST["Firstname"],
"phone"=>$_POST["phone"],
"addressLine1"=>$_POST["add1"],
"addressLine2"=>$_POST["add2"],
"city"=>$_POST["city"],
"state"=>$_POST["state"],
"postalCode"=>$_POST["pc"],
"country"=>$_POST["country"],
"salesRepEmployeeNumber"=>$_POST["salesR"],
"creditLimit"=>$_POST["limit"],
"membership"=>$_POST["rdiobox"],
"newClient"=>$newmember
);
$customer = new Customer($info);
$db->addCustomer($customer);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Customer DB</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" type="text/css" href="reset.css"/>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<div class="container">
<div class="main">
<header class="fix">
<h1>Herzing College<span>Manipulate Customer Database</span></h1>
</header>
<form>
<div><label for="clients" class="fs">Select Customer:</label>
<select name="clients" id="clients" class="fw" onchange="changeCustomer()">
<?php foreach ($_SESSION["data"] as $value) { ?>
<option><?php echo $value["customerNumber"] . " - " . $value["contactFirstName"] . " " . $value["contactLastName"] . " - " . $value["customerName"];} ?></option>
</select>
</div>
</form>
<br>
<hr>
<br>
<form id="ws" method="post" action="#">
<div class="divform">
<div class="r">
<div class="c"><label for="CustNumber" class="fs">Customer Number:</label><input id="CustNumber" name="CustNumber" type="text" required="true" /></div>
<div class="c"><label for="Middlename" class="fs">Customer Name:</label><input id="Middlename" name="Middlename" type="text" required="true" /></div>
<div class="c"><label for="Firstname" class="fs">Contact First Name:</label><input id="Firstname" name="Firstname" type="text" required="true" /></div>
<div class="c"><label for="Lastname" class="fs">Contact Last Name:</label><input id="Lastname" name="Lastname" type="text" required="true" /></div>
<div class="c"><label for="phone" class="fs">Phone:</label><input id="phone" name="phone" type="text" / required="true" ></div>
</div>
</div>
<div><label for="add1" class="fs">Address 1:</label><input id="add1" name="add1" type="text" class="fw" required="true"/></div>
<div><label for="add2" class="fs">Address 2:</label><input id="add2" name="add2" type="text" class="fw"/></div>
<div class="divform">
<div class="r">
<div class="c"><label for="city" class="fs" required="true">City:</label><input id="city" name="city" type="text" /></div>
<div class="c"><label for="state" class="fs">State:</label><input id="state" name="state" type="text" /></div>
<div class="c"><label for="pc" class="fs" required="true">Postal Code:</label><input id="pc" name="pc" type="text" /></div>
<div class="c"><label for="country" class="fs" required="true">Country:</label><input id="country" name="country" type="text" /></div>
</div>
<div class="r">
<div class="c"><span class="fheading fs">Membership</span><input id="rdiobox" name="rdiobox" type="radio" value="Premium" required="true" /><label for="rdiobox">Premium</label> <input id="rdiobox2" name="rdiobox" type="radio" value="Free" required="true" /><label for="rdiobox2">Free</label></div>
<div class="c"><span class="fheading fs">New Member</span><input id="chkbox" name="chkbox" type="checkbox" value="Yes"><label for="chkbox">Yes / No</label></div>
<div class="c"><label for="limit" class="fs">Credit Limit:</label><input id="limit" name="limit" type="text" required="true" /></div>
<div class="c"><label for="salesR" class="fs" >Sales Representative #:</label><input id="salesR" name="salesR" type="text" required="true" /></div>
</div>
<div class="r">
<div class="c"><input name="button1" type="submit" value="Edit" /></div>
<div class="c"><input name="button2" type="submit" value="Delete" /></div>
<div class="c"><input name="button3" type="submit" value="Add New Client" /></div>
</div>
</div>
</form>
</div>
</div>
<script type="text/javascript">
var select = document.getElementById("clients");
function changeCustomer(){
selectedCustomer = select.options[select.selectedIndex].text;
}
</script>
</body>
</html>
我的数据库类:
<?php
/**
/**
This is my Database manager class for the customers CRUD.
@author Francis Charpentier
@copyright 2014 Herzing Colllege
@version 1.0
**/
/**
*Constructor
*@param none
*@return void
**/
class DB_manager{
private $_theDB;
public function __construct(){
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "";
$dbName = "herzing";
try{
$this->_theDB = new PDO("mysql:host=".$dbHost.";dbname=".$dbName, $dbUser, $dbPass);
$this->_theDB->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
} catch(Exception $e){
die("Error: " . $e->getMessage());
}
}
public function addCustomer($obj){
$query = $this->_theDB->prepare("INSERT INTO customers (customerNumber, customerName, contactLastName, contactFirstName, phone, addressLine1, addressLine2, city, state, postalCode, country, salesRepEmployeeNumber, creditLimit, membership, newClient) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
$insertedID = 0;
try{
$this->_theDB->beginTransaction();
$query->execute(array(
$obj->getCustomerNumber(),
$obj->getCustomerName(),
$obj->getContactLastName(),
$obj->getContactFirstName(),
$obj->getPhone(),
$obj->getAddressLine1(),
$obj->getAddressLine2(),
$obj->getCity(),
$obj->getState(),
$obj->getPostalCode(),
$obj->getCountry(),
$obj->getSalesRepEmployeeNumber(),
$obj->getCreditLimit(),
$obj->getMembership(),
$obj->getNewClient()
));
$insertedID = $this->_theDB->lastInsertId();
$this->_theDB->commit();
} catch(PDOException $e){
$this->_theDB->rollback();
}
return $insertedID;
}
public function getAllCustomersName(){
$query = $this->_theDB->query("SELECT * FROM customers");
$_SESSION["data"] = $query->fetchAll();
return $_SESSION["data"];
}
}
?>
Customer类只包含一个基本构造函数和getter和setter。 添加客户工作得很好,我无法从选择输入中获取信息。
提前致谢!