I’m working on a billing system where the customer ID either needs to be sent to the next page if it is an existing customer and not been sent when it is a new customer in which case the newly added customer ID is applicable. I thought that the use of an Optional paramater would do the trick. This is what I have right now:
Model
public function klant_gegevens($klant_id = '')
{
$sql = "SELECT *
FROM `klant_gegevens`";
if ($klant_id)
{
$sql .= "WHERE `klant_id` = ?";
$stmt = $this->pdo->prepare($sql);
$stmt->execute(array($klant_id));
}
else
{
$sql .= "ORDER BY `klant_id` DESC
LIMIT 1";
$stmt = $this->pdo->query($sql);
}
return $stmt->fetch();
}
Controller
public function product_zoekenAction()
{
if (isset($klant_id))
{
$klant_id = filter_input(INPUT_POST, 'klant_id', FILTER_SANITIZE_NUMBER_INT);
}
else
{
$klant_id = '';
}
$klant_gegevens = $this->administratie->klant_gegevens($klant_id);
}
However, this works only partially. Whenever I add a new client to the system, on the next page where I can add products to the invoice, I get the right customer information. However, when I use an existing customer that I choose from a dropdown menu I always get the information from the customer with the highest id
ORDER BY `klant_id` DESC LIMIT 1
What am I doing wrong. Thank you in advange