doubo3384 2014-03-24 06:54
浏览 38
已采纳

根据使用PHP的选择显示MDB文件的结果

I hope someone can help me with this?

I have a Joomla installation running and the website looks and works great.

Here's the problem, the website is for a car dealership, which means they need to display a list of their stock on the floor.

They are using a custom system to manage their stock and this system saves the data to a MS Access database.

I got it to work to a point where I can display a table from the database. (http://www.autodeal.co.za/newsite/used-car-sales-south-africa).

Now when someone clicks on the model, which is a link that will take them to another page which displays only the information relevant to the selected model.

That's what I can't figure out. The link works fine and it takes me to the page that I want, but it doesn't display the data like it's supposed to.

Please see the code below for connecting to the database and displaying the results:

    <?php

        $dbName = "F:/Domains/autodeal/autodeal.co.za/wwwroot/newsite/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", '', '');

        // This is the query
        // You have to have each column you select in the format tableName.[ColumnName]
        $sql = "SELECT Make, Model, Year, Price, SpecialPrice, Branch, StockNO FROM Vehicle ORDER BY Make";

        // Runs the query above in the table
        $rs = odbc_exec($conn, $sql);

        echo "\t" . "<tr>
";

        echo "\t" . "<th>Make</th><th>Model</th><th>Year</th><th>Price</th><th>Special Price</th><th>Location</th><th>Stock Number</th>" . "
";

        while (odbc_fetch_row($rs))
        {
            $make = odbc_result($rs, Make);
            $model = odbc_result($rs, Model);
            $year = odbc_result($rs, Year);
            $price = odbc_result($rs, Price);
            $specialPrice = odbc_result($rs, SpecialPrice);
            $branch = odbc_result($rs, Branch);
            $stockNo = odbc_result($rs, StockNO);

            echo "\t" . "<tr>
";
            echo "\t\t" . "<td>" . $make . "</td><td><a href=http://www.autodeal.co.za/newsite/selected-vehicles>" . $model . "</a></td><td>" . $year . "</td><td>" . $price . "</td><td>" . $specialPrice . "</td><td>" . $branch . "</td><td>" . $stockNo . "</td>
";

            echo "\t" . "</tr>
";
        }

        odbc_free_result($rs);
        odbc_close($conn);

        // This message is displayed if the query has an error in it
        if (!$rs) {
                exit("There is an error in the SQL!");
        }

    ?>

Please see the code below to display a specific vehicle information from the table based on a selection made from the above script.

    <?php

        $dbName = "F:/Domains/autodeal/autodeal.co.za/wwwroot/newsite/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", '', '');

        // This is the query
        // You have to have each column you select in the format tableName.[ColumnName]


        $selected_id = intval($_GET['Id']);
        $sql = "SELECT Id, Make, Model, Year, Price, SpecialPrice, Branch, StockNO, MainPic FROM Vehicle WHERE Id = Id";

        // Runs the query above in the table
        $rs = odbc_exec($conn, $sql);

        $id = odbc_result($rs, Id);

        $make = odbc_result($rs, Make);

        $model = odbc_result($rs, Model);

        echo $make;

        echo $model;

        $image_path_main = "<img src=db/vehicleImages/" . $mainPic . "/>";


        echo "this is a test";

        odbc_free_result($rs);
        odbc_close($conn);

        // This message is displayed if the query has an error in it
        if (!$rs) {
                exit("There is an error in the SQL!");
        }

    ?>

EDIT So I've updated the above code based an answer received, but the individual records aren't displayed. I printed a test line and that works fine, so that tells me that there's an issue with the query? The thing is, the query works fine to display all the records in a table, but I need to display a single record when that record has been clicked.

Furthermore, the $mainPic variable above is referencing the image name from the database. The actual image isn't saved in the database; it's in a different location. I assume I need to create a variable with the actual path of the image and use the reference variables above to display the image, but it's not working.

So to recap, I need some help displaying all information from the database based on a selection.

For example: in the table, I select 323i. On a new page, I need to display all the information that's in the database about the 323i on a new page.

Is that doable and if so, could anyone please assist me in this matter.

Thank you very much in advance.

  • 写回答

1条回答 默认 最新

  • dongsha7215 2014-03-24 06:59
    关注

    You are not using given ID parameter in your query:

    $sql = "SELECT ... FROM Vehicle WHERE Id = Id ORDER BY Make";
    

    you need to get $ID from user and place it into the query like:

    $id = intval($_GET['id']); // assuming it is an integer
    $sql = "SELECT ... FROM Vehicle WHERE Id = $id; // no need to order
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败