0

I'm just learning PHP and MySQL, so I'm guessing my error is really obvious.

I have a DB Table called inventory and I'm trying to pull up information on the car named Mustang. That is the name of the car under the Make column.

The problem I'm having is that my first echo statement is not ending with what should be the closing " and the following ;. It is echo'ing everything that follows it in the code, down to the ?> at the end of the file.

Here is the code itself. Note this is just a database I threw together in 10 minutes in phpmyadmin and not anything official.

<!DOCTYPE html>
<html>
    <head>
        <title>Realistic Autos Inventory</title>
        <script>
            <?php
                function GetInventory($CarName)
                {
                    $user="Uhrmacher";
                    $host="localhost";
                    $password="";
                    $dbname="realistic autos";
                    $cxn= mysqli_connect($host, $user, $password, $dbname) or die("Failure to Communicate!");
                    $query = "SELECT * FROM inventory WHERE Make='$CarName'";
                    $result = mysqli_query($cxn, $query) or die ("Failure to Query!");
                    $index=1;
                    while($row=mysqli_fetch_query($result))
                    {
                        foreach($row as $colname => $value)
                        {
                            $array_multi[$index][$colname]=$value;
                        }
                        $index++;
                    }
                    return $array_multi;
                }
            ?>
        </script>
    </head>
    <body>
        <?php
            $CarName = "Mustang";
            $CarInfo = GetInventory($CarName);
            echo "<h1>{$type}s</h1>\n";
            echo "<table cellspacing='15'>\n";
            echo "<tr><td colspan='4'><hr /></td></tr>\n";
            for ($i=1; $i<=sizeof($CarInfo); $i++)
            {
                $f_price = number_format($CarInfo[$i]['Price'], 2);
                echo "<tr>\n
                    <td>$i.</td>\n
                    <td>{$CarInfo[$i]['StockNumber']}</td>\n
                    <td>{$CarInfo[$i]['Year']}</td>\n
                    <td>{$CarInfo[$i]['Make']}</td>\n
                    <td>{$CarInfo[$i]['Model']}</td>\n
                    <td>{$CarInfo[$i]['Package']}</td>\n
                    <td style='text-align: right'>\$$f_price</td>\n
                    <td>{$CarInfo[$i]['CurrentMiles']}</td>\n
                    <td>{$CarInfo[$i]['Engine']}</td>\n
                    <td>{$CarInfo[$i]['Transmission']}</td>\n
                    <td>{$CarInfo[$i]['DriveType']}</td>\n
                    <td>{$CarInfo[$i]['VIN']}</td>\n
                    <td>{$CarInfo[$i]['BoughtFrom']}</td>\n
                    <td>{$CarInfo[$i]['BoughtHow']}</td>\n
                    <td>{$CarInfo[$i]['LicensingFee']}</td>\n
                    <td>{$CarInfo[$i]['GasMileage']}</td>\n
                    </tr>\n";
                echo "<tr><td colspan='4'><hr /></td></tr>\n";
            }
            echo "</table>\n";
        ?>
    </body>
</html>

The following is the output.

\n"; echo "
\n"; for ($i=1; $i<=sizeof($CarInfo); $i++) { $f_price = number_format($CarInfo[$i]['Price'], 2); echo "\n $i.\n {$CarInfo[$i]['StockNumber']}\n {$CarInfo[$i]['Year']}\n {$CarInfo[$i]['Make']}\n {$CarInfo[$i]['Model']}\n {$CarInfo[$i]['Package']}\n \$$f_price\n {$CarInfo[$i]['CurrentMiles']}\n {$CarInfo[$i]['Engine']}\n {$CarInfo[$i]['Transmission']}\n {$CarInfo[$i]['DriveType']}\n {$CarInfo[$i]['VIN']}\n {$CarInfo[$i]['BoughtFrom']}\n {$CarInfo[$i]['BoughtHow']}\n {$CarInfo[$i]['LicensingFee']}\n {$CarInfo[$i]['GasMileage']}\n \n"; echo "
\n"; } echo "\n"; ?>

I wasn't sure how to search this, so sorry if this is a common problem.

3
  • Why are there newlines (\n) at the end of the echo statements? Commented Jul 31, 2014 at 22:15
  • It seems like PHP is not executed at all. Commented Jul 31, 2014 at 22:18
  • Get rid of those new line characters and see if it still messes up. Commented Jul 31, 2014 at 22:52

1 Answer 1

1

Get that php out of those script tags! Put it all into one nice php block. Ideally you'd do something like have a "connect.php" page just for setting up your db connection then do an but it'll work on your page you have now.

Get rid of all those "\n"s. Those arnt helping anything. You dont need to have lines in your html, your browser will understand. (i'm assuming thats why you put them in). You had a bunch of crazy stuff going on when you created your table. Maybe you need it so its formatted pretty. I took it out. Lets get basic functionality working first. Table structure goes as follows: Table-head-row definition-closingTableTag.

Edit: Also i dont know why you had characters before and but make sure those are gone. And make sure you're saving your file as .php and not .html.

Try this, get back to us.

 <!DOCTYPE html>
<html>
    <head>
        <title>Realistic Autos Inventory</title>
    </head>
    <body>
        <?php

            $user="Uhrmacher";
            $host="localhost";
            $password="";
            $dbname="realistic autos";
            $cxn= mysqli_connect($host, $user, $password, $dbname) or die("Failure to Communicate!");

            function GetInventory($CarName){
                $query = "SELECT * FROM inventory WHERE Make='$CarName'";
                    $result = mysqli_query($cxn, $query) or die ("Failure to Query!");
                    $index=1;
                    while($row=mysqli_fetch_query($result))
                    {
                        foreach($row as $colname => $value)
                        {
                            $array_multi[$index][$colname]=$value;
                        }
                        $index++;
                    }
                    return $array_multi;
            }           
            $CarName = "Mustang";
            $CarInfo = GetInventory($CarName);
            echo "<h1>{$type}s</h1>";
            echo "<table>";
           //table header for every column you have. You could write a for loop to simplify
            echo "<tr><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th></tr>";
            for ($i=1; $i<=sizeof($CarInfo); $i++)
            {
                $f_price = number_format($CarInfo[$i]['Price'], 2);
                echo "<tr>
                    <td>$i.</td>
                    <td>{$CarInfo[$i]['StockNumber']}</td>
                    <td>{$CarInfo[$i]['Year']}</td>
                    <td>{$CarInfo[$i]['Make']}</td>
                    <td>{$CarInfo[$i]['Model']}</td>
                    <td>{$CarInfo[$i]['Package']}</td>
                    <td>$f_price</td> //DONT GET FANCY YET, JUST MAKE SURE IT WORKS 
                    <td>{$CarInfo[$i]['CurrentMiles']}</td>
                    <td>{$CarInfo[$i]['Engine']}</td>
                    <td>{$CarInfo[$i]['Transmission']}</td>
                    <td>{$CarInfo[$i]['DriveType']}</td>
                    <td>{$CarInfo[$i]['VIN']}</td>
                    <td>{$CarInfo[$i]['BoughtFrom']}</td>
                    <td>{$CarInfo[$i]['BoughtHow']}</td>
                    <td>{$CarInfo[$i]['LicensingFee']}</td>
                    <td>{$CarInfo[$i]['GasMileage']}</td>
                    </tr>";
            }
            echo "</table>";
        ?>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.