I have some drop down boxes that store counties and constituencies in them.
When you select an item from the drop down and click a search button, the value that is returned is the primary key id of the selected county or constituency.
<option value="<?php htmlout($county['ids']); ?>">
<option value="<?php htmlout($constituency['ids']); ?>">
These values are successfully returned as I can output them using:
$countyid = $_GET['county'];
$constituencyid = $_GET['constituency'];
<?php echo $countyid ?>
<?php echo $constituencyid ?>
I then have an sql statement as follows:
$sqlparams = array();
if($countyid !== '') {
$sqlparams[] = "county = '$countyid'";
}
if($constituencyid !== '') {
$sqlparams[] = "constituency = '$constituencyid'";
}
$sql = "SELECT * FROM pt";
if (count($sqlparams) > 0) {
$sql .= " WHERE " . implode(" AND ", $sqlparams);
}
This is then executed and the results stored in an array and all of this works.
However, the problem is that for instance with the following example:
if($countyid !== '') {
$sqlparams[] = "county = '$countyid'";
}
I am comparing ‘county’ a VARCHAR column with ‘$countyid’ an integer.
I want to know the best way to get the name of the county in place of that $countyid variable if that makes sense so that I am comparing a VARCHAR with a VARCHAR.
I apologise if this is very simple but I am confused as to how to do this.
I understand that I could change the value of the dropdown to return the name but there is some stuff going on in the background that requires the id to be returned and I am pretty sure that it is good practice to return the id as this is very flexible.
Any help is appreciated.
Assuming a table Counties with columns name and countyid:
$sqlparams[] = "county IN (SELECT name FROM counties WHERE countyid = $countyid)"