sql - Display a single record from a database with multiple duplicates PHP

200

I have an MS Access MDB database populated with vehicles: 'Models', 'Makes', 'year', etc.In the 'Make' column there are for instance five entries for ASTON MARTIN, each of these have a different ID and 'Model' linked to it, so I don't want to delete the duplicates.

You may have noticed that the database has a single table with all this data in. I wish I could change it but the client has an application which generates this database then saves it on the server via FTP, so I can't change the database.

I've gotten as far as populating drop downs with the Vehicle 'Make', but it loads all of the 'Makes'. How can I prevent it from loading duplicates? So in this 'Makes' drop down it loads five ASTON MARTIN cars, it loads four BMW 'Makes'. Keep in mind I can't delete anything. Also, when a selection has been made in the 'Makes' drop down, it's supposed to load the models for that selected 'Make' into a second, dependent drop down, which it does, but for some reason, it also dumps the 'Makes' into the 'Models' drop down.

Please see the below code to load the 'Makes' and 'Models' into cascading drop downs.

The AJAX Function:

<script type="text/javascript">

function loadXMLDoc() {
    var xmlhttp;
    if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.frm.modelSelection.innerHTML=xmlhttp.responseText;
            }
    }

    var makevalue=document.frm.makeSelection.value;

    xmlhttp.open("GET","?ajaxmake="+ makevalue,true);
    xmlhttp.send();
}

</script>

The PHP code:

<?php 

    $dbName = "C:/xampp/htdocs/new/db/savvyautoweb.mdb";

    // Throws an error if the database cannot be found
    if (!file_exists($dbName)) {
            die("Could not find database file.");
    }

    // Connects to the database
    // Assumes there is no username or password
    $conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName", '', '');

?>

<form action="" method="post" name="frm">
    <select name="makeSelection" onchange="loadXMLDoc()">

<?php
    //Loads the Makes from the database into a dropdown
    $resultMake = odbc_exec($conn, "SELECT DISTICT Make FROM Vehicle ORDER BY Make") or die (odbc_errormsg());
    while ($rowMake = odbc_fetch_array($resultMake)) {
        echo "<option value='$rowMake[Make]'>$rowMake[Make]</option>";
    }
?>
    </select>
    <select name="modelSelection">

<?php
    //loads the models based on the makes selection into a dependant dropdown
    if (isset($_REQUEST['ajaxmake'])) {
        $resultModel = odbc_exec($conn, "SELECT Model FROM Vehicle WHERE Make = '".$_REQUEST['ajaxmake']."'") or die (odbc_errormsg());

        while ($rowModel = odbc_fetch_array($resultModel)) {
            echo "<option value='$rowModel[Model]'>$rowModel[Model]</option>";
        }

        die();
    }
?>
    </select>
    <input type="submit" name="submit" value="Go">
</form>

Any help would be greatly appreciated.

Thank you.

46

Answer

Solution:

To get only the unique entries, useDISTINCT in your sql:

$resultMake = odbc_exec($conn, "SELECT DISTINCT Make FROM Vehicle ORDER BY Make") or die (odbc_errormsg());

People are also looking for solutions to the problem: php - Beginning wit PDO, first program doesn't work

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.