I have 2 tables:
Table bs_steinweg
id | laptopid | anzahl | datum | ustd
Table bs_laptops
id | laptop | anzahl
If users want to reserve a laptop, I want to query whether there are still laptops available.
$readanzahl = $fromform->laptop;
comes from the form field just like
$ustd = $fromform->time;
and
$newdate = date( 'Y-m-d', $fromform->date );
Now I want to connect the two tables. In addition, there is the WHERE clause with 3 conditions.
LBTB.laptopid
must be the same as the one from the form field.
LBTB.datum
must also be the same value as the one from the form field.
LBTB.ustd
also comes from the form field.
If there is already a reservation, then I want to calculate how many laptops are still left.
In the MySQL query, I try to subtract the value LAPTOP.anzahl minus the number of laptops that have already been reserved.
If the number is less than = 0, I want to output a message that no laptops are available.
Unfortunately, I don’t get an error message, so I can’t go any further and I’m hoping for your help.
Thank you very much!
$newdate = date( 'Y-m-d', $fromform->date );
$readanzahl = $fromform->laptop;
$ustd = $fromform->time;
$laptopsanzahl = $DB->get_records_sql( 'SELECT LBTB.datum, LBTB.laptopid, LBTB.ustd, LAPTOP.id, LAPTOP.laptop, LAPTOP.anzahl - LBTB.anzahl AS differenz FROM {bs_steinweg} LBTB
LEFT JOIN {bs_laptops} LAPTOP
ON LBTB.laptopid = LAPTOP.id
WHERE LBTB.laptopid = @$readanzahl
AND LBTB.datum = @newdate
AND LBTB.ustd = @ustd
' );
if ( $laptopsanzahl <= 0 ) {
var_dump($laptopsanzahl);
}