我正在尝试php中的例子(刚开始学习)
employee.php:
<?php
class Employee {
private $id;
private $name;
private $age;
private $address;
private $tax;
private $salary;
public function __construct($name, $age, $address, $tax, $salary) {
$this->name = $name;
$this->age = $age;
$this->address = $address;
$this->tax = $tax;
$this->salary = $salary;
}
public function __get($param) {
return $this->$param;
}
public function calculateSalary()
{
return $this->salary - ($this->salary * $this->tax / 100);
}
}
索引:
<?php
require_once 'db.php';
require_once 'employee.php';
if (isset($_POST['submit']))
{
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
$address = filter_input(INPUT_POST, 'address', FILTER_SANITIZE_STRING);
$age = filter_input(INPUT_POST, 'age', FILTER_SANITIZE_NUMBER_INT);
$salary = filter_input(INPUT_POST, 'salary', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
$tax = filter_input(INPUT_POST, 'tax', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
$params = array( ':name' => $name,
':address' => $address,
':age' => $age,
':salary' => $salary,
':tax' => $tax);
if(isset($_GET['action']) && $_GET['action'] == 'edit' && isset($_GET['id'])){
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
$sql = 'UPDATE employees SET name = :name ,address = :address , age = :age , salary = :salary, tax =:tax WHERE id = :id';
$params[':id'] = $id;
} else {
$sql = 'INSERT INTO employees SET name = :name ,address = :address , age = :age , salary = :salary, tax =:tax ';
}
$stmt = $connection->prepare($sql);
if($stmt->execute($params) === true
)
{
$message = 'Employee ' . $name . ' saved successfully';
header('Location: /advancedphp');
exit;
} else {
$error = true;
$message = 'Error saving employee ' . $name ;
}
}
if (isset($_GET['action']) && $_GET['action'] == 'edit' && isset($_GET['id'])) {
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
if ($id > 0) {
$sql = 'SELECT * FROM employees WHERE id = :id';
$result = $connection->prepare($sql);
$founduser = $result->execute(array(':id' => $id));
if($founduser === true){
$user = $result->fetchall(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Employee', array('name', 'age', 'address', 'tax', 'salary'));
$user = array_shift($user);
}
}
}
//Reading from database back
$sql = 'SELECT * FROM employees';
$stmt = $connection->query($sql);
$result = $stmt->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Employee', array('name', 'age', 'address', 'tax', 'salary'));
?>
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="css/font-awesome.css">
<title>PDO by example</title>
</head>
<body>
<div class="wrapper">
<div class="empform">
<form class="appform" method="post" enctype="application/x-www-form-urlencoded">
<fieldset>
<legend>Employee Information</legend>
<?php if(isset($message)) { ?>
<p class="message <?= isset($error) ? 'error' : ' ' ?>"><?= $message ?></p>
<?php } ?>
<table>
<tr>
<td>
<label for="name">Employee Name</label>
</td>
</tr>
<tr>
<td>
<input required type="text" name="name" id="name" placeholder="Write the employee name " maxlength="50" value="<?= isset($user) ? $user->name : ' ' ?>">
</td>
</tr>
<tr>
<td>
<label for="age">Employee Age</label>
</td>
</tr>
<tr>
<td>
<input required type="number" name="age" id="age" min="22" max="60" value="<?= isset($user) ? $user->age : ' ' ?>">
</td>
</tr>
<tr>
<td>
<label for="address">Employee Address</label>
</td>
</tr>
<tr>
<td>
<input required type="text" name="address" id="address" placeholder="Write the employee address " maxlength="100" value="<?= isset($user) ? $user->address : ' ' ?>">
</td>
</tr>
<tr>
<td>
<label for="salary">Employee Salary</label>
</td>
</tr>
<tr>
<td>
/*line 105*/ <input required type="number" step="0.01" name="salary" id="salary" min="1500" max="9000" value="<?= isset($user) ? $user->salary : ' ' ?>">
</td>
</tr>
<tr>
<td>
<label for="tax">Employee Tax (%)</label>
</td>
</tr>
<tr>
<td>
<input required type="number" step="0.01" name="tax" id="tax" min="1" max="5" value="<?= isset($user) ? $user->tax : ' ' ?>">
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="Save">
</td>
</tr>
</table>
</fieldset>
</form>
</div>
<div class="employees">
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Address</th>
<th>Salary</th>
<th>Tax (%)</th>
<th>Control</th>
</tr>
</thead>
<tbody>
<?php
if(false !== $result){
foreach ($result as $employee) {
?>
<tr>
<td><?= $employee->name ?></td>
<td><?= $employee->age?></td>
<td><?= $employee->address ?></td>
<td><?= round($employee->calculateSalary()) ?> L.E</td>
<td><?= $employee->tax ?></td>
<td>
<a href="/advancedphp/?action=edit&id=<?= $employee->id ?>"><i class="fa fa-edit"></i></a>
<a href="/advancedphp/?action=delete&id=<?= $employee->id ?>"><i class="fa fa-times"></i></a>
</td>
</tr>
<?php
}
} else {
?>
<td colspan="5"><p>Sorry, no employees to list</p></td>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</body>
</html>
该项目运作良好..但如果我编辑字段..它更新了名称和地址字段中的错误
"<br /><b>Notice</b>: Trying to get property of non-object in <b>C:\xampp\htdocs\PhpProject\advancedphp\index.php</b> on line <b>105</b><br />"
如图所示:
我注意到这个错误发生在Windows上,但它在Linux上运行并且运行没有错误 我不知道哪里出错......
(抱歉,我是先生)
答案 0 :(得分:0)
我没有看到你在哪里实例化你的班级Employee
$user
对isset()
的引用由$employee
检查,但您对$user = new Employee(stuff,here,as,required,by,constructor);
的引用不是。你必须在某处实例化该类,即
$employee = new Employee(stuff,here,as,required,by,constructor);
或
{{1}}