I’m struggling with a script. In VSC I get 2 errors that I don’t understand. According to the VSC, the errors are found on line 3 and line 70. Column 41 and 18
Can someone here at PHP Builder be helpful in telling me where and what the error is? I can’t find out what or where it is regardless of whether I have the line and column number. The script is pasted below.
<?php
class DatabaseTable {
public function __construct(private PDO $pdo, private string $table, private string $primaryKey) {
}
public function find($field, $value) {
$query = 'SELECT * FROM ' . $this->table . ' WHERE ' . $field . ' :value';
$values = [
'value' => $value
];
$stmt = $this->pdo->prepare($query);
$stmt->execute($values);
return $stmt->fetchAll();
}
public function findAll() {
$stmt = $this->pdo->prepare('SELECT * FROM ' . $this->table . '');
$stmt->execute();
return $result->fetchAll();
}
public function total() {
$stmt = $this->pdo->prepare('SELECT COUNT(*) FROM ' .$this->table .'');
$stmt->execute();
$row = $stmt->fetch();
return $row[0];
}
public function save ($record) {
try {
if ($record[$this->primaryKey]) {
unset($record[$this->primaryKey]);
}
$this->insert($record);
} catch (PDOException $e) {
$this->update($record);
}
}
private function update($values) {
$quer = ' UPDATE ' . $this->table .' SET ';
foreach ($values as $key => $value) {
$query .= '' . $key . ' = :' . $key . ',';
}
$query = rtrim($query, ',');
$query .= ' WHERE `' . $this->primaryKey . '` = :primaryKey';
//Set the primary key variable
$values = ['primaryKey'] = $values['id'];
$values = $this->processDates($values);
$stmt = $this->prepare($query);
$stmt->execute(values);
}
private function insert($values) {
$query = 'INSERT INTO ' . $this->table . ' (';
foreach ($values as $key => $value) {
$query .= '`' . $key . $key . '`,';
}
$query = rtrim($query, ',');
$query .= ') VALUES (';
foreach ($values as $key => $value) {
$query .= ':' . ',';
}
$query = rtrim($query, ',');
$query .= ')';
$values = $this->processDates($values);
$stmt = $this->pdo->prepare($query);
}
public function delete($field, $value) {
$values = [':value' => $value];
$stmt = $this->pdo->prepare('DELETE FROM `' . $this->table . '`WHERE `'. $field . '` = :value');
$stmt->execute($values);
}
private function processDates($values) {
foreach ($values as $key => $value) {
if ($value instanceof DateTime) {
$values[$key] = $value->format('Y-m-d');
}
}
return $values;
}
}