Forum Moderators: coopster
//Vehicle Details
$getmakesql = "SELECT manufacturer_id, manufacturer_name FROM manufacturer WHERE manufacturer_id = '".$make."'";
$getmakeqry = mysql_query($getmakesql) or die(mysql_error());
$getmakerow = mysql_fetch_row($getmakeqry);
$getmakeresult = $getmakerow[2];
$getmodelsql = "SELECT model_id, model_name FROM model WHERE model_id = '".$model."'";
$getmodelqry = mysql_query($getmodelsql);
$getmodelrow = mysql_fetch_row($getmodelqry);
while($getmodelresult = mysql_fetch_row($getmodelrow))
{
$getmodel = $getmodelresult[1];
}
Name: Test
Make: 1
Make:
Model: 3
Model:
$getmakesql = "SELECT manufacturer_id, manufacturer_name FROM manufacturer WHERE manufacturer_id =$make";
// **ONLY add mysql_error() for debugging - it reveals info about your database
// which could be of use to a potential hacker
$getmakeqry = mysql_query($getmakesql) or die("Cannot select make at line 1234");
if ($getmakerow = mysql_fetch_row($getmakeqry)) {
$getmakeresult = $getmakerow[1];
$getmodelsql = "SELECT model_id, model_name FROM model WHERE model_id=$model";
$getmodelqry = mysql_query($getmodelsql) or die("cannot get model at line 1238");
// Only if is required, should be only one result, right?
// if multiples, return to while
if($getmodelrow = mysql_fetch_row($getmodelqry)) {
$getmodel = $getmodelrow[1];
}
else { echo "<p>No results found for model.</p>"; }
}
else { echo "<p>No results found for make.</p>"; }
The only problem I've got now is if I select a make but not a model and the form is submitted I get "No results found for model."
$where = $jn = $result = null; // Squelches concatenation errors/warnings
$fields = array('mfg_id','name','model_id','model_name');
//
// When you do joins, you should (sometimes, and in this case must) specify the table
$select = "SELECT manufacturer.manufacturer_id as mfg_id, manufacturer.manufacturer_name as name";
if (is_numeric($make) and ($make > 0)) {
$where = " manufacturer.manufacturer_id=$make"; // NOTE SPACE
$from = " from manufacturer";
}
if (is_numeric($model) and ($model > 0)) {
// Only need AND if $where has been set
if ($where) { $where .= ' and'; }
$where .= " model.manufacturer_id=$model";
// The if's are for future potential expansion, see comment belo about YEAR
if (! $from) { $from .= ",model"; }
$select .= ", model.model_id as model_id, model.model_name as model_name";
if (! $jn) { $jn = " left join model on manufacturer.manufacturer_id=model.manufacturer_id"; }
}
// Put them all together
$getsql = "$select $from";
if ($where) { $getsql .= " where $where"; }
if ($jn) { $getsql .= $jn; }
//
// You'll need this - my code is typed on the fly and may have errors
// echo $getsql;
// exit;
//
$getqry = mysql_query($getsql) or die("Cannot select make and model");
while ($getmakerow = mysql_fetch_row($getmakeqry)) {
$result .= '<tr>';
foreach ($fields as $fld) {
// Left joins will return null results if no matching left value
// is found - which is the beauty of it
$val = (isset($getmakerow[$fld]))?$getmakerow[$fld]:'-';
// Might need this instead
// $val = (! empty($getmakerow[$fld]))?$getmakerow[$fld]:'-';
$result .= "<td>$val</td>";
}
$result .= '</tr>';
}
if ($result) {echo "<table> $result </table>"; }
else { echo "<p>No results found for make $make.</p>"; }
if (is_numeric($year) and preg_match('/^\d{4}$/',$year)) {
if ($where) { $where .= ' and'; }
$where .= " model.year=$year";
if (! $from) { $from .= ",model"; }
$select .= ", model.year as year";
if (! $jn) { $jn = " left join model on manufacturer.manufacturer_id=model.manufacturer_id"; }
}