1

So there's a lot of people helping to create a drop down list in html and populating it with their database. I'm trying to do this and I found some php code to do it but it just doesn't work. I understand php, sql and html but just not how they combine together. What seems to be the problem is that after the first echo, the rest of the code is just outputted as code to the page and it doesn't do anything. This is the code:

    <html>
    <body>
        <?php
        mysql_connect('localhost', 'root', 'password');
        mysql_select_db('FoodMatching');

        $sql = "SELECT IngID, IngName FROM Ingredient Characteristics";
        $result = mysql_query($sql);

        echo "<select name='Ingredient Name'>";
        while ($row = mysql_fetch_array($result)) {
            echo "<option value='" . $row['IngName'] ."'>" . $row['IngName'] ."</option>";
}
echo "</select>";
?>
    </body>
</html>

And what I see on the webpage is:

"; while ($row = mysql_fetch_array($result)) { echo "
" . $row['IngName'] ."
"; } echo ""; ?>

There's no errors/warnings that pop up so I don't know what the problem is. Thank you if you can help :)

pmpman
  • 13
  • 3
  • 1
    Don't use the deprecated `mysql_*`-functions. They are deprecated since PHP 5.5 and completely removed in PHP 7. They are also insecure. Use MySQLi or PDO instead. – M. Eriksson Nov 12 '16 at 10:29
  • I'm sure there's something wrong with your sql specifically your table: Ingredient Characteristics... Try running that on your mysql and see... – barudo Nov 12 '16 at 10:32
  • Don't have spaces in table names, form item names and stuff. – M. Eriksson Nov 12 '16 at 10:32
  • I probably sound pretty stupid but I'm new to this. So I'm using phpmyadmin for the database, I removed any spacing from the table names and attributes. I'm thinking maybe it just works differently cause it's phpmyadmin? And I changed the mysql to mysqli – pmpman Nov 12 '16 at 11:17

1 Answers1

0

As mentioned you should look to use PDO's to talk to the DB.

If you get the list before the html is output then you can have much cleaner and easier to understand code

See if the below makes sense, you might need to make a few modifications as its untested.

There are some comments about your mySql, ensure that results are being returned when you run the query.

<?php

define( "DB_DSN", "mysql:host=localhost;dbname=foo");
define( "DB_USERNAME", "root");
define( "DB_PASSWORD", "password" ); 

// define the empty array to be filled from db
$aIngredeintCats = array();


// any other php tasks that dont needthe ingcats


// store sql
$sSQL = "SELECT IngID, IngName FROM IngredientCharacterisitics";

// create an instance of the connection
$conn   = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );

// prepare
$st   = $conn->prepare( $sSQL );

// if required securely bind any user input in the query
// $st->bindValue(":IngID", $sIngName, PDO::PARAM_STR);

// execute the connection
$st->execute();

/* this will show if a result has been returned from the db.
echo "<pre>";
var_dump($st->fetch());
echo "</pre>";
*/

// while myslq has rows loop over them and store
while($row = $st->fetch() ){

    // use the IngID from db as the array key
    // also strip any tags from the output. other sanatisation should be done
    $aIngredeintCats[$row['IngID']] = strip_tags($row['IngName']);
}


// any other php tasks if they need the list of cats

?>


<html>
    <body>
        <form method='post' action='/'>
        <?php 
            // if there are results stored create the select and loop over
            if(!empty($aIngredeintCats)){
                echo "<select name='IngredientName'>";
                echo "<option value='' default>default</option>"
                foreach ($aIngredeintCats as $iIngID => $sIngName) {
                    echo "<option value='".$sIngName."' >".$sIngName."</option>";
                }
                echo "</select>";
            }else{
                echo "<p>No results avaliable!</p>";
            }
        ?>

        </form>
    </body>
</html>
atoms
  • 2,993
  • 2
  • 22
  • 43
  • it looks like it should work but the same kind of thing keeps happening. After the "$st = $conn->" just below the //prepare line. The rest of the code just gets printed to the page as text. I'm writing this in c9.io if that makes a difference. I inspected element and after $conn-> the rest of the code just appears between the tags. – pmpman Nov 12 '16 at 13:59
  • Try to create the file in a text editor and ftp/fssh to the server. Might be the ide? – atoms Nov 12 '16 at 14:07
  • Wait. I think i've been really stupid. Should it be a .php file instead of .html – pmpman Nov 12 '16 at 14:19
  • Yeah that would help lol – atoms Nov 12 '16 at 14:20
  • yeah it all works now thank you very much – pmpman Nov 12 '16 at 14:26