Class script - 2 VSC errors

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;
}
}

Using php 8.1.12, the only error is on line 57, where you have not correctly formed a reference to $values[‘primaryKey’]. You also have a mistake a few lines later, using values instead of $values.

Perhaps relying on a Microsoft software tool, for a language they didn’t develop, is a waste of time?

I know where that code originally came from. You could have just copy/pasted from the book. Where did you get it?

Also, you magically have $result in the return of the findAll method.

The day before yesterday I changed server, and now PHP 8.1 is running on the server.

As you told me, I corrected the values/$values error on the line as you described as a few lines under $values[‘primaryKey’] line too. By the way, that line is line 56, and not 57 as you mentioned :slight_smile:
Anyway, I can’t find the error on line 56/57. Thank you for your input. It helped me a little further.

Sponsor our Newsletter | Privacy Policy | Terms of Service