So I am building a job website where the user selects several locations, and it should return the corresponding jobs. This means I will get an array of locations from the user. How would I use PDO to bind this array. At the moment I just add the value into the SQL directly however i know this is a security issue. How could I rewrite it so it uses syntax like:
$stmt->bindValue(":locations", $locations);
This is as far as I have got:
$location = $_POST['location'];
$locs = "'" . implode("', '", $location) . "'";
//create query
$sql =
"SELECT *
FROM job
JOIN location ON job.location_id = location.id
WHERE location.region IN ($locs)
";
//get results
try
{
$stmt = $DB->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll();
}
catch (Exception $ex)
{
echo $ex->getMessage();
}
//print results
foreach($results as $res)
{
echo "<p>Title:" . $res['title'];
echo "<p>Views:" . $res['views'];
echo "<p>Created At:" . $res['created_at'];
echo "<p>Image:" . $res['img_url'];
echo "<p>Salary:" . $res['salary'];
echo "<p>Region:" . $res['region'];
echo "<p>State:" . $res['state'];
echo "<br/><br/>";
}