I've seen so many different ways of doing this all seem very complex and all seem to be done in MySQL. I'm using PDO in my project so I've had a go myself to see what I could come up with.
- Is this safe?
- Is there something I've missed a better way of doing it?
//build query to insert users_contracts
$sth = "INSERT INTO `users_contracts` (users_id,contracts_id) VALUES (:user,:contract)";
$q = $conn->prepare($sth);
//take each item in the contract array from _POST and bind to the above INSERT query
foreach($contract as $contract => $contract_id)
{
try {
$q->execute(array(':user'=>$last_id,':contract'=>$contract_id));
} catch (PDOException $e) {
$errors[] = array(
'message' => "Could not insert \"$contract\", \"$contract_id\".",
'error' => $e
);
}
}
Its intended purpose is to allow the admin user to assign contracts to users via a checkbox list.