Hallo zusammen,
ich stehe vor einem etwas komischen Problem.
Ich habe hier eine Kundendatenbank in der ich Daten eintrage (was auch funktioniert).
Allerdings sobald ich diese ändern will gibt mir das System einen Fehler aus.
Hier mal die wichtigsten Codes
Das ist die Customer.php die die Anweisungen gibt.
hier die class.customer.php die alle SQL anweisungen macht!
Im Prinzip geht es um den Firmennamen, in die SQL Datenbank wir er eingetragen, aber ändern lässt er sich nicht mehr über das Frontend.
Seht ihr evtl. den Fehler?!
ich stehe vor einem etwas komischen Problem.
Ich habe hier eine Kundendatenbank in der ich Daten eintrage (was auch funktioniert).
Allerdings sobald ich diese ändern will gibt mir das System einen Fehler aus.
Hier mal die wichtigsten Codes
Das ist die Customer.php die die Anweisungen gibt.
PHP-Code:
<?php
require_once('config.php');
if($core->is_logged_in()) {
require_once('lib/class.customers.php');
$customers = new customers($conn, $core, $errorHandler);
$do = (isset($_GET['do']) ? $_GET['do'] : 'overview');
$tpl->assign('do', $do);
if($do == 'overview') {
$tpl->assign('customers', $customers->load_all_customers());
} elseif($do == 'view' && isset($_GET['customer_id'])) {
$tpl->assign('customer', $customers->load_single_customer($_GET['customer_id']));
} elseif($do == 'new') {
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$customers->add($_POST['firmname'], $_POST['name'], $_POST['surname'], $_POST['email'], $_POST['address'], $_POST['zipcode'], $_POST['city'], $_POST['phone']);
if(!sizeof($errorHandler->get())) {
$tpl->assign('success', 'Customer added.');
} else {
$tpl->assign('errors', $errorHandler->get());
$errorHandler->reset();
$tpl->assign('post', $_POST);
}
}
} elseif($do == 'edit' && isset($_GET['customer_id'])) {
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$customers->edit($_GET['customer_id'], $_POST['firmname'], $_POST['name'], $_POST['surname'], $_POST['email'], $_POST['address'], $_POST['zipcode'], $_POST['city'], $_POST['phone']);
if(!sizeof($errorHandler->get())) {
$tpl->assign('success', 'Customer edited.');
} else {
$tpl->assign('errors', $errorHandler->get());
$errorHandler->reset();
}
}
$tpl->assign('current', $customers->load_single_customer($_GET['customer_id']));
} elseif($do == 'delete' && isset($_GET['customer_id'])) {
$customers->delete($_GET['customer_id']);
if(!sizeof($errorHandler->get())) {
header('Location: customers.php');
exit();
} else {
$tpl->assign('errors', $errorHandler->get());
$errorHandler->reset();
}
}
$tpl->display('_head.tpl');
$tpl->display('_header.tpl');
$tpl->display('customers.tpl');
$tpl->display('_footer.tpl');
} else {
header('Location: login.php');
exit();
}
?>
PHP-Code:
<?php
class customers {
private $conn;
private $core;
private $error;
private $id;
private $firmname;
private $name;
private $surname;
private $email;
private $address;
private $zipcode;
private $city;
private $phone;
function __construct($conn, $core, $errorHandler) {
$this->conn = $conn;
$this->core = $core;
$this->error = $errorHandler;
$this->id = '';
$this->firmname = '';
$this->name = '';
$this->surname = '';
$this->email = '';
$this->address = '';
$this->zipcode = '';
$this->city = '';
$this->phone = '';
}
public function add($firmname, $name, $surname, $email, $address, $zipcode, $city, $phone) {
$this->setFirmname($firmname);
$this->setName($name);
$this->setSurname($surname);
$this->setEmail($email);
$this->setAddress($address);
$this->setZipcode($zipcode);
$this->setCity($city);
$this->setPhone($phone);
if(!sizeof($this->error->get())) {
$res = $this->conn->query("INSERT INTO customers (firmname, name, surname, email, address, zipcode, city, phone, date_added) VALUES ('".$this->firmname."', '".$this->name."', '".$this->surname."', '".$this->email."', '".$this->address."', '".$this->zipcode."', '".$this->city."', '".$this->phone."', ".time().")");
if(!$res) {
$this->error->set('Couldn\'t insert customer into database.');
}
}
}
public function edit($id, $firmname, $name, $surname, $email, $address, $zipcode, $city, $phone) {
$this->setId($id);
$this->setFirmname($firmname);
$this->setName($name);
$this->setSurname($surname);
$this->setEmail($email);
$this->setAddress($address);
$this->setZipcode($zipcode);
$this->setCity($city);
$this->setPhone($phone);
if(!sizeof($this->error->get())) {
$res = $this->conn->query("UPDATE customers SET firmname = '".$this->firmname."', name = '".$this->name."', surname = '".$this->surname."', email = '".$this->email."', address = '".$this->address."', zipcode = '".$this->zipcode."', city = '".$this->city."', phone = '".$this->phone."' WHERE id = ".$this->id);
if(!$res) {
$this->error->set('Couldn\'t update customer (database failure).');
}
}
}
public function delete($id) {
$this->setId($id);
if(!sizeof($this->error->get())) {
$res = $this->conn->query("DELETE FROM customers WHERE id = ".$this->id);
if(!$res) {
$this->error->set('Couldn\'t delete customer (database failure).');
}
}
}
public function load_all_customers() {
$q = $this->conn->query("SELECT * FROM customers ORDER BY id DESC");
if($this->conn->num_rows($q)) {
while($assoc = $this->conn->fetch_assoc($q)) {
$assoc['formatted_id'] = sprintf('%06d', $assoc['id']);
$items[] = $assoc;
}
return $items;
}
}
public function load_single_customer($customer_id) {
$this->setId($customer_id);
if(!sizeof($this->error->get())) {
$q = $this->conn->query("SELECT * FROM customers WHERE id = ".$this->id." LIMIT 1");
if($this->conn->num_rows($q)) {
$assoc = $this->conn->fetch_assoc($q);
$assoc['date_added'] = $this->core->format_date($assoc['date_added']);
$assoc['formatted_id'] = sprintf('%06d', $assoc['id']);
return $assoc;
}
}
}
private function setId($id) {
if(ctype_digit($id)) {
$this->id = $id;
} else {
$this->error->set('Invalid id.');
}
}
private function setFirmname($firmname) {
$firmname = $this->core->escape($firmname);
if(preg_match('/^[a-z0-9-\s]+$/i', $firmname) AND $firmname != '' AND strlen($firmname) <= 90) {
$this->firmname = $firmname;
} else {
$this->error->set('Invalid Firmname.');
}
}
private function setName($name) {
$name = $this->core->escape($name);
if(preg_match('/^[a-z0-9-\s]+$/i', $name) AND $name != '' AND strlen($name) <= 30) {
$this->name = $name;
} else {
$this->error->set('Invalid name.');
}
}
private function setSurname($surname) {
$surname = $this->core->escape($surname);
if(preg_match('/^[a-z0-9-\s]+$/i', $surname) AND $surname != '' AND strlen($surname) <= 50) {
$this->surname = $surname;
} else {
$this->error->set('Invalid surname.');
}
}
private function setEmail($email) {
$email = $this->core->escape($email);
if(filter_var($email, FILTER_VALIDATE_EMAIL) AND $email != '' AND strlen($email) <= 255) {
$this->email = $email;
} else {
$this->error->set('Invalid e-mail.');
}
}
private function setAddress($address) {
$address = $this->core->escape($address);
if((preg_match('/^[a-z0-9-\s]+$/i', $address) AND strlen($address) <= 90) OR $address == '') {
$this->address = $address;
} else {
$this->error->set('Invalid address.');
}
}
private function setZipcode($zipcode) {
$zipcode = $this->core->escape($zipcode);
if((preg_match('/^[a-z0-9-\s]+$/i', $zipcode) AND strlen($zipcode) <= 10) OR $zipcode == '') {
$this->zipcode = $zipcode;
} else {
$this->error->set('Invalid zipcode.');
}
}
private function setCity($city) {
$city = $this->core->escape($city);
if((preg_match('/^[a-z-\s]+$/i', $city) AND strlen($city) <= 30) OR $city == '') {
$this->city = $city;
} else {
$this->error->set('Invalid city.');
}
}
private function setPhone($phone) {
$phone = $this->core->escape($phone);
if((preg_match('/^[0-9+-\s]+$/i', $phone) AND strlen($phone) <= 25) OR $phone == '') {
$this->phone = $phone;
} else {
$this->error->set('Invalid phone.');
}
}
}
?>
Seht ihr evtl. den Fehler?!
Kommentar