I am trying to write a reusable UPDATE Query in PHP based on Jeffery's INSERT query on laracast
This is Jeff's Insert Query
public function insert($table, $parameters)
{
$sql = sprintf(
'INSERT INTO %s (%s) VALUES (%s)',
$table,
implode(', ', array_keys($parameters)),
':' . implode(', :', array_keys($parameters))
);
try {
$statement = $this->pdo->prepare($sql);
$statement->execute($parameters);
} catch (Exception $exception) {
die("Something Went Wrong");
}
}
This is the Update Code I am Trying to Write
public function update($table, $parameters, $Condition)
{
$sql = sprintf(
'UPDATE %s SET %s=%s WHERE ' . $Condition,
$table,
implode('=,', array_keys($parameters))
,
':' . implode(', :', array_keys($parameters))
);
try {
$statement = $this->pdo->prepare($sql);
$statement->execute($parameters);
} catch (Exception $exception) {
die("Something Went Wrong");
}
}
I want to make it as reusable as the Insert Query by just passing the Data in
All Help is Highly Appricated