Gonçalo Loureiro Gonçalo Loureiro - 7 months ago 18
SQL Question

PHP Form with MySql Database

Hi I need some help to create an form from database:Database

And I have this:

global $wpdb;
//$query ="SELECT modelo,ID FROM volumes";
$query ="SELECT Modelo,ID FROM volumes order by ID";
$wpdb->query($query)


And this:

<div class="form_fields">
<p>
<label for="modelo">Model:
echo "<select name=Modelo value=''>Modelo</option>"; // list box select command

foreach ($wpdb->query($query) as $row){//Array or records stored in $row

echo "<option value=$row[ID]>$row[Modelo]</option>";

/* Option values are added by looping through the array */

}

echo "</select>";// Closing of list box
</label>
</p></div>


What is wrong?

Answer

Try this :

<?php
global $wpdb;
//$query ="SELECT modelo,ID FROM volumes"; 
$query = "SELECT Modelo,ID FROM volumes order by ID";
$results = $wpdb->get_results($query);
?>
<div class="form_fields">
    <p>
        <label for="modelo">Model:
            <select name=Modelo>
                <option value=''>Modelo</option>
                <?php
                foreach ($results as $row) {//Array or records stored in $row
                    echo "<option value='{$row->ID}'>{$row->Modelo}</option>";
                    /* Option values are added by looping through the array */
                }
                ?>
            </select>
        </label>
    </p>
</div>
Comments