Conditional MySQL Insert Logic

Job ID: 40569795

Budget: $10 – $30 USD

I have an existing web form that already writes to MySQL. Before any record is committed, I now need an extra safeguard: the insert must only proceed when the two text fields coming from the form are both present in a reference table.

Think of it as wrapping the current INSERT in a SELECT-EXISTS condition. Conceptually it looks like:

```
INSERT INTO target_table (col1, col2)
SELECT ?, ?
WHERE EXISTS (
SELECT 1
FROM reference_table
WHERE lookup_column = ?
);
```

Your task is to turn that idea into working code, wire it into the existing PHP/JavaScript form handler, and verify that no row is added unless the lookup succeeds. I will provide the current form files and database schema so you can drop in the updated.

Both values may be supplied by the user, by our internal script, or a mix of the two, so please sanitise inputs and use prepared statements to stay clear of injection issues.

Deliverable: updated source file(s) or a neatly documented SQL snippet plus short instructions on where to place it.