Question

This seems like such a simple thing to do but for some reason, I can't get this to work.

I have a database with vehicle data stored in it. (Used Cars (I'm developing a car dealership website)).

I successfully display results from the database without images.

The images for each record aren't stored in the database, instead they're dumped on the server in a directory and those images are only referenced in the database.

If I echo the image name out it works fine, and if I echo the actual image out, the path is correct if you look at the image info. But in the info it states that the image is of text. i don't know how to change this.

Please find some of the code below.

    <?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 selecting individual records
        $selected_id = intval($_GET['Id']);

        //this is the query
        $sql = "SELECT Id, Make, Model, Year, Price, SpecialPrice, Branch, StockNO, MainPic FROM Vehicle WHERE Id = $selected_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);
        $mainPic = odbc_result($rs, MainPic);

        //this is a failsafe for when there are no images for a specific record
        $no_image = "<a href='db/Nopic.gif' data-lightbox='nopic' title='No Image Available'><img src='db/Nopic.gif' /></a>";

        //This successfully displays the name of the image referenced in the database
        $main_pic = "<img src='db/vehicleImages/'" .$mainPic. "/>";

        //this is supposed to display the actual
        echo $main_pic . "<br />";

        echo $no_image;


        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!");
        }

    ?>

Any help in the regard would be greatly appreciated.

Thank you. :)

Was it helpful?

Solution

you should use quotations around the attributes of html objects (it looks like you might be breaking things via your method):

yours:

<a href=db/Nopic.gif data-lightbox=nopic title=No Image Available><img src=db/Nopic.gif /></a>

correct:

<a href='db/Nopic.gif' data-lightbox='nopic' title='No Image Available'><img src='db/Nopic.gif' /></a>

whether or not this fixes your problem will be determined by what you come back with :p

OTHER TIPS

User forward slash in front of your image paths. This way you can be sure the image path always starts from the root folder. The image path is relative to the location of the executed php script.

For example if your script.php file is in the folder /root/some_folder/script.php and you use the image path db/vehicleImages/image.jpg the script is starting the path from the same folder where it is itself which results in a path like this /root/some_folder/db/vehicleImages/image.jpg.

If you use forward slash in front of the path like this /db/vehicleImages/image.jpg it tells the script to start from the root folder like so /root/db/vehicleImages/image.jpg.
I think this is the case with your script - you are giving it the wrong path which results in a file not found.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top