Jump to content

sql in php form


andrej13

Recommended Posts

As you can see, my array contains drinks  $dranken = array("cola", "fanta", "bier", "koffie", "thee");

but how can I call the drinks up from an sql table instead of typing them in like I did now?

<?php if (!isset($_POST['submit'])) { ?>

    <html>

        <form method="post" action="<?php echo $PHP_SELF; ?>">

        <?php
        $dranken = array("cola", "fanta", "bier", "koffie", "thee");
        $prijzen = array("2", "2", "1.80", "2.20", "2.20");


        $i = 0;

        echo "<table>";
        while ($dranken[$i]) {
            $listnaam = $dranken[$i] . "_aantal";
            $optionlist = "<select name= '$listnaam'><option>0</option><option>1</option><option>2</option><option>3</option></select>";

            echo "<tr><td >" . $dranken[$i] . "</td>";
            echo "<td>" . $optionlist . "</td>";
            echo "<td>" . $prijzen[$i] . "</td></tr>";

            $i++;
        }

        echo "</table>";
        ?>

        <input type="submit" value="Toon Output" name="submit"/>

    </form>

    <?php
    }
    
    $dranken = array("cola", "fanta", "bier", "koffie", "thee");
    $prijzen = array("2", "2", "1.80", "2.20", "2.20");
    
    $i = 0;
    $totaalPrijs = 0;
    while ($dranken[$i]) {
        $aantal = $_POST[$dranken[$i] . "_aantal"];
        if ($aantal > 0) {
            $prijsperDrank = $aantal * $prijzen[$i];
            echo $dranken[$i] . ":" . $aantal . "Prijs:" . $prijsperDrank . "</br>";
            $totaalPrijs += $prijsperDrank;
            echo "totaal: $totaalPrijs";
        }
        $i++;
    }
    ?>

</html>

 

Link to comment
https://forums.phpfreaks.com/topic/230001-sql-in-php-form/
Share on other sites

try this

 

<?php

 

// connect to your database

 

$DrinkArray=array() ;

 

$DrinkResult=mysql_query("select flavour from drinks ")or die(mysql_error());

 

while($DrinkRow=mysql_fetch_assoc($DrinkResult)){

$DrinkArray[]=$DrinkRow[flavour];

}

 

//check your output with something like this

foreach($DrinkArray as $value){

echo $value.'<br />';

}

?>

Link to comment
https://forums.phpfreaks.com/topic/230001-sql-in-php-form/#findComment-1184761
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.