我要在插入到另一个表之后在表上插入一条记录。这是我的示例代码:
if(isset($_POST['submit'])){
$database = new Database();
$db = $database->getConnection();
$employee = new Employee($db);
if(!empty($first_name) && !empty($family_name) && !empty($first_name_arabic) && !empty($family_name_arabic)){
$emp_number = isset($_POST['emp_number']) && !empty($_POST['emp_number']) ? $_POST['emp_number'] : $employee->generateEmployeeNumber();
$fields = [
'reference' => $reference,
'emp_number' => $emp_number,
'first_name' => $first_name,
'middle_name' => $middle_name,
'third_name' => $third_name,
'family_name' => $family_name,
'nationality' => $nationality,
];
$employee->createOrUpdate($fields, '');
$result = $employee->selectOne('Reference', $reference);
if(!empty($_POST['passport_number']) && !empty($_POST['passport_place_issue']) && !empty($_POST['passport_date_issue']) && !empty($_POST['passport_date_expiry']) && !empty($result['id'])) {
$passport = new Passport($db);
$passport_fields = [
'passport_number' => $_POST['passport_number'],
'place_issue' => $_POST['passport_place_issue'],
'date_issue' => date_format(date_create($_POST['passport_date_issue']), 'Y-m-d'),
'date_expiry' => date_format(date_create($_POST['passport_date_expiry']), 'Y-m-d'),
'reference' => $result['reference'] ,
'employee_id' => $result['id'],
'status' => 'Active',
];
$passport->createOrUpdate($passport_fields, '');
}
}
}
但是我的问题是我插入passport
表没有做任何事情,也没有错误返回给我。至于creatOrUpdate
,这就是我的做法:
function createOrUpdate($fields, $id){
if(is_null($id) || empty($id)){
$implodeColumns = implode(', ', array_keys($fields));
$implodePlaceholder = implode(", :", array_keys($fields));
$sql = "INSERT INTO " . $this->table_name. "($implodeColumns) VALUES(:".$implodePlaceholder.")";
echo "SQL ".$sql;
$stmt = $this->conn->prepare($sql);
foreach ($fields as $key => $value) {
$stmt->bindValue(":".$key,$value);
}
$stmt->execute();
}else{}
}
我不知道创建具有相同名称的方法是否会影响代码,但是员工的createOeUpdate
方法来自employee.php
,其中createOrUpdate
的护照方法来自{{ 1}}。只有员工被保存在我的数据库中。
非常感谢您的帮助
答案 0 :(得分:0)
如果不同文件中的名称相同,则需要使用以下命名空间:
namespace name_of_your_main_file;
// Maybe need to add full path
// if it's not in the same folder.
use employee;
use passport;
// And use it with same names as:
employee::createOrUpdate(