$options
попадает $_POST
public static function saveStaff($options)
{
$db = Db::getConnection();
// Проверка есть ли хотя бы одно поле
if (isset($options['director_surname']) || isset($options['director_name'])
|| isset($options['director_ot4']) || isset($options['director_oklad'])
|| isset($options['director_date_start']))
{
$sql = 'UPDATE staff SET ';
if (isset($options['director_surname'])) {
$sql .= 'surname = :surname,';
}
if (isset($options['director_name'])) {
$sql .= 'name = :name,';
}
if (isset($options['director_ot4'])) {
$sql .= 'ot4 = :ot4,';
}
if (isset($options['director_oklad'])) {
$sql .= 'oklad = :oklad,';
}
if (isset($options['director_date_start'])) {
$sql .= 'date_start = :date_start,';
}
// Удаляем последнюю запятую в запросе
$sql = substr($sql,0,-1);
$sql .= ' WHERE id = 1;';
$result = $db->prepare($sql);
if (isset($options['director_surname'])) {
$result->bindParam(':surname', $options['director_surname'], PDO::PARAM_STR);
}
if (isset($options['director_name'])) {
$result->bindParam(':name', $options['director_name'], PDO::PARAM_STR);
}
if (isset($options['director_ot4'])) {
$result->bindParam(':ot4', $options['director_ot4'], PDO::PARAM_STR);
}
if (isset($options['director_oklad'])) {
$result->bindParam(':oklad', $options['director_oklad'], PDO::PARAM_STR);
}
if (isset($options['director_date_start'])) {
$result->bindParam(':date_start', $options['director_date_start'], PDO::PARAM_STR);
}
$result->execute();
}
}
public static function saveStaff($options)
{
if (self::isValidStaff($options)) {
$staffFields = self::getFilledStaffFileds($options);
$qb = new QueryBuilder( Db::getConnection() );
$qb->where('id = 1')->update('staff', $staffFields);
}
}
public static function getFilledStaffFileds($options)
{
$staff = [];
if ($options['director_surname']) {
$staff['surname'] = $options['director_surname'];
}
if ($options['director_surname']) {
$staff['name'] = $options['director_name'];
}
if ($options['director_surname']) {
$staff['ot4'] = $options['director_ot4'];
}
if ($options['director_surname']) {
$staff['oklad'] = $options['director_oklad'];
}
if ($options['director_surname']) {
$staff['date_start'] = $options['director_date_start'];
}
return $staff;
}
function static function isValidStaff($options)
{
return isset($options['surname'])
|| isset($options['name'])
|| isset($options['ot4'])
|| isset($options['oklad'])
|| isset($options['date_start']);
}
class QueryBuilder {
protected $db;
protected $where = '';
public function __construct($db) {
$this->db = $db;
}
public function where($where)
{
$this->where = trim($where);
return $this;
}
public function update($table, $params)
{
$values = [];
foreach ($params as $key => $value) {
$values[] = "`{$key}` = :{$key}";
}
$sql = 'UPDATE `' . $table . '` SET ' . join(', ', $values);
if ($this->where) {
$sql .= ' WHERE ' . $this->where;
}
$result = $this->db->prepare($sql);
foreach ($params as $key => $value) {
$result->bindParam(':' . $key, $value, PDO::PARAM_STR);
}
return $result->execute();
}
}